<script lang="ts">
    import { onMount } from 'svelte';

    import { api } from '$lib/api/api';
    import { ae_loc, ae_sess, ae_api, slct, slct_trigger } from '$lib/stores/ae_stores';

    type key_val = {
        [key: string]: any;
    };

    let ae_account_obj_get_promise;
    let ae_sponsorship_obj_li_get_promise;
    let v3_test_result: any = $state(null);

    onMount(() => {
        console.log('Testing: +page.svelte');
        let url = window.location.href;
        console.log(url);
    });

    async function test_v3_get_id() {
        console.log('*** test_v3_get_id() ***');
        v3_test_result = 'loading...';

        // Test standard V3 GET ID
        const result = await api.get_ae_obj_v3({
            api_cfg: $ae_api,
            obj_type: 'event',
            obj_id: '9dv-IV-iz-LY', // Use a known ID
            view: 'base',
            log_lvl: 1
        });

        v3_test_result = result;
        console.log('V3 GET ID Result:', result);
    }

    async function test_v3_get_nested_id() {
        console.log('*** test_v3_get_nested_id() ***');
        v3_test_result = 'loading...';

        // Test nested V3 GET ID
        const result = await api.get_nested_ae_obj_v3({
            api_cfg: $ae_api,
            parent_type: 'event',
            parent_id: '9dv-IV-iz-LY',
            child_type: 'event_badge',
            child_id: 'XHTX-23-20-42', // Use a known ID
            view: 'base',
            log_lvl: 1
        });

        v3_test_result = result;
        console.log('V3 GET Nested ID Result:', result);
    }

    async function test_v3_create_nested() {
        console.log('*** test_v3_create_nested() ***');
        v3_test_result = 'loading...';

        // Test creating a journal entry under the test journal
        const result = await api.create_nested_obj_v3({
            api_cfg: $ae_api,
            parent_type: 'journal',
            parent_id: 'JGEB-80-92-50',
            child_type: 'journal_entry',
            fields: {
                account_id_random: 'nqOzejLCDXM',
                name: 'Test V3 Nested Create',
                content: 'This was created using the new V3 nested create wrapper!',
                enable: true
            },
            log_lvl: 1
        });

        v3_test_result = result;
        console.log('V3 Create Nested Result:', result);
    }

    async function test_v3_update_nested() {
        console.log('*** test_v3_update_nested() ***');
        v3_test_result = 'loading...';

        // Test updating the journal entry we just created
        // ID is from the previous step result: nKiyj0JV5CY
        const result = await api.update_nested_obj_v3({
            api_cfg: $ae_api,
            parent_type: 'journal',
            parent_id: 'JGEB-80-92-50',
            child_type: 'journal_entry',
            child_id: 'nKiyj0JV5CY',
            fields: {
                name: 'Test V3 Nested Update - UPDATED',
                content: 'This was UPDATED using the new V3 nested update wrapper!'
            },
            log_lvl: 1
        });

        v3_test_result = result;
        console.log('V3 Update Nested Result:', result);
    }

    async function test_v3_delete_nested() {
        console.log('*** test_v3_delete_nested() ***');
        v3_test_result = 'loading...';

        // Test soft deleting (disabling) the journal entry we just updated
        const result = await api.delete_nested_ae_obj_v3({
            api_cfg: $ae_api,
            parent_type: 'journal',
            parent_id: 'JGEB-80-92-50',
            child_type: 'journal_entry',
            child_id: 'nKiyj0JV5CY',
            method: 'disable', // Sets enable = false
            log_lvl: 1
        });

        v3_test_result = result;
        console.log('V3 Delete (Disable) Nested Result:', result);
    }

    import { journals_func } from '$lib/ae_journals/ae_journals_functions';

    async function test_journal_module_soft_delete() {
        console.log('*** test_journal_module_soft_delete() ***');
        v3_test_result = 'loading...';

        // 1. Create a fresh entry first
        const new_entry = await journals_func.create_ae_obj__journal_entry({
            api_cfg: $ae_api,
            journal_id: 'JGEB-80-92-50',
            data_kv: {
                name: 'Soft Delete Test Entry',
                content: 'Testing disable and hide methods...',
                account_id_random: 'nqOzejLCDXM'
            },
            log_lvl: 1
        });

        if (!new_entry) {
            v3_test_result = 'Failed to create test entry';
            return;
        }

        const entry_id = new_entry.journal_entry_id_random;
        console.log('Created test entry:', entry_id);

        // 2. Test 'disable'
        console.log('Testing DISABLE...');
        await journals_func.delete_ae_obj_id__journal_entry({
            api_cfg: $ae_api,
            journal_entry_id: entry_id,
            method: 'disable',
            log_lvl: 1
        });

        // 3. Test 'hide'
        console.log('Testing HIDE...');
        const final_result = await journals_func.delete_ae_obj_id__journal_entry({
            api_cfg: $ae_api,
            journal_entry_id: entry_id,
            method: 'hide',
            log_lvl: 1
        });

        v3_test_result = {
            message: 'Soft delete (disable) and hide tests completed for entry ' + entry_id,
            last_result: final_result
        };
    }
</script>

<div class="container h-full mx-auto flex flex-col justify-center items-center p-4 gap-4">
    <div class="space-y-10 text-center flex flex-col items-center">
        <h1 class="h1">Aether - V3 API Testing</h1>

        <div class="flex justify-center flex-wrap gap-2">
            <button class="btn variant-filled-primary" onclick={test_v3_get_id}>
                Test V3 GET ID
            </button>
            <button class="btn variant-filled-secondary" onclick={test_v3_get_nested_id}>
                Test V3 GET Nested ID
            </button>
            <button class="btn variant-filled-tertiary" onclick={test_v3_create_nested}>
                Test V3 Create Nested
            </button>
            <button class="btn variant-filled-warning" onclick={test_v3_update_nested}>
                Test V3 Update Nested
            </button>
            <button class="btn variant-filled-error" onclick={test_v3_delete_nested}>
                Test V3 Delete (Disable) Nested
            </button>
            <button class="btn variant-filled-success" onclick={test_journal_module_soft_delete}>
                Test Journal Module Soft Deletes
            </button>
        </div>
    </div>

    {#if v3_test_result}
        <div class="card p-4 w-full max-w-2xl bg-surface-100-800-token">
            <h3 class="h3">Test Result:</h3>
            <pre class="text-xs text-left overflow-auto max-h-96">
                {JSON.stringify(v3_test_result, null, 2)}
            </pre>
        </div>
    {/if}
</div>

<style lang="postcss">
    /* figure {
	@apply flex relative flex-col;
}
figure svg,
.img-bg {
	@apply w-64 h-64 md:w-80 md:h-80;
}
.img-bg {
	@apply absolute z-[-1] rounded-full blur-[50px] transition-all;
	animation: pulse 5s cubic-bezier(0, 0, 0, 0.5) infinite,
		glow 5s linear infinite;
}
@keyframes glow {
	0% {
		@apply bg-primary-400/50;
	}
	33% {
		@apply bg-secondary-400/50;
	}
	66% {
		@apply bg-tertiary-400/50;
	}
	100% {
		@apply bg-primary-400/50;
	}
}
@keyframes pulse {
	50% {
		transform: scale(1.5);
	}
} */
</style>
