@using Microsoft.Extensions.Localization
@using AliasVault.Client.Auth.Components

<FormModal
    IsOpen="@IsOpen"
    Title="@Title"
    MaxWidth="sm"
    ConfirmText="@Localizer["ConfirmButton"]"
    CancelText="@SharedLocalizer["Cancel"]"
    ConfirmDisabled="@(string.IsNullOrEmpty(_password))"
    IsLoading="false"
    OnConfirm="@HandleConfirm"
    OnClose="@HandleClose"
    SubmitOnEnter="true">
    <Icon>
        <svg class="h-6 w-6 text-orange-600 dark:text-orange-400" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
            <path stroke-linecap="round" stroke-linejoin="round" d="M16.5 10.5V6.75a4.5 4.5 0 10-9 0v3.75m-.75 11.25h10.5a2.25 2.25 0 002.25-2.25v-6.75a2.25 2.25 0 00-2.25-2.25H6.75a2.25 2.25 0 00-2.25 2.25v6.75a2.25 2.25 0 002.25 2.25z" />
        </svg>
    </Icon>
    <ChildContent>
        <p class="text-sm text-gray-500 dark:text-gray-400 mb-4">
            @Description
        </p>

        @if (!string.IsNullOrEmpty(ErrorMessage))
        {
            <AlertMessageError Message="@ErrorMessage" HasTopMargin="false" />
        }

        <div class="mt-4">
            <label for="password-confirm" class="block text-sm font-medium text-gray-900 dark:text-white mb-2">
                @SharedLocalizer["Password"]
            </label>
            <PasswordInputField
                Id="password-confirm"
                @bind-Value="_password"
                Placeholder="@Localizer["EnterPasswordPlaceholder"]" />
        </div>
    </ChildContent>
</FormModal>

@code {
    [Inject]
    private IStringLocalizerFactory LocalizerFactory { get; set; } = default!;

    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Main.Shared.PasswordConfirmationModal", "AliasVault.Client");
    private IStringLocalizer SharedLocalizer => LocalizerFactory.Create("SharedResources", "AliasVault.Client");

    /// <summary>
    /// Gets or sets whether the modal is open.
    /// </summary>
    [Parameter]
    public bool IsOpen { get; set; }

    /// <summary>
    /// Gets or sets the modal title.
    /// </summary>
    [Parameter]
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// Gets or sets the description text shown in the modal.
    /// </summary>
    [Parameter]
    public string Description { get; set; } = string.Empty;

    /// <summary>
    /// Gets or sets the error message to display.
    /// </summary>
    [Parameter]
    public string ErrorMessage { get; set; } = string.Empty;

    /// <summary>
    /// Gets or sets the callback when password is submitted. Returns the entered password.
    /// </summary>
    [Parameter]
    public EventCallback<string> OnPasswordSubmitted { get; set; }

    /// <summary>
    /// Gets or sets the callback when modal is closed.
    /// </summary>
    [Parameter]
    public EventCallback OnClose { get; set; }

    private string _password = string.Empty;

    /// <inheritdoc />
    protected override void OnParametersSet()
    {
        // Reset password when modal is opened
        if (IsOpen && string.IsNullOrEmpty(_password))
        {
            _password = string.Empty;
        }
    }

    /// <summary>
    /// Handles the confirm button click - submits the password to the parent.
    /// </summary>
    private async Task HandleConfirm()
    {
        if (string.IsNullOrEmpty(_password))
        {
            return;
        }

        // Submit password to parent for verification
        await OnPasswordSubmitted.InvokeAsync(_password);
        _password = string.Empty;
    }

    /// <summary>
    /// Handles the modal close event.
    /// </summary>
    private async Task HandleClose()
    {
        _password = string.Empty;
        await OnClose.InvokeAsync();
    }
}
