<!-- #region PasswordPrompt [C:3] [TYPE Component] [SEMANTICS password, prompt, modal, security, input] -->
<!-- @ingroup Components -->
<!-- @BRIEF Modal prompt for database passwords when a migration task is paused awaiting credentials. -->
<!-- @LAYER UI -->
<!-- @RELATION BINDS_TO -> [EXT:frontend:onresume] -->
<!-- @RELATION BINDS_TO -> [EXT:frontend:oncancel] -->
<!-- @UX_STATE: Hidden -> Modal is closed, password state is reset. -->
<!-- @UX_STATE: Visible -> Modal open, password fields rendered for each database. -->
<!-- @UX_STATE: Submitting -> Submit button disabled, spinner/text indicates submission. -->
<!-- @UX_STATE: Error -> Error message banner displayed above form fields. -->
<!-- @UX_FEEDBACK Error banner on submission failure. -->
<!-- @UX_FEEDBACK Submit button disabled during submission. -->
<!-- @UX_RECOVERY Close modal or correct passwords and re-submit. -->
<script lang="ts">
    import { t } from '$lib/i18n/index.svelte.js';
    import { Button } from '$lib/ui';

    let {
        show = false,
        databases = [],
        errorMessage = "",
        onresume = () => {},
        oncancel = () => {},
    }: {
        show?: boolean;
        databases?: string[];
        errorMessage?: string;
        onresume?: (_payload: { passwords: Record<string, string> }) => void;
        oncancel?: () => void;
    } = $props();

    let passwords = $state<Record<string, string>>({});
    let submitting = $state(false);

    // #region handleSubmit [C:2] [TYPE Function] [SEMANTICS form,validation]
// @ingroup Components
    // @BRIEF Validates all passwords and calls parent onresume callback.
    // @PRE All database passwords must be entered; submitting flag is false.
    // @POST Parent onresume callback receives passwords payload; submitting remains true until parent resets.
    function handleSubmit() {
        if (submitting) return;

        const missing = databases.filter((db) => !passwords[db]);
        if (missing.length > 0) {
            alert($t.migration?.password_required || "Please enter passwords for: " + missing.join(", "));
            return;
        }

        submitting = true;
        onresume({ passwords });
    }
    // #endregion handleSubmit

    // #region handleCancel [C:1] [TYPE Function]
    // @BRIEF Closes modal without resuming.
    // @PRE Modal is open.
    // @POST Parent oncancel callback is invoked and show is set to false.
    function handleCancel() {
        oncancel();
        show = false;
    }
    // #endregion handleCancel

    // Reset passwords when modal closes
    $effect(() => {
        if (!show) {
            passwords = {};
            submitting = false;
        }
    });
</script>

{#if show}
    <div
        class="fixed inset-0 z-50 overflow-y-auto"
        aria-labelledby="modal-title"
        role="dialog"
        aria-modal="true"
    >
        <div class="flex items-end justify-center min-h-screen pt-4 px-4 pb-20 text-center sm:block sm:p-0">
            <!-- Background overlay -->
            <div
                class="fixed inset-0 bg-surface-muted bg-opacity-75 transition-opacity"
                aria-hidden="true"
                onclick={handleCancel}
            ></div>

            <span class="hidden sm:inline-block sm:align-middle sm:h-screen" aria-hidden="true">&#8203;</span>

            <div class="inline-block align-bottom bg-surface-card rounded-lg text-left overflow-hidden shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-lg sm:w-full">
                <div class="bg-surface-card px-4 pt-5 pb-4 sm:p-6 sm:pb-4">
                    <div class="sm:flex sm:items-start">
                        <div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-destructive-light sm:mx-0 sm:h-10 sm:w-10">
                            <svg
                                class="h-6 w-6 text-destructive"
                                xmlns="http://www.w3.org/2000/svg"
                                fill="none"
                                viewBox="0 0 24 24"
                                stroke="currentColor"
                                aria-hidden="true"
                            >
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
                            </svg>
                        </div>
                        <div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left w-full">
                            <h3 class="text-lg leading-6 font-medium text-text" id="modal-title">
                                {$t.migration?.db_password_title || "Database Password Required"}
                            </h3>
                            <div class="mt-2">
                                <p class="text-sm text-text-muted mb-4">
                                    {$t.migration?.db_password_description || "The migration process requires passwords for the following databases to proceed."}
                                </p>

                                {#if errorMessage}
                                    <div class="mb-4 p-2 bg-destructive-light text-destructive text-xs rounded border border-destructive-light">
                                        {$t.migration?.error_prefix || "Error:"} {errorMessage}
                                    </div>
                                {/if}

                                <form onsubmit={(e) => { e.preventDefault(); handleSubmit(); }} class="space-y-4">
                                    {#each databases as dbName}
                                        <div>
                                            <label for="password-{dbName}" class="block text-sm font-medium text-text">
                                                {$t.migration?.password_for || "Password for"} {dbName}
                                            </label>
                                            <input
                                                type="password"
                                                id="password-{dbName}"
                                                bind:value={passwords[dbName]}
                                                class="mt-1 block w-full border-border-strong rounded-md shadow-sm focus:ring-primary-ring focus:border-primary-ring sm:text-sm p-2 border"
                                                placeholder={$t.migration?.enter_password || "Enter password"}
                                                required
                                            />
                                        </div>
                                    {/each}
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="bg-surface-muted px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse">
                    <Button variant="primary" onclick={handleSubmit} disabled={submitting}>
                        {submitting
                            ? ($t.migration?.resuming || "Resuming...")
                            : ($t.migration?.resume || "Resume Migration")}
                    </Button>
                    <Button variant="secondary" onclick={handleCancel} disabled={submitting}>
                        {$t.actions?.cancel || "Cancel"}
                    </Button>
                </div>
            </div>
        </div>
    </div>
{/if}
<!-- #endregion PasswordPrompt -->
