import { persisted } from 'svelte-persisted-store';

import { writable } from 'svelte/store';
import type { Writable } from 'svelte/store';

import type { key_val } from '$lib/stores/ae_stores';

/* *** BEGIN *** Initialize journals_local_data_struct */
// This is for longer term or sticky app data. This should be stored to *local* storage.
// Updated 2025-03-20
const journals_local_data_struct: key_val = {
    ver: '2024-08-20_19',
    // Shared
    name: 'Aether - Journals (SvelteKit 2.x Svelte 5.x)',
    title: `OSIT's Æ Journals`, // &AElig;

    mode__edit: false,
    mode__debug: false,

    datetime_format: 'datetime_12_long',
    time_format: 'time_12_short',
    time_hours: 12, // 12 or 24

    qry__journal_id: null,

    journal_view_history_li: [], // Appended each time the journal is loaded.
    entry_view_history_li: [], // NO LONGER USED: Appended each time the entry is loaded.
    entry_view_history_kv: {}, // Keyed by journal_entry_id for quick lookup.
    entry_view_history_max: 15, // Maximum number of journal entries to keep in history.

    llm__api_base_url: 'https://ai.dgrzone.com/api',
    llm__api_model: 'dgrzone-deepseek-8b-quick',
    llm__api_token:
        'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVhYjI2MzdlLThiMjktNGM2Zi05MzVhLWFkYjU1MDkwMGU5MCJ9.VObfR91GrX3j1vHbHZqGsOWEyrL981cbSWWjaXfYbUQ',
    llm__api_dangerous_browser: false, // This allows for use of localhost.
    llm__stream: false, // Whether to use streaming or not. Not all APIs support this.
    llm__timeout_ms: 60000, // 60 seconds
    llm__max_retries: 3, // Number of times to retry a failed LLM API call.
    llm__retry_delay_ms: 2000, // 2 seconds between retries.

    llm__system_prompt:
        'You are a helpful assistant that helps people find information.',
    llm__max_tokens: 1024,
    llm__temperature: 0.7,
    llm__top_p: 1.0,
    llm__n: 1,
    llm__frequency_penalty: 0.0,
    llm__presence_penalty: 0.0,

    journal: {
        edit: false,
        edit_kv: {},

        // Query / Search Settings
        qry__search_text: '',
        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 25,
        qry__offset: 0,
        qry__order_by_li: {
            // 'created_on': 'desc',
            // 'updated_on': 'desc',
        },

        type_code_li: [
            { code: 'diary', name: 'Diary' },
            { code: 'log', name: 'Log' },
            { code: 'journal', name: 'Journal' },
            { code: 'notebook', name: 'Notebook' },
            { code: 'personal', name: 'Personal' },
            { code: 'professional', name: 'Professional' },
            { code: 'tracking', name: 'Tracking' },
            { code: 'other', name: 'Other' },
            { code: 'test', name: 'Test' }
            // { code: 'notepad', name: 'Notepad' },
        ]
    },
    entry: {
        edit: false,
        edit_kv: {},

        // Query / Search Settings
        search_version: 0,
        qry__remote_first: false,
        qry__global_person_search: false,
        qry__search_text: '',
        qry__category_code: '', // For filtering
        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 50,
        qry__offset: 0,
        qry__order_by_li: {
            // 'created_on': 'desc',
            // 'updated_on': 'desc',
        },

        // This is effectively "last used journal for creating entries"
        qry__journal_id: null,

        llm__system_prompt:
            'Summarize the following journal entry content in a concise manner, focusing on key points and insights.',
        llm__max_tokens: 512,
        llm__temperature: 0.7,
        llm__top_p: 1.0,
        llm__n: 1,
        llm__frequency_penalty: 0.0,
        llm__presence_penalty: 0.0,

        auto_save: false
    }
};
// console.log(`AE Stores - App Journals Local Storage Data:`, journals_local_data_struct);

// This works and uses *local* storage:
export const journals_loc: Writable<key_val> = persisted(
    'ae_journals_loc',
    journals_local_data_struct
);
// console.log(`AE Stores - App Local Storage Data:`, get(ae_loc));

/* *** BEGIN *** Initialize journals_session_data_struct */
// Temporary app data. This is lost if the page is refreshed or using different tabs/windows. This should be stored to *session* storage.
// Updated 2025-03-20
const journals_session_data_struct: key_val = {
    ver: '2024-08-20_19',
    log_lvl: 1,

    // Shared Triggers
    trigger: null,
    trigger__journal_id: null,
    // trigger__journal_li: null,

    show__modal__journals_config: false,

    show__modal_edit__journal_obj: false,
    show__modal_new__journal_obj: false,
    show__modal_view__journal_id: null,
    show_list__journal_entry_li_group: true,
    show__modal_view__journal_entry_id: null,
    show__modal_edit__journal_entry_id: null,

    show__content__journal_entry_history: false,

    journal: {
        edit: false,
        edit_kv: {},

        new_journal_name: '',
        new_journal_type_code: '',

        tmp_obj: {}
    },
    entry: {
        show__ai_summary: false,
        ai_summary: '',
        decrypt_kv: {}, // Essentially flag that the entry (content and history) can be decrypted.
        edit: false,
        edit_kv: {},

        tmp_obj: {}
    },

    journal_kv: {
        // journal_id: {},
    }
};
// console.log(`AE Stores - App Journals Session Storage Data:`, journals_session_data_struct);
export const journals_sess = writable(journals_session_data_struct);

/* *** BEGIN *** Initialize journals_slct and journals_trig */
/* The slct and slct_trigger variable should not be stored in local storage. Only use session storage because browser tabs can be open to different journals, badges, exhibits, etc. */

// Intended for temporary session storage.
// Updated 2024-08-20
const journals_slct_obj_template: key_val = {
    // Top level
    journal_id: null,
    journal_obj: {},
    journal_obj_li: [],

    tmp_journal_obj: {}, // Temporary object for new journal
    tmp_journal_entry_obj: {}, // Temporary object for new journal entry

    lq__journal_obj: {} // Testing passing a LiveQuery object around...
};
// console.log(`AE Stores - Selected Journals Objects:`, journals_slct_obj_template);

// This works, and uses *session* (not local) storage:
export const journals_slct = writable(journals_slct_obj_template);

/* *** BEGIN *** Initialize journals_trig */
// Intended for temporary session storage.
// Updated 2025-03-16
const journals_trig_template: key_val = {
    journal_id: false,
    journal_entry_li: false
};
export const journals_trig: any = writable(journals_trig_template);
// console.log(`AE Journals Stores - Journals Trigger:`, journals_trig);

/* *** BEGIN *** Initialize journals_prom */
// Intended for temporary session storage.
// Updated 2025-03-16
const journals_prom_template: key_val = {
    journal_id: false,
    journal_entry_li: false
};
export const journals_prom: any = writable(journals_prom_template);
// console.log(`AE Journals Stores - Journals Trigger:`, journals_prom);
