@page "/admin/tax-profiles"
@using TaxBaik.Web.Services.AdminClients
@using TaxBaik.Web.Components.Admin.Shared
@inject ITaxProfileBrowserClient TaxProfileClient
@inject IClientBrowserClient ClientClient
@inject ISnackbar Snackbar
@inject IDialogService DialogService
@attribute [Authorize]

<PageTitle>세무 프로필</PageTitle>

<section class="admin-page-hero">
    <div>
        <MudText Typo="Typo.caption" Class="admin-eyebrow">CRM & 세무관리</MudText>
        <MudText Typo="Typo.h4" Class="admin-page-title">세무 프로필</MudText>
        <MudText Typo="Typo.body2" Class="admin-page-subtitle">고객별 세무 프로필, 신고 일정, 위험도 추적</MudText>
    </div>
    <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="PrepareCreate" StartIcon="@Icons.Material.Filled.Add" id="btn-add-profile">
        새 프로필 추가
    </MudButton>
</section>

<AdminDataPanel Loading="@(profiles == null)" SkeletonContent="@ProfileSkeleton">
@if (profiles == null)
{
}
else
{
    <MudGrid Spacing="2" Class="mt-2">
        <!-- Left: Dense Grid List -->
        <MudItem XS="12" MD="8">
            @if (profiles.Count == 0)
            {
                <MudAlert Severity="Severity.Info">세무 프로필이 없습니다.</MudAlert>
            }
            else
            {
                <MudDataGrid T="TaxProfile"
                    Items="@profiles"
                    Dense="true"
                    Hover="true"
                    Striped="true"
                    Virtualize="true"
                    RowsPerPage="30"
                    SelectedItem="@selectedProfile"
                    SelectedItemChanged="OnRowSelected"
                    Class="admin-grid">
                    <Columns>
                        <PropertyColumn Property="x => x.Id" Title="ID" Sortable="true" />
                        <TemplateColumn Title="고객">
                            <CellTemplate>
                                @if (clientMap.TryGetValue(context.Item.ClientId, out var clientName))
                                {
                                    @clientName
                                }
                            </CellTemplate>
                        </TemplateColumn>
                        <PropertyColumn Property="x => x.BusinessType" Title="사업 유형" />
                        <TemplateColumn Title="위험도">
                            <CellTemplate>
                                <MudChip Size="Size.Small" Color="@GetRiskColor(context.Item.TaxRiskLevel)" Variant="Variant.Filled">
                                    @context.Item.TaxRiskLevel
                                </MudChip>
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn Title="다음 신고">
                            <CellTemplate>
                                @if (context.Item.NextFilingDueDate.HasValue)
                                {
                                    @context.Item.NextFilingDueDate.Value.ToString("yyyy-MM-dd")
                                }
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn Title="작업" Sortable="false">
                            <CellTemplate>
                                <MudIconButton Icon="@Icons.Material.Filled.Delete" Color="Color.Error" Size="Size.Small" OnClick="@(async () => await DeleteProfile(context.Item.Id))" />
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                </MudDataGrid>
            }
        </MudItem>

        <!-- Right: Detail Form Panel (Inline Editor) -->
        <MudItem XS="12" MD="4">
            <MudPaper Class="pa-4 admin-surface admin-editor-panel" Elevation="0" Style="border: 1px solid var(--border-color); min-height: 400px;">
                <div class="d-flex align-center justify-space-between mb-4">
                    <MudText Typo="Typo.h6" Class="font-weight-bold">@(isEditMode ? "세무 프로필 수정" : "새 세무 프로필 추가")</MudText>
                    @if (isEditMode)
                    {
                        <MudButton Size="Size.Small" Variant="Variant.Outlined" Color="Color.Secondary" OnClick="PrepareCreate">
                            새로 작성
                        </MudButton>
                    }
                </div>
                <MudForm @ref="form">
                    <MudSelect T="int?" @bind-Value="profileForm.ClientId" Label="고객" Required="true" Variant="Variant.Outlined" FullWidth="@true" Class="mb-3" RequiredError="고객을 선택하세요." Disabled="@isEditMode">
                        @foreach (var client in clients)
                        {
                            <MudSelectItem Value="@((int?)client.Id)">@GetClientDisplayName(client)</MudSelectItem>
                        }
                    </MudSelect>
                    <CommonCodeSelect @bind-Value="profileForm.BusinessType" Group="BUSINESS_TYPE" Label="사업 유형" Class="mb-3" Required="true" />
                    <CommonCodeSelect @bind-Value="profileForm.TaxRiskLevel" Group="TAX_RISK_LEVEL" Label="위험도" Class="mb-3" />
                    <MudDatePicker @bind-Date="profileForm.NextFilingDueDate" Label="다음 신고 예정일" Variant="Variant.Outlined" FullWidth="@true" Class="mb-3" />
                    <MudTextField T="string" @bind-Value="profileForm.SpecialNotes" Label="특수 사항" Variant="Variant.Outlined" FullWidth="@true" Lines="3" Class="mb-4" />
                    
                    <div class="d-flex justify-end gap-2">
                        @if (isEditMode)
                        {
                            <MudButton Variant="Variant.Outlined" Color="Color.Error" OnClick="@(async () => await DeleteProfile(selectedProfile?.Id ?? 0))">삭제</MudButton>
                        }
                        <MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="SaveProfile" id="btn-save-profile">저장</MudButton>
                    </div>
                </MudForm>
            </MudPaper>
        </MudItem>
    </MudGrid>
}
</AdminDataPanel>

@code {
    [CascadingParameter]
    private Task<AuthenticationState>? AuthStateTask { get; set; }

    private List<TaxProfile>? profiles;
    private List<Client> clients = [];
    private Dictionary<int, string> clientMap = new();
    private List<CommonCode> riskLevels = [];
    private MudForm? form;
    private bool isEditMode;
    private TaxProfile? selectedProfile;
    private TaxProfileForm profileForm = new();

    private RenderFragment ProfileSkeleton => builder =>
    {
        builder.OpenComponent<AdminSkeletonRows>(0);
        builder.AddAttribute(1, "Rows", 6);
        builder.AddAttribute(2, "Columns", 4);
        builder.CloseComponent();
    };

    protected override async Task OnInitializedAsync()
    {
        if (AuthStateTask != null)
        {
            var authState = await AuthStateTask;
            if (authState.User.Identity?.IsAuthenticated == true)
            {
                await LoadData();
                PrepareCreate();
            }
        }
    }

    private async Task LoadData()
    {
        try
        {
            profiles = await TaxProfileClient.GetAllAsync();
            var (clientItems, _) = await ClientClient.GetPagedAsync(pageSize: 1000);
            clients = clientItems.ToList();
            clientMap = clients.ToDictionary(c => c.Id, GetClientDisplayName);

        }
        catch (Exception ex)
        {
            Snackbar.Add($"데이터 로드 실패: {ex.Message}", Severity.Error);
        }
    }

    private void PrepareCreate()
    {
        selectedProfile = null;
        isEditMode = false;
        profileForm = new TaxProfileForm
        {
            ClientId = clients.FirstOrDefault()?.Id,
            TaxRiskLevel = "normal",
            NextFilingDueDate = DateTime.Today.AddMonths(1)
        };
    }

    private void OnRowSelected(TaxProfile profile)
    {
        if (profile == null) return;
        selectedProfile = profile;
        isEditMode = true;
        profileForm = new TaxProfileForm
        {
            ClientId = profile.ClientId,
            BusinessType = profile.BusinessType ?? "",
            TaxRiskLevel = profile.TaxRiskLevel,
            NextFilingDueDate = profile.NextFilingDueDate,
            SpecialNotes = profile.SpecialNotes
        };
    }

    private async Task SaveProfile()
    {
        if (form != null)
        {
            await form.Validate();
            if (!form.IsValid)
            {
                Snackbar.Add("고객을 선택하세요.", Severity.Warning);
                return;
            }
        }

        try
        {
            if (isEditMode && selectedProfile != null)
            {
                await TaxProfileClient.UpdateAsync(selectedProfile.Id, profileForm.BusinessType,
                    null, profileForm.NextFilingDueDate, profileForm.TaxRiskLevel);
                Snackbar.Add("세무 프로필이 수정되었습니다.", Severity.Success);
            }
            else
            {
                if (!profileForm.ClientId.HasValue)
                {
                    Snackbar.Add("고객을 선택하세요.", Severity.Warning);
                    return;
                }
                var newId = await TaxProfileClient.CreateAsync(
                    profileForm.ClientId.Value,
                    profileForm.BusinessType);
                if (newId > 0)
                {
                    await TaxProfileClient.UpdateAsync(
                        newId,
                        profileForm.BusinessType,
                        null,
                        profileForm.NextFilingDueDate,
                        profileForm.TaxRiskLevel);
                    Snackbar.Add("세무 프로필이 추가되었습니다.", Severity.Success);
                }
            }
            PrepareCreate();
            await LoadData();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"저장 실패: {ex.Message}", Severity.Error);
        }
    }

    private async Task DeleteProfile(int id)
    {
        var parameters = new DialogParameters();
        parameters.Add("Title", "삭제 확인");
        parameters.Add("Message", "이 세무 프로필을 삭제하시겠습니까?");

        var dialog = await DialogService.ShowAsync<ConfirmDialog>("", parameters);
        var result = await dialog.Result;

        if (result?.Canceled ?? true)
            return;

        try
        {
            await TaxProfileClient.DeleteAsync(id);
            Snackbar.Add("세무 프로필이 삭제되었습니다.", Severity.Success);
            if (selectedProfile?.Id == id)
            {
                PrepareCreate();
            }
            await LoadData();
        }
        catch (Exception ex)
        {
            Snackbar.Add($"삭제 실패: {ex.Message}", Severity.Error);
        }
    }

    private Color GetRiskColor(string riskLevel) => riskLevel switch
    {
        "high" => Color.Error,
        "normal" => Color.Warning,
        "low" => Color.Success,
        _ => Color.Default
    };

    private static string GetClientDisplayName(Client client)
        => !string.IsNullOrWhiteSpace(client.CompanyName)
            ? client.CompanyName
            : !string.IsNullOrWhiteSpace(client.Name)
                ? client.Name
                : $"Client #{client.Id}";

    private class TaxProfileForm
    {
        public int? ClientId { get; set; }
        public string BusinessType { get; set; } = "";
        public string TaxRiskLevel { get; set; } = "normal";
        public DateTime? NextFilingDueDate { get; set; }
        public string? SpecialNotes { get; set; }
    }
}
