<MudDataGrid T="Domain.Entities.Inquiry"
    Items="@filteredInquiries"
    Dense="true"
    Hover="true"
    Striped="true"
    Virtualize="true"
    RowsPerPage="30"
    Class="admin-grid mt-4">

    <Columns>
        <PropertyColumn Property="x => x.Id" Title="ID" Sortable="true"
            Class="admin-table-id-column" />

        <PropertyColumn Property="x => x.Name" Title="이름" Sortable="true"
            Filterable="true" />

        <PropertyColumn Property="x => x.Phone" Title="전화" Sortable="false" />

        <PropertyColumn Property="x => x.ServiceType" Title="분야" Sortable="true"
            Filterable="true" />

        <TemplateColumn Title="상태" Sortable="true">
            <CellTemplate>
                <MudChip T="string"
                    Size="Size.Small"
                    Color="@GetStatusColor(context.Item.Status)"
                    Variant="Variant.Filled">
                    @GetStatusLabel(context.Item.Status)
                </MudChip>
            </CellTemplate>
        </TemplateColumn>

        <PropertyColumn Property="x => x.Message" Title="메시지"
            Sortable="false"
            CellClass="admin-table-message-column" />

        <PropertyColumn Property="x => x.CreatedAt" Title="생성일"
            Sortable="true"
            Format="yyyy-MM-dd" />

        <TemplateColumn Title="작업" CellStyleFunc="@(_ => "text-align: center;")">
            <CellTemplate>
                <MudButtonGroup Size="Size.Small" Variant="Variant.Outlined">
                    <MudButton Color="Color.Primary"
                        Href="@($"./inquiries/{context.Item.Id}")">
                        @Icons.Material.Filled.Visibility
                    </MudButton>
                    <MudButton Color="Color.Info"
                        Href="@($"./inquiries/{context.Item.Id}/edit")">
                        @Icons.Material.Filled.Edit
                    </MudButton>
                </MudButtonGroup>
            </CellTemplate>
        </TemplateColumn>
    </Columns>

    <PagerContent>
        <MudDataGridPager T="Domain.Entities.Inquiry" />
    </PagerContent>
</MudDataGrid>

@code {
    [Parameter, EditorRequired]
    public IReadOnlyList<Domain.Entities.Inquiry> Inquiries { get; set; } = [];

    [Parameter]
    public string Status { get; set; } = "";

    private IReadOnlyList<Domain.Entities.Inquiry> filteredInquiries = [];

    protected override void OnParametersSet()
    {
        if (Inquiries == null || Inquiries.Count == 0)
        {
            filteredInquiries = [];
            return;
        }

        filteredInquiries = string.IsNullOrEmpty(Status)
            ? Inquiries
            : Inquiries.Where(x => x.Status == Status).ToList();
    }

    private static Color GetStatusColor(string status) => status switch
    {
        "new" => Color.Warning,
        "consulting" => Color.Info,
        "contracted" => Color.Success,
        "rejected" => Color.Error,
        "closed" => Color.Dark,
        _ => Color.Default
    };

    private static string GetStatusLabel(string status) =>
        status switch
        {
            "new" => "신규",
            "consulting" => "상담중",
            "contracted" => "계약완료",
            "rejected" => "거절",
            "closed" => "종결",
            _ => status
        };
}
