@page "/admin/inquiries/create"
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
@attribute [Authorize]
@using TaxBaik.Application.DTOs
@using TaxBaik.WasmClient.Components.Admin.Forms
@inject IInquiryBrowserClient InquiryClient
@inject NavigationManager Navigation
@inject ISnackbar Snackbar

<PageTitle>문의 등록</PageTitle>

<AdminCrudPageShell Title="새 문의 등록"
                    Eyebrow="Customer Relations"
                    Subtitle="고객 문의를 등록합니다. (전화, 오프라인 등)"
                    Loading="@false"
                    OnCancel="@GoBack">
    <MudPaper Class="pa-4 mt-4" Elevation="1">
        <InquiryForm ButtonText="등록" OnSubmit="HandleCreate" OnCancel="GoBack" />
    </MudPaper>
</AdminCrudPageShell>

@code {
    private void GoBack()
    {
        Navigation.NavigateTo("./inquiries");
    }

    private async Task HandleCreate(InquiryFormModel model)
    {
        try
        {
            // ViewModel validation is the first line; DTO validation remains the contract boundary.
            var result = await InquiryClient.CreateAsync(new SubmitInquiryDto
            {
                Name = model.Name,
                Phone = model.Phone,
                Email = model.Email,
                ServiceType = model.ServiceType,
                Message = model.Message,
                SuppressNotification = false
            });

            if (result == null)
            {
                Snackbar.Add("문의가 등록되지 않았습니다.", Severity.Error);
                return;
            }

            Snackbar.Add("✅ 문의가 등록되었고 고객에게 알림이 발송되었습니다.", Severity.Success);
            // 사용자가 성공 메시지를 볼 수 있도록 3초 후에 목록으로 이동
            await Task.Delay(3000);
            Navigation.NavigateTo("./inquiries");
        }
        catch (ValidationException ex)
        {
            Snackbar.Add(ex.Message, Severity.Error);
        }
        catch (Exception ex)
        {
            Snackbar.Add($"등록 실패: {ex.Message}", Severity.Error);
        }
    }
}
