gen-bsky 0.1.22

A Library to generate and post a bluesky post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
#[cfg_attr(doc, aquamarine::aquamarine)]
/// # Post Module Documentation
///
/// ## Overview
///
/// The `post` module provides functionality for managing and publishing blog posts to Bluesky.
/// It handles the complete workflow from loading post files to authenticating with Bluesky,
/// publishing posts, and managing post lifecycle including cleanup of successfully posted content.
///
/// ## Module Architecture
///
/// ```mermaid
/// graph TD
///     A["Post Files (.post)"] --> B["Post::load()"]
///     B --> C["BskyPost Collection"]
///     C --> D["Post::post_to_bluesky()"]
///     D --> E["Bluesky Authentication"]
///     E --> F["Publish Posts"]
///     F --> G["Update Post States"]
///     G --> H["Post::delete_posted_posts()"]
///     H --> I["Cleanup Files"]
/// ```
///
/// ## Post Lifecycle
///
/// Posts progress through several states during their lifecycle:
///
/// 1. **Read**: Posts are loaded from `.post` files and ready for publishing
/// 2. **Posted**: Posts have been successfully published to Bluesky
/// 3. **Deleted**: Post files have been cleaned up after successful publishing
///
/// ```mermaid
/// stateDiagram-v2
///     [*] --> Read: Load from file
///     Read --> Posted: Successful publish
///     Read --> Read: Publish error
///     Posted --> Deleted: File cleanup
///     Deleted --> [*]
/// ```
///
/// ## Key Components
///
/// - **Post**: Main struct managing the posting workflow and Bluesky credentials
/// - **BskyPost**: Individual post representation with state tracking
/// - **BskyPostState**: Enum representing the current state of each post
/// - **PostError**: Comprehensive error handling for all posting operations
///
/// ## Authentication
///
/// The module handles Bluesky authentication using:
/// - User identifier (handle or email)
/// - Password or app-specific password
/// - Automatic session management and labeler configuration
///
/// ## File Format
///
/// Posts are expected to be stored as JSON files with `.post` extension containing
/// Bluesky `RecordData` structures that can be directly published to the platform.
///
/// ## Testing Mode
///
/// The module supports a testing mode activated by the `TESTING` environment variable,
/// which simulates posting without actually publishing to Bluesky.
///
/// ## Error Handling
///
/// Comprehensive error handling covers:
/// - Authentication failures
/// - File I/O errors
/// - JSON deserialization errors
/// - Bluesky API errors
/// - Network connectivity issues
///
/// ## Usage Example
///
/// ```rust,ignore
/// use crate::post::Post;
///
/// async fn publish_posts() -> Result<(), Box<dyn std::error::Error>> {
///     let mut poster = Post::new("user@example.com", "password")?;
///     
///     poster
///         .load("./posts")?        // Load .post files
///         .post_to_bluesky().await? // Publish to Bluesky
///         .delete_posted_posts()?;  // Clean up successful posts
///         
///     println!("Cleaned up {} posts", poster.count_deleted());
///     Ok(())
/// }
/// ```
use std::{fmt::Display, fs, io::BufReader, path::Path};

const TESTING_FLAG: &str = "TESTING";
pub(crate) mod bsky_post;

use bsky_post::{BskyPost, BskyPostState};
use bsky_sdk::{agent::config::Config as BskyConfig, BskyAgent};
// use serde::{Deserialize, Serialize};
use thiserror::Error;

/// # PostError - Comprehensive Error Handling for Post Operations
///
/// The `PostError` enum provides detailed error information for all operations
/// in the post module, from authentication to file handling to API communication.
///
/// ## Error Categories
///
/// ### Authentication Errors
/// - `NoBlueskyIdentifier`: Missing or empty user identifier
/// - `NoBlueskyPassword`: Missing or empty password
/// - `BlueskyLoginError`: Authentication failure with Bluesky
///
/// ### File System Errors
/// - `Io`: File operations, directory reading, or file deletion errors
///
/// ### Data Processing Errors
/// - `SerdeJsonError`: JSON parsing errors when reading post files
///
/// ### API Communication Errors
/// - `BskySdk`: Bluesky SDK errors including network and API issues
///
/// ## Error Handling Strategy
///
/// ```rust,ignore
/// use crate::post::{Post, PostError};
///
/// async fn handle_posting() {
///     match Post::new("user", "pass") {
///         Ok(mut poster) => {
///             if let Err(e) = poster.load("./posts") {
///                 match e {
///                     PostError::Io(io_err) => eprintln!("File error: {}", io_err),
///                     PostError::SerdeJsonError(json_err) => eprintln!("JSON error: {}", json_err),
///                     _ => eprintln!("Other error: {}", e),
///                 }
///             }
///         },
///         Err(PostError::NoBlueskyIdentifier) => eprintln!("Missing username"),
///         Err(PostError::NoBlueskyPassword) => eprintln!("Missing password"),
///         Err(e) => eprintln!("Setup error: {}", e),
///     }
/// }
/// ```
///
/// ## Recovery Strategies
///
/// - **Authentication errors**: Verify credentials and network connectivity
/// - **File errors**: Check file permissions and directory structure
/// - **JSON errors**: Validate post file format and content
/// - **API errors**: Check network connectivity and Bluesky service status
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum PostError {
    /// Bluesky identifier required to post to bluesky not provided.
    ///
    /// This error occurs when:
    /// - An empty string is passed as the identifier to `Post::new()`
    /// - The identifier parameter is whitespace-only
    ///
    /// ## Resolution
    /// Provide a valid Bluesky handle (e.g., "user.bsky.social") or email address.
    #[error("No bluesky identifier provided")]
    NoBlueskyIdentifier,

    /// Bluesky password required to post to bluesky not provided.
    ///
    /// This error occurs when:
    /// - An empty string is passed as the password to `Post::new()`
    /// - The password parameter is whitespace-only
    ///
    /// ## Resolution
    /// Provide a valid password or app-specific password for the Bluesky account.
    #[error("No bluesky password provided")]
    NoBlueskyPassword,

    /// Bluesky SDK reports a login error.
    ///
    /// This error occurs when:
    /// - Invalid credentials are provided
    /// - Network connectivity issues prevent authentication
    /// - Bluesky service is temporarily unavailable
    /// - Account is suspended or restricted
    ///
    /// ## Resolution
    /// - Verify username and password are correct
    /// - Check network connectivity
    /// - Verify account status on Bluesky
    /// - Wait and retry if service issues are suspected
    #[error("bsky_sdk create_session error says: {0:?}")]
    BlueskyLoginError(String),

    /// Error reported from the std::io module.
    ///
    /// This error occurs during:
    /// - Reading post directories in `load()`
    /// - Opening post files for reading
    /// - Deleting posted files in `delete_posted_posts()`
    /// - Any other file system operations
    ///
    /// ## Common Causes
    /// - File or directory doesn't exist
    /// - Insufficient permissions
    /// - Disk full or I/O errors
    /// - File in use by another process
    #[error("io error says: {0:?}")]
    Io(#[from] std::io::Error),

    /// Error reported from the serde_json crate.
    ///
    /// This error occurs when:
    /// - Post files contain invalid JSON
    /// - JSON structure doesn't match expected RecordData format
    /// - File encoding issues
    ///
    /// ## Resolution
    /// - Validate JSON syntax in post files
    /// - Ensure post files match Bluesky RecordData schema
    /// - Check file encoding (should be UTF-8)
    #[error("serde_json create_session error says: {0:?}")]
    SerdeJsonError(#[from] serde_json::error::Error),

    /// Error reported from the bsky_sdk crate.
    ///
    /// This error occurs during:
    /// - API calls to Bluesky
    /// - Network communication
    /// - Post creation and submission
    /// - Preference retrieval
    ///
    /// ## Common Causes
    /// - Network connectivity issues
    /// - API rate limiting
    /// - Invalid post content
    /// - Service temporarily unavailable
    #[error("bsky_sdk error says: {0:?}")]
    BskySdk(#[from] bsky_sdk::Error),

    /// Error writing back to frontmatter.
    #[error("frontmatter write-back error: {0:?}")]
    FmWrite(#[from] crate::frontmatter_writeback::FmWriteError),
}

/// # Post - Bluesky Post Management and Publishing
///
/// The `Post` struct manages the complete lifecycle of publishing posts to Bluesky,
/// from loading post files to authentication, publishing, and cleanup.
///
/// ## Fields
///
/// - `bsky_posts`: Collection of individual posts with state tracking
/// - `id`: Bluesky user identifier (handle or email)
/// - `pwd`: Authentication password or app-specific password
///
/// ## Memory Characteristics
///
/// - Memory usage scales linearly with the number of posts loaded
/// - Credentials are stored as owned `String` instances for the session duration
/// - Post data is kept in memory during the entire publishing workflow
/// - Implements `Clone` for flexibility, but credentials are duplicated
///
/// ## Usage Pattern
///
/// ```rust,ignore
/// use crate::post::Post;
///
/// async fn publish_workflow() -> Result<(), Box<dyn std::error::Error>> {
///     let mut poster = Post::new("user.bsky.social", "app-password")?;
///     
///     // Chain operations for fluent API usage
///     poster
///         .load("./post_directory")?        // Load all .post files
///         .post_to_bluesky().await?         // Authenticate and publish
///         .delete_posted_posts()?;          // Clean up successful posts
///         
///     println!("Published and cleaned up {} posts", poster.count_deleted());
///     Ok(())
/// }
/// ```
///
/// ## Thread Safety
///
/// This struct is not thread-safe due to:
/// - Mutable references returned by methods
/// - Internal state mutations during posting
/// - File system operations
///
/// ## Credential Security
///
/// - Credentials are stored in memory as plain text
/// - No automatic credential clearing (consider `zeroize` for sensitive use)
/// - Credentials are logged only in debug builds with appropriate redaction
#[derive(Default, Debug, Clone)]
pub struct Post {
    /// Collection of Bluesky posts with individual state tracking
    bsky_posts: Vec<BskyPost>,

    /// Bluesky user identifier (handle like "user.bsky.social" or email)
    id: String,

    /// Authentication password or app-specific password
    pwd: String,
    // Future: Track sent posts for additional state management
    // sent_posts: Vec<PathBuf>,
}

impl Post {
    /// Creates a new Post instance with Bluesky credentials.
    ///
    /// Initializes a Post struct with the provided Bluesky authentication credentials.
    /// The post collection starts empty and credentials are validated for basic requirements.
    ///
    /// ## Parameters
    ///
    /// - `id`: Bluesky user identifier (handle like "user.bsky.social" or email address)
    /// - `password`: Account password or app-specific password
    ///
    /// ## Returns
    ///
    /// - `Ok(Post)`: Successfully created Post instance ready for loading posts
    /// - `Err(PostError)`: Validation failure for credentials
    ///
    /// ## Errors
    ///
    /// - `PostError::NoBlueskyIdentifier`: Empty or whitespace-only identifier
    /// - `PostError::NoBlueskyPassword`: Empty or whitespace-only password
    ///
    /// ## Security Considerations
    ///
    /// - Credentials are stored as plain text in memory
    /// - No validation of credential format or existence
    /// - Consider using app-specific passwords instead of main account passwords
    ///
    /// ## Examples
    ///
    /// ```rust,ignore
    /// use crate::post::Post;
    ///
    /// // Using handle format
    /// let poster = Post::new("user.bsky.social", "app-password")?;
    ///
    /// // Using email format  
    /// let poster = Post::new("user@example.com", "password")?;
    ///
    /// // Error handling
    /// match Post::new("", "password") {
    ///     Err(PostError::NoBlueskyIdentifier) => println!("Missing username"),
    ///     Ok(_) => println!("Success"),
    ///     Err(e) => println!("Other error: {}", e),
    /// }
    /// ```
    pub fn new(id: &str, password: &str) -> Result<Self, PostError> {
        if id.is_empty() {
            return Err(PostError::NoBlueskyIdentifier);
        };

        if password.is_empty() {
            return Err(PostError::NoBlueskyPassword);
        };

        Ok(Post {
            id: id.to_string(),
            pwd: password.to_string(),
            ..Default::default()
        })
    }

    /// Loads Bluesky post documents from a specified directory.
    ///
    /// Scans the provided directory for files with `.post` extension and loads them
    /// as JSON-formatted Bluesky `RecordData`. Successfully loaded posts are added
    /// to the internal collection with `Read` state.
    ///
    /// ## Parameters
    ///
    /// - `directory`: Path to directory containing `.post` files (implements `AsRef<Path> + Display`)
    ///
    /// ## Returns
    ///
    /// - `Ok(&mut Self)`: Fluent interface for method chaining
    /// - `Err(PostError)`: File system or parsing errors
    ///
    /// ## Errors
    ///
    /// - `PostError::Io`: Directory doesn't exist, permission denied, or I/O errors
    /// - `PostError::SerdeJsonError`: Invalid JSON format or schema mismatch
    ///
    /// ## behaviour
    ///
    /// - Only processes files with `.post` extension
    /// - Ignores files that don't match the extension
    /// - Accumulated posts from multiple `load()` calls
    /// - Non-UTF-8 filenames may cause panics (uses `.unwrap()`)
    ///
    /// ## File Format
    ///
    /// Post files must contain valid JSON matching Bluesky's `RecordData` schema:
    /// ```json
    /// {
    ///   "text": "Your post content here",
    ///   "createdAt": "2024-01-01T00:00:00.000Z",
    ///   "langs": ["en"]
    /// }
    /// ```
    ///
    /// ## Performance
    ///
    /// - **Time Complexity**: O(n) where n is the number of `.post` files
    /// - **Memory**: Loads all posts into memory simultaneously
    /// - **I/O**: Sequential file reading, not optimized for large directories
    ///
    /// ## Examples
    ///
    /// ```rust,ignore
    /// use crate::post::Post;
    ///
    /// let mut poster = Post::new("user", "pass")?;
    ///
    /// // Load from single directory
    /// poster.load("./posts")?;
    ///
    /// // Load from multiple directories (accumulative)
    /// poster.load("./posts1")?
    ///       .load("./posts2")?;
    ///
    /// // Using PathBuf
    /// use std::path::PathBuf;
    /// let post_dir = PathBuf::from("./posts");
    /// poster.load(post_dir)?;
    /// ```
    pub fn load<P>(&mut self, directory: P) -> Result<&mut Self, PostError>
    where
        P: AsRef<Path> + Display,
    {
        let files = fs::read_dir(&directory)?;
        let mut bsky_posts = Vec::new();
        for file in files {
            let file = file?;
            let file_name = file.file_name().into_string().unwrap();
            if file_name.ends_with(".post") {
                let file_path = file.path();
                let post = fs::File::open(&file_path)?;
                let reader = BufReader::new(post);
                let post_file: bsky_post::PostFile = serde_json::from_reader(reader)?;
                let bsky_post = if let Some(src) = post_file.source_path {
                    BskyPost::new_with_source(post_file.record, file_path, src)
                } else {
                    BskyPost::new(post_file.record, file_path)
                };
                bsky_posts.push(bsky_post);
            }
        }
        self.bsky_posts.extend(bsky_posts);
        Ok(self)
    }

    /// Publishes all loaded posts to Bluesky.
    ///
    /// Authenticates with Bluesky using stored credentials, then publishes all posts
    /// currently in `Read` state. Posts are processed sequentially, and individual
    /// failures don't stop the overall process.
    ///
    /// ## Returns
    ///
    /// - `Ok(&mut Self)`: Publishing completed (check individual post states for results)
    /// - `Err(PostError)`: Authentication or SDK initialization failure
    ///
    /// ## Errors
    ///
    /// - `PostError::BlueskyLoginError`: Invalid credentials or authentication failure
    /// - `PostError::BskySdk`: Network errors, API errors, or SDK issues
    ///
    /// ## Async behaviour
    ///
    /// - Establishes Bluesky session asynchronously
    /// - Posts are published sequentially (not concurrently)
    /// - Network timeouts are handled by the underlying SDK
    ///
    /// ## Authentication Flow
    ///
    /// 1. Create BskyAgent with default configuration
    /// 2. Login using stored credentials
    /// 3. Retrieve and configure user preferences
    /// 4. Configure content labelers from preferences
    ///
    /// ## Testing Mode
    ///
    /// When `TESTING` environment variable is set:
    /// - No actual posts are made to Bluesky
    /// - All posts are logged as "successfully posted"
    /// - Post states remain as `Read` (not changed to `Posted`)
    /// - Useful for CI/CD and development testing
    ///
    /// ## Error Recovery
    ///
    /// - Individual post failures are logged and skipped
    /// - Failed posts remain in `Read` state for retry
    /// - Successful posts transition to `Posted` state
    /// - Authentication failure stops the entire process
    ///
    /// ## Logging
    ///
    /// - Info: Authentication success, testing mode, successful posts
    /// - Debug: Post content preview, validation status
    /// - Warn: Individual post failures
    ///
    /// ## Examples
    ///
    /// ```rust,ignore
    /// use crate::post::Post;
    /// use std::env;
    ///
    /// async fn publish_example() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut poster = Post::new("user", "pass")?;
    ///     poster.load("./posts")?;
    ///
    ///     // Enable testing mode
    ///     env::set_var("TESTING", "1");
    ///     
    ///     // Publish posts
    ///     poster.post_to_bluesky().await?;
    ///
    ///     // Check results
    ///     let posted_count = poster.bsky_posts.iter()
    ///         .filter(|p| p.state() == &BskyPostState::Posted)
    ///         .count();
    ///     println!("Posted {} posts", posted_count);
    ///     
    ///     Ok(())
    /// }
    /// ```
    pub async fn post_to_bluesky(&mut self) -> Result<&mut Self, PostError> {
        let bsky_config = BskyConfig::default();

        let agent = BskyAgent::builder().config(bsky_config).build().await?;

        agent
            .login(&self.id, &self.pwd)
            .await
            .map_err(|e| PostError::BlueskyLoginError(e.to_string()))?;
        // Set labelers from preferences
        let preferences = agent.get_preferences(true).await?;
        agent.configure_labelers_from_preferences(&preferences);
        log::info!("Bluesky login successful!");

        let testing = std::env::var(TESTING_FLAG).is_ok();
        if testing {
            log::info!("No posts will be made to bluesky as this is a test.");
        }

        for bsky_post in &mut self
            .bsky_posts
            .iter_mut()
            .filter(|p| p.state() == &BskyPostState::Read)
        {
            // Skip posts whose source markdown already has [bluesky].published set.
            if let Some(src) = bsky_post.source_path() {
                if crate::frontmatter_writeback::read_bluesky_date_field(src, "published").is_some()
                {
                    log::debug!("Skipping post — [bluesky].published already set in {src:?}");
                    continue;
                }
            }

            log::debug!("Post: {}", bsky_post.post().text.clone());

            if testing {
                // log::debug!("Deleting related file: {:?}", bsky_post.file_path);
                // fs::remove_file(&bsky_post.file_path)?;

                log::info!(
                    "Successfully posted `{}` to Bluesky",
                    bsky_post.file_path().to_string_lossy()
                );
            } else {
                let result = agent.create_record(bsky_post.post().clone()).await;

                let Ok(output) = result else {
                    log::warn!("Error posting to Bluesky: {}", result.err().unwrap());
                    continue;
                };

                log::debug!("Post validation: `{:?}`", output.validation_status.as_ref());

                log::info!(
                    "Successfully posted `{}` to Bluesky",
                    bsky_post
                        .post()
                        .text
                        .split_terminator('\n')
                        .collect::<Vec<&str>>()[0],
                );

                bsky_post.set_state(BskyPostState::Posted);
            };
        }

        Ok(self)
    }

    /// Deletes post files for all successfully posted content.
    ///
    /// Removes the source `.post` files from the filesystem for all posts
    /// currently in `Posted` state, then transitions them to `Deleted` state.
    /// This cleanup step prevents reprocessing of already-published content.
    ///
    /// ## Returns
    ///
    /// - `Ok(&mut Self)`: Cleanup completed (check individual post states for results)
    /// - `Err(PostError)`: File system errors during deletion
    ///
    /// ## Errors
    ///
    /// - `PostError::Io`: File deletion failures, permission issues, or I/O errors
    ///
    /// ## behaviour
    ///
    /// - Only processes posts in `Posted` state
    /// - Individual file deletion failures stop the entire process
    /// - Successfully deleted files transition posts to `Deleted` state
    /// - Files are deleted from filesystem immediately
    ///
    /// ## Safety Considerations
    ///
    /// - **Irreversible operation**: Deleted files cannot be recovered
    /// - Ensure posts are actually published before calling this method
    /// - Consider backup strategies for important content
    /// - File locks or permissions may prevent deletion
    ///
    /// ## Use Cases
    ///
    /// - Cleanup after successful publishing workflow
    /// - Prevent duplicate posting in subsequent runs
    /// - Disk space management
    /// - Automated CI/CD pipeline cleanup
    ///
    /// ## Error Recovery
    ///
    /// - Failed deletions leave posts in `Posted` state
    /// - Partial deletions are possible (some files deleted, others not)
    /// - Manual intervention may be required for locked files
    /// - Consider retry logic for transient filesystem issues
    ///
    /// ## Logging
    ///
    /// - Debug: File paths being deleted
    /// - Info: Successful deletion confirmations with file paths
    ///
    /// ## Examples
    ///
    /// ```rust,ignore
    /// use crate::post::{Post, PostError};
    ///
    /// async fn cleanup_workflow() -> Result<(), PostError> {
    ///     let mut poster = Post::new("user", "pass")?;
    ///     
    ///     poster.load("./posts")?
    ///           .post_to_bluesky().await?
    ///           .delete_posted_posts()?;  // Clean up successful posts
    ///           
    ///     println!("Deleted {} post files", poster.count_deleted());
    ///     Ok(())
    /// }
    ///
    /// // Error handling example
    /// async fn careful_cleanup(mut poster: Post) {
    ///     match poster.delete_posted_posts() {
    ///         Ok(_) => println!("Cleanup successful"),
    ///         Err(PostError::Io(io_err)) => {
    ///             eprintln!("File deletion failed: {}", io_err);
    ///             // Posts remain in Posted state for manual review
    ///         },
    ///         Err(e) => eprintln!("Unexpected error: {}", e),
    ///     }
    /// }
    /// ```
    pub fn delete_posted_posts(&mut self) -> Result<&mut Self, PostError> {
        for bsky_post in &mut self
            .bsky_posts
            .iter_mut()
            .filter(|p| p.state() == &BskyPostState::Posted)
        {
            // Write `published = <today>` back to the source .md file when known.
            if let Some(src) = bsky_post.source_path() {
                let today = crate::draft::today();
                match crate::frontmatter_writeback::write_bluesky_date_field(
                    src,
                    "published",
                    today,
                ) {
                    Ok(()) => log::debug!(
                        "Wrote [bluesky].published = {} to `{}`",
                        today,
                        src.display()
                    ),
                    Err(e) => log::warn!(
                        "Failed to write [bluesky].published to `{}`: {e}",
                        src.display()
                    ),
                }
            }

            log::debug!("Deleting related file: {:?}", bsky_post.file_path());
            fs::remove_file(bsky_post.file_path())?;

            log::info!(
                "Successfully deleted `{}` bluesky post file",
                bsky_post.file_path().to_string_lossy()
            );

            bsky_post.set_state(BskyPostState::Deleted);
        }

        Ok(self)
    }

    /// Returns the count of posts that have been successfully deleted.
    ///
    /// Counts all posts currently in `Deleted` state, which indicates they were
    /// successfully published to Bluesky and their source files have been removed
    /// from the filesystem.
    ///
    /// ## Returns
    ///
    /// - `usize`: Number of posts in `Deleted` state
    ///
    /// ## Performance
    ///
    /// - **Time Complexity**: O(n) where n is the total number of loaded posts
    /// - **Space Complexity**: O(1) constant space usage
    /// - **Cost**: Iterates through all posts to count matching states
    ///
    /// ## Use Cases
    ///
    /// - Reporting and logging after cleanup operations
    /// - Verification that cleanup completed successfully
    /// - Metrics collection for automated workflows
    /// - Progress tracking in batch processing scenarios
    ///
    /// ## State Transitions
    ///
    /// Posts reach `Deleted` state through this workflow:
    /// 1. `Read` (after loading from files)
    /// 2. `Posted` (after successful publish to Bluesky)
    /// 3. `Deleted` (after successful file cleanup)
    ///
    /// ## Examples
    ///
    /// ```rust,ignore
    /// use crate::post::Post;
    ///
    /// async fn workflow_with_reporting() -> Result<(), Box<dyn std::error::Error>> {
    ///     let mut poster = Post::new("user", "pass")?;
    ///     
    ///     poster.load("./posts")?;
    ///     let initial_count = poster.bsky_posts.len();
    ///     println!("Loaded {} posts", initial_count);
    ///     
    ///     poster.post_to_bluesky().await?
    ///           .delete_posted_posts()?;
    ///           
    ///     let deleted_count = poster.count_deleted();
    ///     println!("Successfully processed {} of {} posts", deleted_count, initial_count);
    ///     
    ///     if deleted_count < initial_count {
    ///         println!("Warning: {} posts may have failed", initial_count - deleted_count);
    ///     }
    ///     
    ///     Ok(())
    /// }
    /// ```
    pub fn count_deleted(&self) -> usize {
        self.bsky_posts
            .iter()
            .filter(|b| b.state() == &BskyPostState::Deleted)
            .count()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env;
    use std::fs;
    use std::io::Write;
    use tempfile::TempDir;

    /// Helper function to create a test Post instance
    fn create_test_post() -> Result<Post, PostError> {
        Post::new("test.user.bsky.social", "test-password")
    }

    /// Helper function to create a temporary directory with test post files
    fn create_test_posts_directory() -> Result<TempDir, std::io::Error> {
        let temp_dir = tempfile::tempdir()?;

        // Create valid post file
        let post_content = r#"{
            "text": "Test post content",
            "createdAt": "2024-01-01T00:00:00.000Z",
            "langs": ["en"]
        }"#;
        let post_path = temp_dir.path().join("test1.post");
        let mut post_file = fs::File::create(post_path)?;
        post_file.write_all(post_content.as_bytes())?;

        // Create another valid post file
        let post_content2 = r#"{
            "text": "Another test post",
            "createdAt": "2024-01-02T00:00:00.000Z",
            "langs": ["en", "es"]
        }"#;
        let post_path2 = temp_dir.path().join("test2.post");
        let mut post_file2 = fs::File::create(post_path2)?;
        post_file2.write_all(post_content2.as_bytes())?;

        // Create a non-post file (should be ignored)
        let other_file = temp_dir.path().join("readme.txt");
        let mut other = fs::File::create(other_file)?;
        other.write_all(b"This is not a post file")?;

        Ok(temp_dir)
    }

    /// Helper function to create invalid JSON post file
    fn create_invalid_post_file(temp_dir: &TempDir) -> Result<(), std::io::Error> {
        let invalid_content = r#"{
            "text": "Invalid JSON",
            "createdAt": "2024-01-01T00:00:00.000Z"
            // Missing closing brace and comma
        "#;
        let invalid_path = temp_dir.path().join("invalid.post");
        let mut invalid_file = fs::File::create(invalid_path)?;
        invalid_file.write_all(invalid_content.as_bytes())?;
        Ok(())
    }

    // Constructor tests
    #[test]
    fn test_post_new_success() {
        let result = Post::new("test.user", "password123");
        assert!(result.is_ok());

        let post = result.unwrap();
        assert_eq!(post.id, "test.user");
        assert_eq!(post.pwd, "password123");
        assert_eq!(post.bsky_posts.len(), 0);
    }

    #[test]
    fn test_post_new_empty_identifier() {
        let result = Post::new("", "password");
        assert!(matches!(result, Err(PostError::NoBlueskyIdentifier)));
    }

    #[test]
    fn test_post_new_empty_password() {
        let result = Post::new("user", "");
        assert!(matches!(result, Err(PostError::NoBlueskyPassword)));
    }

    #[test]
    fn test_post_new_both_empty() {
        let result = Post::new("", "");
        // Should fail on identifier first
        assert!(matches!(result, Err(PostError::NoBlueskyIdentifier)));
    }

    #[test]
    fn test_post_new_whitespace_identifier() {
        let result = Post::new("   ", "password");
        // Current implementation allows whitespace, but should be empty string
        assert!(result.is_ok());
    }

    // Load method tests
    #[test]
    fn test_load_success() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;

        let result = post.load(temp_dir.path().to_string_lossy().to_string());
        assert!(result.is_ok());

        assert_eq!(post.bsky_posts.len(), 2);

        // Check that all posts are in Read state
        for bsky_post in &post.bsky_posts {
            assert_eq!(bsky_post.state(), &BskyPostState::Read);
        }

        // Verify post content - posts can be loaded in any order
        let post_texts: Vec<&str> = post
            .bsky_posts
            .iter()
            .map(|p| p.post().text.as_str())
            .collect();
        assert!(post_texts.contains(&"Test post content"));
        assert!(post_texts.contains(&"Another test post"));

        Ok(())
    }

    #[test]
    fn test_load_nonexistent_directory() {
        let mut post = create_test_post().unwrap();
        let result = post.load("/nonexistent/directory");
        assert!(matches!(result, Err(PostError::Io(_))));
    }

    #[test]
    fn test_load_invalid_json() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        create_invalid_post_file(&temp_dir)?;

        let result = post.load(temp_dir.path().to_string_lossy().to_string());
        // Should fail due to invalid JSON
        assert!(matches!(result, Err(PostError::SerdeJsonError(_))));

        Ok(())
    }

    #[test]
    fn test_load_empty_directory() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = tempfile::tempdir()?;

        let result = post.load(temp_dir.path().to_string_lossy().to_string());
        assert!(result.is_ok());
        assert_eq!(post.bsky_posts.len(), 0);

        Ok(())
    }

    #[test]
    fn test_load_no_post_files() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = tempfile::tempdir()?;

        // Create non-post files
        let txt_file = temp_dir.path().join("readme.txt");
        fs::write(txt_file, "Not a post file")?;

        let json_file = temp_dir.path().join("config.json");
        fs::write(json_file, "{\"config\": \"value\"}")?;

        let result = post.load(temp_dir.path().to_string_lossy().to_string());
        assert!(result.is_ok());
        assert_eq!(post.bsky_posts.len(), 0);

        Ok(())
    }

    #[test]
    fn test_load_accumulative() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;

        // Create first directory with posts
        let temp_dir1 = create_test_posts_directory()?;
        post.load(temp_dir1.path().to_string_lossy().to_string())?;
        assert_eq!(post.bsky_posts.len(), 2);

        // Create second directory with more posts
        let temp_dir2 = tempfile::tempdir()?;
        let post_content = r#"{
            "text": "Third post",
            "createdAt": "2024-01-03T00:00:00.000Z",
            "langs": ["fr"]
        }"#;
        let post_path = temp_dir2.path().join("test3.post");
        fs::write(post_path, post_content)?;

        post.load(temp_dir2.path().to_string_lossy().to_string())?;
        assert_eq!(post.bsky_posts.len(), 3);

        Ok(())
    }

    // Post to Bluesky tests (using testing mode)
    #[tokio::test]
    async fn test_post_to_bluesky_testing_mode() -> Result<(), Box<dyn std::error::Error>> {
        unsafe { env::set_var("TESTING", "1") };

        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        post.load(temp_dir.path().to_string_lossy().to_string())?;

        // In testing mode, this should not fail even with fake credentials
        let result = post.post_to_bluesky().await;

        // Clean up environment variable
        unsafe { env::remove_var("TESTING") };

        // Note: This test might fail due to actual network calls even in testing mode
        // depending on the implementation. The current code still tries to authenticate.
        // For a proper test, we'd need to mock the BskyAgent.

        // If authentication fails, that's expected with fake credentials
        match result {
            Err(PostError::BlueskyLoginError(_)) => {
                // Expected with fake credentials
            }
            Ok(_) => {
                // Success in testing mode (if implementation is properly mocked)
            }
            Err(e) => {
                // Other errors might indicate implementation issues
                println!("Unexpected error: {e:?}");
            }
        }

        Ok(())
    }

    // Delete posted posts tests
    #[test]
    fn test_delete_posted_posts_no_posted() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        post.load(temp_dir.path().to_string_lossy().to_string())?;

        // No posts are in Posted state yet
        let result = post.delete_posted_posts();
        assert!(result.is_ok());

        // Verify files still exist
        assert!(temp_dir.path().join("test1.post").exists());
        assert!(temp_dir.path().join("test2.post").exists());

        Ok(())
    }

    #[test]
    fn test_delete_posted_posts_with_posted() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        post.load(temp_dir.path().to_string_lossy().to_string())?;

        // Manually set one post to Posted state
        let first_post_file_path = post.bsky_posts[0].file_path().clone();
        post.bsky_posts[0].set_state(BskyPostState::Posted);

        let result = post.delete_posted_posts();
        assert!(result.is_ok());

        // Verify the posted file was deleted
        assert!(!first_post_file_path.exists());

        // Count files to verify only one was deleted
        let remaining_files: Vec<_> = ["test1.post", "test2.post"]
            .iter()
            .filter(|filename| temp_dir.path().join(filename).exists())
            .collect();
        assert_eq!(remaining_files.len(), 1);

        // Verify state transition
        assert_eq!(post.bsky_posts[0].state(), &BskyPostState::Deleted);
        assert_eq!(post.bsky_posts[1].state(), &BskyPostState::Read);

        Ok(())
    }

    #[test]
    fn test_delete_posted_posts_file_not_found() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        post.load(temp_dir.path().to_string_lossy().to_string())?;

        // Find the post that corresponds to test1.post and delete its file
        let test1_post_index = post
            .bsky_posts
            .iter()
            .position(|p| p.file_path().file_name().unwrap().to_str().unwrap() == "test1.post")
            .expect("Should find test1.post");

        // Remove the actual file
        fs::remove_file(post.bsky_posts[test1_post_index].file_path())?;

        // Set the post to Posted state
        post.bsky_posts[test1_post_index].set_state(BskyPostState::Posted);

        let result = post.delete_posted_posts();
        // Should fail because file doesn't exist
        assert!(matches!(result, Err(PostError::Io(_))));

        Ok(())
    }

    // Count deleted tests
    #[test]
    fn test_count_deleted_none() {
        let post = create_test_post().unwrap();
        assert_eq!(post.count_deleted(), 0);
    }

    #[test]
    fn test_count_deleted_with_deleted_posts() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        post.load(temp_dir.path().to_string_lossy().to_string())?;

        // Set posts to different states
        post.bsky_posts[0].set_state(BskyPostState::Deleted);
        post.bsky_posts[1].set_state(BskyPostState::Posted);

        assert_eq!(post.count_deleted(), 1);

        Ok(())
    }

    // Error handling tests
    #[test]
    fn test_error_display() {
        let errors = vec![
            PostError::NoBlueskyIdentifier,
            PostError::NoBlueskyPassword,
            PostError::BlueskyLoginError("Login failed".to_string()),
            PostError::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "File not found",
            )),
        ];

        for error in errors {
            let error_string = format!("{error}");
            assert!(!error_string.is_empty());
        }
    }

    // Clone and Debug tests
    #[test]
    fn test_post_clone() -> Result<(), Box<dyn std::error::Error>> {
        let mut original = create_test_post()?;
        let temp_dir = create_test_posts_directory()?;
        original.load(temp_dir.path().to_string_lossy().to_string())?;

        let cloned = original.clone();
        assert_eq!(original.id, cloned.id);
        assert_eq!(original.pwd, cloned.pwd);
        assert_eq!(original.bsky_posts.len(), cloned.bsky_posts.len());

        Ok(())
    }

    #[test]
    fn test_post_debug() {
        let post = create_test_post().unwrap();
        let debug_str = format!("{post:?}");
        assert!(debug_str.contains("Post"));
    }

    // Default implementation test
    #[test]
    fn test_post_default() {
        let post = Post::default();
        assert!(post.id.is_empty());
        assert!(post.pwd.is_empty());
        assert_eq!(post.bsky_posts.len(), 0);
    }

    // Fluent interface tests
    #[test]
    fn test_fluent_interface() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir1 = create_test_posts_directory()?;
        let temp_dir2 = tempfile::tempdir()?;

        // Create a post in second directory
        let post_content = r#"{
            "text": "Fluent test",
            "createdAt": "2024-01-04T00:00:00.000Z"
        }"#;
        fs::write(temp_dir2.path().join("fluent.post"), post_content)?;

        // Test method chaining
        let result = post
            .load(temp_dir1.path().to_string_lossy().to_string())?
            .load(temp_dir2.path().to_string_lossy().to_string());

        assert!(result.is_ok());
        assert_eq!(post.bsky_posts.len(), 3);

        Ok(())
    }

    // RED: source_path loading and published write-back (issue #909)

    fn create_post_file_with_source_path(
        dir: &std::path::Path,
        filename: &str,
        source_path: &str,
    ) -> std::path::PathBuf {
        let content = format!(
            r#"{{"text":"Test","createdAt":"2026-04-03T00:00:00.000000Z","langs":null,"reply":null,"embed":null,"facets":null,"tags":null,"entities":null,"labels":null,"source_path":"{source_path}"}}"#
        );
        let path = dir.join(filename);
        fs::write(&path, content).unwrap();
        path
    }

    fn create_md_with_bluesky_created(dir: &std::path::Path, filename: &str) -> std::path::PathBuf {
        let content = "+++\ntitle = \"Test\"\ndescription = \"test\"\ndate = 2026-04-03\n\n[bluesky]\ndescription = \"bsky desc\"\ncreated = 2026-04-03\n+++\n\nBody.";
        let path = dir.join(filename);
        fs::write(&path, content).unwrap();
        path
    }

    #[test]
    fn test_load_reads_source_path_from_post_file() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = tempfile::tempdir()?;
        let md_path = temp_dir.path().join("my-post.md");
        create_post_file_with_source_path(temp_dir.path(), "abc.post", &md_path.to_string_lossy());

        post.load(temp_dir.path().to_string_lossy().to_string())?;
        assert_eq!(post.bsky_posts.len(), 1);
        assert_eq!(
            post.bsky_posts[0].source_path(),
            Some(&md_path),
            "source_path should be loaded from .post file"
        );
        Ok(())
    }

    #[test]
    fn test_delete_posted_posts_writes_published_to_frontmatter() {
        // delete_posted_posts is where the write-back happens; no network needed.
        let mut post = create_test_post().unwrap();
        let temp_dir = tempfile::tempdir().unwrap();

        // Create a .md file with [bluesky].created
        let md_path = create_md_with_bluesky_created(temp_dir.path(), "my-post.md");

        // Create a .post file that references this .md
        create_post_file_with_source_path(temp_dir.path(), "abc.post", &md_path.to_string_lossy());

        post.load(temp_dir.path().to_string_lossy().to_string())
            .unwrap();
        assert_eq!(post.bsky_posts.len(), 1);

        // Simulate a successful post by setting state to Posted
        post.bsky_posts[0].set_state(BskyPostState::Posted);

        post.delete_posted_posts().unwrap();

        let md_content = fs::read_to_string(&md_path).unwrap();
        assert!(
            md_content.contains("published ="),
            ".md file should have published date after delete_posted_posts: {md_content}"
        );
    }

    // Memory and performance tests
    #[test]
    fn test_large_number_of_posts() -> Result<(), Box<dyn std::error::Error>> {
        let mut post = create_test_post()?;
        let temp_dir = tempfile::tempdir()?;

        // Create 100 post files
        for i in 0..100 {
            let post_content = format!(
                r#"{{
                    "text": "Post number {i}",
                    "createdAt": "2024-01-01T00:00:00.000Z"
                }}"#
            );
            let post_path = temp_dir.path().join(format!("post{i}.post"));
            fs::write(post_path, post_content)?;
        }

        let result = post.load(temp_dir.path().to_string_lossy().to_string());
        assert!(result.is_ok());
        assert_eq!(post.bsky_posts.len(), 100);

        // Test counting with different states
        for i in 0..30 {
            post.bsky_posts[i].set_state(BskyPostState::Posted);
        }
        for i in 30..50 {
            post.bsky_posts[i].set_state(BskyPostState::Deleted);
        }

        assert_eq!(post.count_deleted(), 20);

        Ok(())
    }
}