<!-- [DEF:PasswordPrompt:Component] -->
<!--
@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 -> frontend/src/lib/api.js (inferred)
@RELATION: EMITS -> resume, cancel
-->
<script>
    import { createEventDispatcher } from 'svelte';
    
    export let show = false;
    export let databases = []; // List of database names requiring passwords
    export let errorMessage = "";
    
    const dispatch = createEventDispatcher();
    
    let passwords = {};
    let submitting = false;
    
    // [DEF:handleSubmit:Function]
    // @PURPOSE: Validates and dispatches the passwords to resume the task.
    // @PRE: All database passwords must be entered.
    // @POST: 'resume' event is dispatched with passwords.
    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;
        dispatch('resume', { passwords });
        // Reset submitting state is handled by parent or on close
    }
    // [/DEF:handleSubmit:Function]
    
    // [DEF:handleCancel:Function]
    // @PURPOSE: Cancels the password prompt.
    // @PRE: Modal is open.
    // @POST: 'cancel' event is dispatched and show is set to false.
    function handleCancel() {
        dispatch('cancel');
        show = false;
    }
    // [/DEF:handleCancel:Function]
    
    // Reset passwords when modal opens/closes
    $: 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" on:click={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-red-50 text-red-700 text-xs rounded border border-red-200">
                                        Error: {errorMessage}
                                    </div>
                                {/if}

                                <form on:submit|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"
                        on:click={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"
                        on:click={handleCancel}
                        disabled={submitting}
                    >
                        Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
{/if}
<!-- [/DEF:PasswordPrompt:Component] -->