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
use lazy_db::*;
use crate::home_dir;
use crate::list;
use crate::unwrap_opt;
use soulog::*;
use std::fs;
use std::path::PathBuf;
use std::path::Path;
use crate::entry::Entry;
use crate::moc::MOC;

pub struct Archive {
    database: LazyDB,
    uid: u64,
    pub itver: u16,
}

impl Archive {
    /// Initialises a new archive, will throw error if one already exists
    pub fn init(mut logger: impl Logger) -> Self {
        let path = home_dir().join("archive");
        let path_string = path.to_string_lossy();
        // Check if archive already exists
        if path.exists() {
            log!((logger.error) Init("Archive '{path_string}' already exists, try wiping it before initialising again") as Fatal);
            return logger.crash()
        }

        log!((logger) Init("Initialising a new archive at '{path_string}'..."));
        let database = if_err!((logger) [Init, err => ("While initialising database: {err:?}")] retry LazyDB::init(&path));
        
        let uid = {
            use std::collections::hash_map::RandomState;
            use std::hash::{BuildHasher, Hasher};
            RandomState::new().build_hasher().finish()
        };
        let itver = 0u16;

        log!((logger) Init("Writing uid and itver to archive..."));
        if_err!((logger) [Init, err => ("While writing uid: {err:?}")] retry write_database!((&database) uid = new_u64(uid)));
        if_err!((logger) [Init, err => ("While writing itver: {err:?}")] retry write_database!((&database) itver = new_u16(itver)));

        log!((logger) Init("Initialising sorted and unsorted entry containers..."));
        if_err!((logger) [Init, err => ("While writing stack length: {err:?}")] retry write_database!((&database) /order/sorted::length = new_u16(0)));
        if_err!((logger) [Init, err => ("While writing stack length: {err:?}")] retry write_database!((&database) /order/unsorted::length = new_u16(0)));

        log!((logger.vital) Init("Successfully initialised archive '{path_string}'") as Log);
        Self {
            database,
            uid,
            itver,
        }
    }

    /// Loads an archive at the cli's home
    #[inline]
    pub fn load(logger: impl Logger) -> Self {
        let path = home_dir().join("archive");
        Self::load_dir(path, logger)
    }

    /// Loads an archive at a specified path
    pub fn load_dir(path: PathBuf, mut logger: impl Logger) -> Self {
        let path_string = path.to_string_lossy();
        log!((logger) Archive("Loading archive '{path_string}'..."));

        // Checks if path exists or not
        if !path.is_dir() {
            log!((logger.vital) Archive("Archive '{path_string}' not found; initialising a new one...") as Inconvenience);
            return Self::init(logger)
        };

        let database = if_err!((logger) [Archive, err => ("While loading archive '{path_string}': {err:?}")] retry LazyDB::load_dir(&path));
        log!((logger) Archive("Loading uid and itver of archive..."));
        let uid = if_err!((logger) [Archive, err => ("While loading archive uid: {err:?}")] retry (|| search_database!((&database) uid)?.collect_u64())());
        let itver = if_err!((logger) [Archive, err => ("While loading archive itver: {err:?}")] retry (|| search_database!((&database) itver)?.collect_u16())());

        log!((logger.verbose) Archive("Successfully loaded archive at '{path_string}'") as Log);
        log!((logger) Archive(""));

        Self {
            database,
            uid,
            itver,
        }
    }

    /// Rolls back to last backup
    pub fn rollback(force: bool, mut logger: impl Logger) {
        log!((logger) RollBack("Rolling back to last backup..."));
        log!((logger.vital) RollBack("Rollback cannot revert successful commits; only unsuccessful ones that corrupt the archive.") as Warning);
        let path = home_dir().join("backup.ldb");
        if !path.is_file() {
            log!((logger.error) RollBack("No recent backups made; cannot rollback") as Fatal);
            return logger.crash();
        } Self::load_backup(path, force, logger.hollow());
        log!((logger.vital) RollBack("Successfully rolled back to last backup") as Log);
    }

    /// Backs up home archive to specified path
    pub fn backup(out_path: impl AsRef<Path>, mut logger: impl Logger) {
        let out_path = out_path.as_ref();
        let path = home_dir().join("archive");
        let path_string = path.to_string_lossy();
        let out_string = out_path.to_string_lossy();
        
        log!((logger) Backup("Backing up archive '{path_string}' as '{out_string}'..."));

        if !path.is_dir() {
            log!((logger.error) Backup("Archive does not exist, run `diary-cli init` to create a new one before you can back it up.") as Fatal);
            return logger.crash();
        }

        let database = if_err!((logger) [Backup, err => ("While backing up archive: {err:?}")] retry LazyDB::load_dir(&path));
        if_err!((logger) [Backup, err => ("While backing up archive: {err:?}")] retry database.compile(out_path));
        log!((logger.vital) Backup("Successfully backed up archive '{path_string}' as '{out_string}'") as Log);
        log!((logger) Backup(""));
    }

    /// Loads a backup if that backup is the same as the active archive and or newer than the active archive, otherwise errors will be thrown
    pub fn load_backup(path: impl AsRef<Path>, force: bool, mut logger: impl Logger) {
        let path = path.as_ref();
        let archive = home_dir().join("archive");
        let archive_string = archive.to_string_lossy();
        let path_string = path.to_string_lossy();

        log!((logger) Backup("Loading archive backup '{path_string}'..."));

        // Check if backup exists
        if !path.is_file() {
            log!((logger.error) Backup("Backup file '{path_string}' does not exist") as Fatal);
            return logger.crash();
        }

        // Check if archive already exists
        if archive.is_dir() {
            log!((logger.vital) Backup("Detected that there is already a loaded archive at '{archive_string}'") as Inconvenience);
            let old = Archive::load(logger.hollow()); // Loads old archive

            if force {
                log!((logger.vital) Backup("Forcefully loading backup; this may result in archive data loss") as Warning);
            }

            // Load new archive
            let new = home_dir().join("new");
            if_err!((logger) [Backup, err => ("While decompiling backup '{path_string}': {err:?}")] retry LazyDB::decompile(path, &new));
            let new = Archive::load_dir(new, logger.hollow());

            let _ = std::fs::remove_dir_all(new.database.path()); // cleanup

            // Check if uid is the same and that the itver is higher
            if new.uid != old.uid && !force {
                log!((logger.error) Backup("Cannot load backup as it is a backup of a different archive (uids don't match)") as Fatal);
                log!((logger.vital) Backup("If you still want to load it (deleting your current archive in the process) then run the same command but with `-f` to force it.") as Warning);
                return logger.crash();
            }

            if old.itver == new.itver && !force {
                log!((logger.vital) Backup("Detected that backup is the same age as the currently loaded archive (itver is the same)") as Warning);
            }

            if old.itver > new.itver && !force {
                log!((logger.error) Backup("Cannot load backup as it is older than the currently loaded archive (itver is less)") as Fatal);
                log!((logger.vital) Backup("If you still want to load it (losing un-backed changes in the process) then run the same command but with `-f` to force it.") as Warning);
                return logger.crash();
            }
            
            let _ = std::fs::remove_dir_all(&archive); // cleanup
        }

        if_err!((logger) [Backup, err => ("While decompiling backup '{path_string}': {err:?}")] retry LazyDB::decompile(path, &archive));
        log!((logger.vital) Backup("Successfully loaded backup '{path_string}'") as Log);
    }

    /// Wipes the specified archive and asks the user for confirmation
    pub fn wipe(self, mut logger: impl Logger) {
        // Confirm with the user about the action
        let expected = "I, as the user, confirm that I fully understand that I am wiping my ENTIRE archive and that this action is permanent and irreversible";
        log!((logger.vital) Wipe("To confirm with wiping your ENTIRE archive PERMANENTLY enter the phrase below (without quotes):") as Log);
        if_err!((logger) [Wipe, err => ("Entered phrase incorrect, please retry")] retry {
            log!((logger.vital) Wipe("\"{expected}\"") as Log);
            let input = logger.ask("Wipe", "Enter the phrase");
            if &input[0..input.len() - 1] != expected { Err(()) }
            else { Ok(()) }
        });

        log!((logger) Wipe("Wiping archive..."));

        let path = home_dir().join("archive");
        // Check if path exists
        if !path.exists() {
            log!((logger.vital) Wipe("Archive '{}' doesn't exist; doing nothing", path.to_string_lossy()) as Inconvenience);
            return;
        }

        // Wipe archive
        if_err!((logger) [Wipe, err => ("While wiping archive: {err:?}")] retry std::fs::remove_dir_all(&path));
        log!((logger.vital) Wipe("Successfully wiped archive! Run `diary-cli init` to init a new archive\n") as Log);
    }

    pub fn commit(&self, config: impl AsRef<Path>, mut logger: impl Logger) {
        let config = config.as_ref();
        let path = home_dir().join("archive");
        let path_string = path.to_string_lossy();

        // Checks if path exists or not
        if !path.is_dir() {
            log!((logger.error) Commit("Archive '{path_string}' doesn't exist! Run `diary-cli init` before you can commit") as Fatal);
            return logger.crash();
        }

        // Check if entry path exists or not
        let config_string = config.to_string_lossy();
        if !config.is_file() {
            log!((logger.error) Commit("Entry config file '{config_string}' doesn't exist") as Fatal);
            return logger.crash();
        }
        
        // Backup archive before modification
        let _ = std::fs::remove_file(home_dir().join("backup.ldb")); // Clean up
        Self::backup(home_dir().join("backup.ldb"), logger.hollow());

        // Parse toml
        log!((logger) Commit("Parsing toml at '{}'", config.to_string_lossy()));
        let entry = if_err!((logger) [Commit, err => ("While reading the entry config file: {err:?}")] retry std::fs::read_to_string(config));
        let entry = if_err!((logger) [Commit, err => ("While parsing entry config toml: {err:?}")] {entry.parse::<toml::Table>()} crash {
            log!((logger.error) Commit("{err:#?}") as Fatal);
            logger.crash()
        });

        
        // Checks if it is a moc
        let is_moc = entry.get("is-moc")
            .map(|x| unwrap_opt!((x.as_bool()) with logger, format: Commit("`is-moc` attribute of config file '{config_string}' must be boolean")))
            .unwrap_or(false);
        
        if is_moc {
            let container = if_err!((logger) [Commit, err => ("While loading archive as container: {err:?}")] retry search_database!((self.database) /mocs/));
            log!((logger) Commit("Detected that config file '{config_string}' is an moc (map of contents)"));
            MOC::new(entry, &config_string, container, logger.hollow());
        } else {
            let container = if_err!((logger) [Commit, err => ("While loading archive as container: {err:?}")] retry search_database!((self.database) /entries/));
            log!((logger) Commit("Detected that config file '{config_string}' is an entry"));
            
            // Add to unsorted list
            let entry = Entry::new(entry, &config_string, container, logger.hollow());
            log!((logger) Commit("Adding entry to unsorted stack..."));
            list::push(
                |file| LazyData::new_string(file, &entry.uid),
                &if_err!((logger) [Commit, err => ("While loaded unsorted stack: {err:?}")] retry search_database!((self.database) /order/unsorted)),
                logger.hollow(),
            );
        }

        // Update itver
        log!((logger) Commit("Updating archive itver..."));
        if_err!((logger) [Commit, err => ("While update archive itver: {err:?}")] retry write_database!((self.database) itver = new_u16(self.itver + 1)));

        log!((logger.vital) Commit("Successfully commited config to archive") as Log);
    }

    #[inline]
    pub fn database(&self) -> &LazyDB {
        &self.database
    }

    #[inline]
    pub fn database_exists(&self, path: impl AsRef<Path>) -> bool {
        self.database().path().join(path).exists()
    }

    pub fn get_entry(&self, uid: String, mut logger: impl Logger) -> Option<Entry> {
        if !self.database_exists(format!("entries/{uid}")) {
            log!((logger.error) Archive("Entry of uid `{uid}` does not exist") as Fatal);
            return logger.crash();
        }

        match search_database!((self.database) /entries/(&uid)) {
            Ok(x) => Some(Entry::load_lazy(uid, x)),
            Err(err) => match err {
                LDBError::DirNotFound(..) => None,
                _ => {
                    log!((logger.error) Archive("While getting entry '{uid}': {err:?}") as Fatal);
                    logger.crash()
                }
            }
        }
    }

    pub fn get_moc(&self, uid: String, mut logger: impl Logger) -> Option<MOC> {
        if !self.database_exists(format!("mocs/{uid}")) {
            log!((logger.error) Archive("Moc of uid `{uid}` does not exist") as Fatal);
            return logger.crash();
        }

        match search_database!((self.database) /mocs/(&uid)) {
            Ok(x) => Some(MOC::load_lazy(uid, x)),
            Err(err) => match err {
                LDBError::DirNotFound(..) => None,
                _ => {
                    log!((logger.error) Archive("While getting moc '{uid}': {err:?}") as Fatal);
                    logger.crash()
                }
            }
        }
    }

    pub fn list_entries(&self, mut logger: impl Logger) -> Vec<Entry> {
        let path = self.database.path().join("entries");

        if !path.is_dir() {
            log!((logger.vital) Entries("Path '{}' does not exist; doing nothing", path.to_string_lossy()) as Inconvenience);
            return Vec::with_capacity(0);
        }

        let mut logger1 = logger.hollow();
        let logger2 = logger.hollow();
        let dir = if_err!((logger) [Entries, err => ("While reading directory {}'s contents: {err:?}", path.to_string_lossy())] retry fs::read_dir(&path));
        dir.into_iter()
            .map(|x| if_err!((logger) [Entries, err => ("While reading dir element: {err:?}")] {x} crash logger.crash()))
            .filter(|x| if_err!((logger1) [Entries, err => ("While reading dir element: {err:?}")] {x.file_type()} crash logger1.crash()).is_dir())
            .map(|x| self.get_entry(x.file_name().to_string_lossy().to_string(), logger2.hollow()).unwrap())
            .collect()
    }

    pub fn list_mocs(&self, mut logger: impl Logger) -> Vec<MOC> {
        let path = self.database.path().join("mocs");

        if !path.is_dir() {
            log!((logger.vital) MOCs("Path '{}' does not exist; doing nothing", path.to_string_lossy()) as Inconvenience);
            return Vec::with_capacity(0);
        }

        let mut logger1 = logger.hollow();
        let logger2 = logger.hollow();
        let dir = if_err!((logger) [MOCs, err => ("While reading directory {}'s contents: {err:?}", path.to_string_lossy())] retry fs::read_dir(&path));
        dir.into_iter()
            .map(|x| if_err!((logger) [MOCs, err => ("While reading dir element: {err:?}")] {x} crash logger.crash()))
            .filter(|x| if_err!((logger1) [MOCs, err => ("While reading dir element: {err:?}")] {x.file_type()} crash logger1.crash()).is_dir())
            .map(|x| self.get_moc(x.file_name().to_string_lossy().to_string(), logger2.hollow()).unwrap())
            .collect()
    }
}