<!-- #region PasswordPrompt [C:3] [TYPE Component] [SEMANTICS password, prompt, modal, security, input] -->
<!-- @BRIEF Component component: components/PasswordPrompt.svelte -->
<!-- @LAYER UI -->
<!--
@SEMANTICS: password, prompt, modal, input, security
@PURPOSE: A modal component to prompt the user for database passwords when a migration task is paused.
@LAYER UI
@RELATION USES -> [ApiModule]
@RELATION BINDS_TO -> [EXT:frontend:onresume]
@RELATION BINDS_TO -> [EXT:frontend:oncancel]
-->
<script lang="ts">
    let {
        show = false,
        databases = [],
        errorMessage = "",
        onresume = () => {},
        oncancel = () => {},
    } = $props();

    let passwords = $state({});
    let submitting = $state(false);

    // #region handleSubmit:Function [TYPE Function]
    // @PURPOSE: Validates and forwards passwords to resume the task.
    // @PRE: All database passwords must be entered.
    // @POST: Parent resume callback receives passwords payload.
    function handleSubmit() {
        if (submitting) return;

        // Validate all passwords entered
        const missing = databases.filter((db) => !passwords[db]);
        if (missing.length > 0) {
            alert(`Please enter passwords for: ${missing.join(", ")}`);
            return;
        }

        submitting = true;
        onresume({ passwords });
        // Reset submitting state is handled by parent or on close
    }
    // #endregion handleSubmit:Function

    // #region handleCancel:Function [TYPE Function]
    // @PURPOSE: Cancels the password prompt.
    // @PRE: Modal is open.
    // @POST: Parent cancel callback is invoked and show is set to false.
    function handleCancel() {
        oncancel();
        show = false;
    }
    // #endregion handleCancel:Function

    // Reset passwords when modal opens/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-gray-500 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-white 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-white 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-red-100 sm:mx-0 sm:h-10 sm:w-10"
                        >
                            <!-- Heroicon name: outline/lock-closed -->
                            <svg
                                class="h-6 w-6 text-red-600"
                                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-gray-900"
                                id="modal-title"
                            >
                                Database Password Required
                            </h3>
                            <div class="mt-2">
                                <p class="text-sm text-gray-500 mb-4">
                                    The migration process requires passwords for
                                    the following databases to proceed.
                                </p>

                                {#if errorMessage}
                                    <div
                                        class="mb-4 p-2 bg-destructive-light text-red-700 text-xs rounded border border-destructive-light"
                                    >
                                        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-gray-700"
                                            >
                                                Password for {dbName}
                                            </label>
                                            <input
                                                type="password"
                                                id="password-{dbName}"
                                                bind:value={passwords[dbName]}
                                                class="mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm p-2 border"
                                                placeholder="Enter password"
                                                required
                                            />
                                        </div>
                                    {/each}
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div
                    class="bg-gray-50 px-4 py-3 sm:px-6 sm:flex sm:flex-row-reverse"
                >
                    <button
                        type="button"
                        class="w-full inline-flex justify-center rounded-md border border-transparent shadow-sm px-4 py-2 bg-indigo-600 text-base font-medium text-white hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:ml-3 sm:w-auto sm:text-sm disabled:opacity-50"
                        onclick={handleSubmit}
                        disabled={submitting}
                    >
                        {submitting ? "Resuming..." : "Resume Migration"}
                    </button>
                    <button
                        type="button"
                        class="mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 sm:mt-0 sm:ml-3 sm:w-auto sm:text-sm"
                        onclick={handleCancel}
                        disabled={submitting}
                    >
                        Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
{/if}
<!-- #endregion PasswordPrompt -->
