@page "/admin/clients"
@attribute [Authorize]
@using TaxBaik.Web.Services
@using TaxBaik.Domain.Entities
@inject IClientBrowserClient ClientClient
@inject NavigationManager Navigation
@inject IDialogService DialogService
@inject ISnackbar Snackbar

<PageTitle>고객 관리</PageTitle>

<AdminPageHeader Title="고객 관리" Eyebrow="CRM" Subtitle="고객 카드를 등록하고 상담 이력을 관리합니다.">
    <ChildContent>
        <MudButton Variant="Variant.Filled" Color="Color.Primary"
                   StartIcon="@Icons.Material.Filled.PersonAdd"
                   Href="/taxbaik/admin/clients/create">
            고객 등록
        </MudButton>
    </ChildContent>
</AdminPageHeader>

@* 검색/필터 바 *@
<MudPaper Class="admin-surface mb-3 pa-3" Elevation="0">
    <MudGrid>
        <MudItem xs="12" md="5">
            <MudTextField @bind-Value="searchText" Label="검색 (이름·연락처·회사명)"
                          Adornment="Adornment.End" AdornmentIcon="@Icons.Material.Filled.Search"
                          Immediate="false" OnKeyUp="@OnSearchKeyUp" />
        </MudItem>
        <MudItem xs="12" md="3">
            <CommonCodeSelect @bind-Value="statusFilter" Group="CLIENT_STATUS" Label="상태" Placeholder="전체" Clearable="true" />
        </MudItem>
        <MudItem xs="12" md="2" Class="d-flex align-center">
            <MudButton Variant="Variant.Outlined" OnClick="@SearchAsync" FullWidth="true">검색</MudButton>
        </MudItem>
        <MudItem xs="12" md="2" Class="d-flex align-center">
            <MudButton Variant="Variant.Text" OnClick="@ResetAsync" FullWidth="true">초기화</MudButton>
        </MudItem>
    </MudGrid>
</MudPaper>

<AdminDataPanel Loading="@(clients is null)" SkeletonContent="@ClientListSkeleton">
    @if (clients is null)
    {
    }
    else if (!clients.Any())
    {
        <AdminEmptyState Icon="@Icons.Material.Filled.PeopleAlt" Message="등록된 고객이 없습니다." />
    }
    else
    {
        <MudSimpleTable Striped="true" Dense="true" Class="admin-table">
            <thead>
                <tr>
                    <th>이름</th>
                    <th>회사명</th>
                    <th>연락처</th>
                    <th>서비스</th>
                    <th>세금 유형</th>
                    <th>상태</th>
                    <th>유입 경로</th>
                    <th>등록일</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                @foreach (var c in clients)
                {
                    <tr>
                        <td><strong>@c.Name</strong></td>
                        <td>@(c.CompanyName ?? "—")</td>
                        <td>@(c.Phone ?? "—")</td>
                        <td>
                            @if (!string.IsNullOrEmpty(c.ServiceType))
                            {
                                <MudChip Size="Size.Small" Color="Color.Primary">@c.ServiceType</MudChip>
                            }
                        </td>
                        <td>@(c.TaxType ?? "—")</td>
                        <td>
                            @if (c.Status == "active")
                            {
                                <MudChip Size="Size.Small" Color="Color.Success">활성</MudChip>
                            }
                            else
                            {
                                <MudChip Size="Size.Small" Color="Color.Default">비활성</MudChip>
                            }
                        </td>
                        <td>@(c.Source ?? "—")</td>
                        <td class="small">@c.CreatedAt.ToLocalTime().ToString("yy.MM.dd")</td>
                        <td>
                            <MudButtonGroup Size="Size.Small" Variant="Variant.Outlined">
                                <MudButton @onclick="@(() => Navigation.NavigateTo($"/taxbaik/admin/clients/{c.Id}/edit"))">
                                    수정
                                </MudButton>
                                <MudButton Color="Color.Error" @onclick="@(() => DeleteAsync(c))">
                                    삭제
                                </MudButton>
                            </MudButtonGroup>
                        </td>
                    </tr>
                }
            </tbody>
        </MudSimpleTable>

        @* 페이징 *@
        @if (totalPages > 1)
        {
            <div class="d-flex justify-center pa-3">
                <MudPagination BoundaryCount="1" MiddleCount="3"
                               Count="@totalPages" Selected="@currentPage"
                               SelectedChanged="@OnPageChanged" />
            </div>
        }
        <MudText Typo="Typo.caption" Class="pa-2 text-muted">총 @(totalCount)명</MudText>
    }
</AdminDataPanel>

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

    private List<Client>? clients;
    private string searchText = "";
    private string statusFilter = "";
    private int currentPage = 1;
    private int totalCount;
    private int totalPages;
    private const int PageSize = 20;

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

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

    private async Task LoadAsync()
    {
        try
        {
            var (items, total) = await ClientClient.GetPagedAsync(
                currentPage, PageSize,
                string.IsNullOrEmpty(statusFilter) ? null : statusFilter,
                string.IsNullOrEmpty(searchText) ? null : searchText);

            clients = items.ToList();
            totalCount = total;
            totalPages = (int)Math.Ceiling((double)total / PageSize);
        }
        catch (Exception ex)
        {
            Snackbar.Add($"오류: {ex.Message}", Severity.Error);
            clients = [];
            totalCount = 0;
            totalPages = 0;
        }
    }

    private async Task SearchAsync()
    {
        currentPage = 1;
        await LoadAsync();
    }

    private async Task ResetAsync()
    {
        searchText = "";
        statusFilter = "";
        currentPage = 1;
        await LoadAsync();
    }

    private async Task OnPageChanged(int page)
    {
        currentPage = page;
        await LoadAsync();
    }

    private async Task OnSearchKeyUp(KeyboardEventArgs e)
    {
        if (e.Key == "Enter") await SearchAsync();
    }

    private async Task DeleteAsync(Client client)
    {
        var confirmed = await DialogService.ShowMessageBox(
            "고객 삭제",
            $"'{client.Name}' 고객을 삭제하시겠습니까? 관련 데이터도 함께 삭제됩니다.",
            yesText: "삭제", cancelText: "취소");

        if (confirmed != true) return;

        try
        {
            var success = await ClientClient.DeleteAsync(client.Id);
            if (success)
            {
                Snackbar.Add($"{client.Name} 고객이 삭제되었습니다.", Severity.Success);
                await LoadAsync();
            }
            else
            {
                Snackbar.Add("삭제에 실패했습니다.", Severity.Error);
            }
        }
        catch (Exception ex)
        {
            Snackbar.Add($"오류: {ex.Message}", Severity.Error);
        }
        await LoadAsync();
    }
}
