@using TaxBaik.Application.DTOs
@using TaxBaik.Domain.Entities

<MudForm @ref="form">
    <AdminFormSection Title="기본 정보" Description="제목과 카테고리, 발행 여부를 먼저 설정합니다." CssClass="mb-4">
        <MudTextField @bind-Value="Model.Title" Label="제목 *"
            Variant="Variant.Outlined" Class="mb-4" Required="true" RequiredError="제목을 입력하세요." Counter="100" MaxLength="100" />

        <MudSelect T="int?" @bind-Value="Model.CategoryId" Label="카테고리"
            Variant="Variant.Outlined" Class="mb-4">
            @foreach (var category in Categories)
            {
                <MudSelectItem T="int?" Value="@((int?)category.Id)">@category.Name</MudSelectItem>
            }
        </MudSelect>

        <MudCheckBox @bind-Checked="Model.IsPublished" Label="즉시 발행" Class="mb-4" />
    </AdminFormSection>

    <AdminFormSection Title="본문" Description="SEO와 실제 노출 본문을 함께 관리합니다." CssClass="mb-4">
        <MudTextField @bind-Value="Model.Content" Label="본문 내용 *"
            Variant="Variant.Outlined" Lines="16" Required="true" RequiredError="본문 내용을 입력하세요."
            Class="mb-4" />

        <MudTextField @bind-Value="Model.Tags" Label="태그 (쉼표로 구분)"
            Variant="Variant.Outlined" Class="mb-4" />

        <MudTextField @bind-Value="Model.SeoTitle" Label="SEO 제목"
            Variant="Variant.Outlined" Class="mb-4" />

        <MudTextField @bind-Value="Model.SeoDescription" Label="SEO 설명"
            Variant="Variant.Outlined" Lines="3" Class="mb-4" />
    </AdminFormSection>

    <AdminFormActions SubmitText="@SubmitText"
                      LoadingText="저장 중..."
                      CancelText="취소"
                      SubmitIcon="@Icons.Material.Filled.Save"
                      OnSubmit="@HandleSubmit"
                      OnCancel="@OnCancel"
                      IsSubmitting="false" />
</MudForm>

@code {
    [Parameter, EditorRequired]
    public BlogFormModel Model { get; set; } = new();

    [Parameter]
    public IReadOnlyList<Category> Categories { get; set; } = [];

    [Parameter]
    public string SubmitText { get; set; } = "저장";

    [Parameter]
    public EventCallback OnSubmit { get; set; }

    [Parameter]
    public EventCallback OnCancel { get; set; }

    private MudForm? form;

    private async Task HandleSubmit()
    {
        if (form == null)
            return;

        await form.Validate();
        if (!form.IsValid)
            return;

        await OnSubmit.InvokeAsync();
    }

    public class BlogFormModel
    {
        public string Title { get; set; } = "";
        public string Content { get; set; } = "";
        public int? CategoryId { get; set; }
        public string? Tags { get; set; }
        public string? SeoTitle { get; set; }
        public string? SeoDescription { get; set; }
        public bool IsPublished { get; set; }
    }
}
