<!-- [DEF:ConnectionForm:Component] -->
<!--
@SEMANTICS: connection, form, settings
@PURPOSE: UI component for creating a new database connection configuration.
@LAYER: UI
@RELATION: USES -> frontend/src/services/connectionService.js
-->
<script>
    // [SECTION: IMPORTS]
    import { createEventDispatcher } from 'svelte';
    import { createConnection } from '../../services/connectionService.js';
    import { addToast } from '../../lib/toasts.js';
    // [/SECTION]

    const dispatch = createEventDispatcher();

    let name = '';
    let type = 'postgres';
    let host = '';
    let port = 5432;
    let database = '';
    let username = '';
    let password = '';
    let isSubmitting = false;

    // [DEF:handleSubmit:Function]
    // @PURPOSE: Submits the connection form to the backend.
    // @PRE: All required fields (name, host, database, username, password) must be filled.
    // @POST: A new connection is created via the connection service and a success event is dispatched.
    async function handleSubmit() {
        if (!name || !host || !database || !username || !password) {
            addToast('Please fill in all required fields', 'warning');
            return;
        }

        isSubmitting = true;
        try {
            const newConnection = await createConnection({
                name, type, host, port, database, username, password
            });
            addToast('Connection created successfully', 'success');
            dispatch('success', newConnection);
            resetForm();
        } catch (e) {
            addToast(e.message, 'error');
        } finally {
            isSubmitting = false;
        }
    }
    // [/DEF:handleSubmit:Function]

    // [DEF:resetForm:Function]
    /* @PURPOSE: Resets the connection form fields to their default values.
       @PRE: None.
       @POST: All form input variables are reset.
    */
    function resetForm() {
        name = '';
        host = '';
        port = 5432;
        database = '';
        username = '';
        password = '';
    }
    // [/DEF:resetForm:Function]
</script>

<!-- [SECTION: TEMPLATE] -->
<div class="bg-white p-6 rounded-lg shadow-sm border border-gray-200">
    <h3 class="text-lg font-medium text-gray-900 mb-4">Add New Connection</h3>
    <form on:submit|preventDefault={handleSubmit} class="space-y-4">
        <div>
            <label for="conn-name" class="block text-sm font-medium text-gray-700">Connection Name</label>
            <input type="text" id="conn-name" bind:value={name} placeholder="e.g. Production DWH" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
        </div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label for="conn-host" class="block text-sm font-medium text-gray-700">Host</label>
                <input type="text" id="conn-host" bind:value={host} placeholder="10.0.0.1" class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
            </div>
            <div>
                <label for="conn-port" class="block text-sm font-medium text-gray-700">Port</label>
                <input type="number" id="conn-port" bind:value={port} class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
            </div>
        </div>
        <div>
            <label for="conn-db" class="block text-sm font-medium text-gray-700">Database Name</label>
            <input type="text" id="conn-db" bind:value={database} class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
        </div>
        <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
            <div>
                <label for="conn-user" class="block text-sm font-medium text-gray-700">Username</label>
                <input type="text" id="conn-user" bind:value={username} class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
            </div>
            <div>
                <label for="conn-pass" class="block text-sm font-medium text-gray-700">Password</label>
                <input type="password" id="conn-pass" bind:value={password} class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" />
            </div>
        </div>
        <div class="flex justify-end pt-2">
            <button type="submit" disabled={isSubmitting} class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50">
                {isSubmitting ? 'Creating...' : 'Create Connection'}
            </button>
        </div>
    </form>
</div>
<!-- [/SECTION] -->
<!-- [/DEF:ConnectionForm:Component] -->