<!-- [DEF:ConnectionList:Component] -->
<!--
@SEMANTICS: connection, list, settings
@PURPOSE: UI component for listing and deleting saved database connection configurations.
@LAYER: UI
@RELATION: USES -> frontend/src/services/connectionService.js
-->
<script>
    // [SECTION: IMPORTS]
    import { onMount } from 'svelte';
    import { getConnections, deleteConnection } from '../../services/connectionService.js';
    import { addToast } from '../../lib/toasts.js';
    import { t } from '../../lib/i18n';
    import { Button, Card } from '../../lib/ui';
    // [/SECTION]

    let connections = $state([]);
    let isLoading = $state(true);

    // [DEF:fetchConnections:Function]
    // @PURPOSE: Fetches the list of connections from the backend.
    // @PRE: None.
    // @POST: connections array is populated.
    async function fetchConnections() {
        isLoading = true;
        try {
            connections = await getConnections();
        } catch (e) {
            addToast($t.connections?.fetch_failed, 'error');
        } finally {
            isLoading = false;
        }
    }
    // [/DEF:fetchConnections:Function]

    // [DEF:handleDelete:Function]
    // @PURPOSE: Deletes a connection configuration.
    // @PRE: id is provided and user confirms deletion.
    // @POST: Connection is deleted from backend and list is reloaded.
    async function handleDelete(id) {
        if (!confirm($t.connections?.delete_confirm)) return;
        
        try {
            await deleteConnection(id);
            addToast($t.connections?.deleted_success, 'success');
            await fetchConnections();
        } catch (e) {
            addToast(e.message, 'error');
        }
    }
    // [/DEF:handleDelete:Function]

    onMount(fetchConnections);

    // Expose fetchConnections to parent
    export { fetchConnections };
</script>

<!-- [SECTION: TEMPLATE] -->
<Card title={$t.connections?.saved} padding="none">
    <ul class="divide-y divide-gray-100">
        {#if isLoading}
            <li class="p-6 text-center text-gray-500">{$t.common.loading}</li>
        {:else if connections.length === 0}
            <li class="p-12 text-center text-gray-500 italic">{$t.connections?.no_saved}</li>
        {:else}
            {#each connections as conn}
                <li class="p-6 flex items-center justify-between hover:bg-gray-50 transition-colors">
                    <div>
                        <div class="text-sm font-medium text-blue-600 truncate">{conn.name}</div>
                        <div class="text-xs text-gray-400 mt-1 font-mono">{conn.type}://{conn.username}@{conn.host}:{conn.port}/{conn.database}</div>
                    </div>
                    <Button
                        variant="danger"
                        size="sm"
                        onclick={() => handleDelete(conn.id)}
                    >
                        {$t.connections?.delete}
                    </Button>
                </li>
            {/each}
        {/if}
    </ul>
</Card>
<!-- [/SECTION] -->
<!-- [/DEF:ConnectionList:Component] -->
