<script lang="ts">
import { ae_loc, ae_api, ae_sess } from '$lib/stores/ae_stores';
import { events_sess } from '$lib/stores/ae_events_stores';
import { launcher_loc } from '$lib/stores/ae_events_stores__launcher.svelte';
import * as native from '$lib/electron/electron_relay';
import Launcher_Cfg_Section from './launcher_cfg_section.svelte';
import { CloudDownload, LoaderCircle, Search, Wand2 } from '@lucide/svelte';
interface Props {
    on_expand?: () => void;
}
let { on_expand }: Props = $props();

let update_source: 'url' | 'file' = $state('file');
let update_path = $state(
    '~/OSIT/Speaker Ready System/Admin Share/Custom Applications/osit_binaries/'
);
let update_url = $state('https://dev-demo.oneskyit.com/updates/ae_native.zip');

let update_status = $state('');
let is_checking = $state(false);
let download_result = $state<any>(null);

async function handle_check_update() {
    is_checking = true;
    update_status = 'Checking for updates...';

    try {
        const args =
            update_source === 'url'
                ? { source: 'url' as const, url: update_url }
                : { source: 'file' as const, path: update_path };

        const res = await native.update_app(args);

        if (res.success) {
            download_result = res;
            update_status = 'Update located/downloaded. Ready to install.';
        } else {
            update_status = `Failed: ${res.error}`;
        }
    } catch (err: any) {
        update_status = `Error: ${err.message}`;
    } finally {
        is_checking = false;
    }
}

async function handle_install() {
    update_status = 'Initiating installation...';
    alert(
        'Installation logic is OS-specific. This will typically swap the application bundle and restart.'
    );
}
</script>

<Launcher_Cfg_Section
    title="Application Updates"
    icon={CloudDownload}
    bind:state={launcher_loc.current.section_state__updates}
    {on_expand}
    description="v1.0.0 | Source: {update_source}">
    <!-- Content omitted for brevity, preserved in file -->
    <div class="col-span-full flex flex-col gap-2">
        <!-- TECHNICAL: Source Config (Edit Mode Only) -->
        {#if $ae_loc.edit_mode}
            <div
                class="bg-surface-500/5 border-surface-500/10 mb-1 flex flex-col gap-2 rounded border p-2">
                <div class="flex flex-row items-center justify-between px-1">
                    <p class="text-[9px] font-bold uppercase opacity-50">
                        Source Type
                    </p>
                    <div class="flex gap-2">
                        <label class="flex items-center gap-1 text-[10px]">
                            <input
                                type="radio"
                                bind:group={update_source}
                                value="file"
                                class="radio radio-sm" /> Local
                        </label>
                        <label class="flex items-center gap-1 text-[10px]">
                            <input
                                type="radio"
                                bind:group={update_source}
                                value="url"
                                class="radio radio-sm" /> Web
                        </label>
                    </div>
                </div>

                {#if update_source === 'file'}
                    <input
                        type="text"
                        bind:value={update_path}
                        placeholder="Path to update package"
                        class="input input-sm preset-tonal-surface h-7 w-full text-[10px]" />
                {:else}
                    <input
                        type="text"
                        bind:value={update_url}
                        placeholder="URL to update package"
                        class="input input-sm preset-tonal-surface h-7 w-full text-[10px]" />
                {/if}
            </div>
        {/if}

        <!-- COMMON: Check Button -->
        <button
            type="button"
            onclick={handle_check_update}
            disabled={is_checking}
            class="btn btn-sm preset-filled-tertiary hover:preset-filled-primary-500 w-full text-[10px]">
            {#if is_checking}
                <LoaderCircle size="0.85em" class="mr-1 animate-spin" /> Checking...
            {:else}
                <Search size="0.85em" class="mr-1" /> Check for Updates
            {/if}
        </button>

        {#if update_status}
            <div
                class="border-surface-500/20 bg-surface-500/5 mt-1 rounded border p-1 text-center text-[9px] italic">
                {update_status}
            </div>
        {/if}

        {#if download_result}
            <button
                type="button"
                onclick={handle_install}
                class="btn btn-sm preset-filled-success hover:preset-filled-primary-500 mt-2 w-full animate-bounce text-[10px] shadow-lg">
                <Wand2 size="0.85em" class="mr-1" /> Install & Relaunch
            </button>
        {/if}
    </div>
</Launcher_Cfg_Section>
