<!-- [DEF:LoginPage:Component] -->
<!--
@TIER:      STANDARD
@SEMANTICS: login, auth, ui, form
@PURPOSE:   Provides the user interface for local and ADFS authentication.
@LAYER:     UI
@RELATION:  USES -> authStore
@RELATION:  CALLS -> api.auth.login

@INVARIANT: Shows both local login form and ADFS SSO button.
-->

<script lang="ts">
    import { onMount } from 'svelte';
    import { auth } from '../../lib/auth/store';
    import { api } from '../../lib/api';
    import { goto } from '$app/navigation';
    import { t } from '$lib/i18n';

    let username = '';
    let password = '';
    let error = '';
    let loading = false;

    // [DEF:handleLogin:Function]
    /**
     * @purpose Submits the local login form to the backend.
     * @pre     Username and password are not empty.
     * @post    User is authenticated and redirected on success.
     */
    async function handleLogin() {
        if (!username || !password) {
            error = $t.auth?.enter_credentials ;
            return;
        }

        loading = true;
        error = '';

        try {
            const formData = new URLSearchParams();
            formData.append('username', username);
            formData.append('password', password);

            const response = await fetch('/api/auth/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: formData
            });

            if (response.ok) {
                const data = await response.json();
                auth.setToken(data.access_token);
                
                // Fetch user profile
                try {
                    const user = await api.fetchApi('/auth/me');
                    auth.setUser(user);
                    goto('/');
                } catch (err) {
                    error = ($t.auth?.profile_fetch_failed )
                        .replace('{error}', err.message);
                }
            } else {
                const errData = await response.json();
                error = errData.detail || ($t.auth?.invalid_credentials );
            }
        } catch (e) {
            error = $t.auth?.login_error ;
            console.error(e);
        } finally {
            loading = false;
        }
    }
    // [/DEF:handleLogin:Function]

    // [DEF:handleADFSLogin:Function]
    /**
     * @purpose Redirects the user to the ADFS login endpoint.
     */
    function handleADFSLogin() {
        window.location.href = '/api/auth/login/adfs';
    }
    // [/DEF:handleADFSLogin:Function]

    onMount(() => {
        if ($auth.isAuthenticated) {
            goto('/');
        }
    });
</script>

<!-- [SECTION: TEMPLATE] -->
<div class="max-w-md mx-auto mt-10 p-6 bg-white rounded-lg shadow-md">
    <h2 class="text-2xl font-bold mb-6 text-center">{$t.auth?.login }</h2>

    {#if error}
        <div class="mb-4 p-3 bg-red-100 text-red-700 rounded border border-red-200">
            {error}
        </div>
    {/if}

    <form onsubmit={(event) => { event.preventDefault(); handleLogin(); }} class="space-y-4">
        <div>
            <label for="username" class="block text-sm font-medium text-gray-700">{$t.admin?.users?.username }</label>
            <input
                type="text"
                id="username"
                bind:value={username}
                class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                required
            />
        </div>

        <div>
            <label for="password" class="block text-sm font-medium text-gray-700">{$t.admin?.users?.password }</label>
            <input
                type="password"
                id="password"
                bind:value={password}
                class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
                required
            />
        </div>

        <button
            type="submit"
            disabled={loading}
            class="w-full py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
        >
            {loading ? ($t.auth?.logging_in ) : ($t.auth?.login )}
        </button>
    </form>

    <div class="mt-6">
        <div class="relative">
            <div class="absolute inset-0 flex items-center">
                <div class="w-full border-t border-gray-300"></div>
            </div>
            <div class="relative flex justify-center text-sm">
                <span class="px-2 bg-white text-gray-500">{$t.auth?.or_continue_with }</span>
            </div>
        </div>

        <div class="mt-6">
            <button
                onclick={handleADFSLogin}
                class="w-full flex justify-center py-2 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
            >
                {$t.auth?.corporate_sso }
            </button>
        </div>
    </div>
</div>
<!-- [/SECTION: TEMPLATE] -->


<!-- [/DEF:LoginPage:Component] -->
