using Domain.Entities.Forum.Boards;
using SharedKernel.Extensions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Forum.Board.Meta;

public class CommentModel(IMediator mediator) : PageModel
{
    public int BoardID { get; set; }
    public List<(int ID, string Name)> BoardList { get; set; } = [];
    public string? QueryString { get; set; }

    [BindProperty]
    public InputModel Input { get; set; } = new();

    public sealed class InputModel
    {
        public int ID { get; set; }
        public int BoardID { get; set; }
        public bool EnableComment { get; set; }
        public ushort PerPage { get; set; }
        public bool AllowLike { get; set; }
        public bool AllowDisLike { get; set; }
        public bool ShowMemberThumb { get; set; }
        public bool ShowMemberIcon { get; set; }
        public string? ContentPlaceholder { get; set; }
        public ushort MinContentLength { get; set; }
        public ushort MaxContentLength { get; set; }
        public bool EnableEditor { get; set; }
        public bool AllowSecret { get; set; }
        public ushort BlameHideCount { get; set; }
        public bool AllowUpdateProtection { get; set; }
        public ushort UpdateProtectionDays { get; set; }
        public bool AllowDeleteProtection { get; set; }
        public ushort DeleteProtectionDays { get; set; }
        public bool EnableCommentUpdateLog { get; set; }
    }

    public async Task OnGetAsync(int id, CancellationToken ct)
    {
        BoardID = id;
        QueryString = Request.QueryString.ToString();

        var boards = await mediator.Send(new SearchBoards.Query(null, null, 1, 100), ct);
        BoardList = [..boards.List.Select(c => (c.ID, c.Name))];

        var meta = await mediator.Send(new GetBoardMeta.Query(id), ct);

        Input = new InputModel
        {
            ID = meta.ID,
            BoardID = meta.BoardID,
            EnableComment = meta.Comment.EnableComment,
            PerPage = meta.Comment.PerPage,
            AllowLike = meta.Comment.AllowLike,
            AllowDisLike = meta.Comment.AllowDisLike,
            ShowMemberThumb = meta.Comment.ShowMemberThumb,
            ShowMemberIcon = meta.Comment.ShowMemberIcon,
            ContentPlaceholder = meta.Comment.ContentPlaceholder,
            MinContentLength = meta.Comment.MinContentLength,
            MaxContentLength = meta.Comment.MaxContentLength,
            EnableEditor = meta.Comment.EnableEditor,
            AllowSecret = meta.Comment.AllowSecret,
            BlameHideCount = meta.Comment.BlameHideCount,
            AllowUpdateProtection = meta.Comment.AllowUpdateProtection,
            UpdateProtectionDays = meta.Comment.UpdateProtectionDays,
            AllowDeleteProtection = meta.Comment.AllowDeleteProtection,
            DeleteProtectionDays = meta.Comment.DeleteProtectionDays,
            EnableCommentUpdateLog = meta.Comment.EnableCommentUpdateLog
        };
    }

    public async Task<IActionResult> OnPostAsync(CancellationToken ct)
    {
        try
        {
            if (!ModelState.IsValid)
            {
                throw new Exception(ModelState.GetErrorMessages());
            }

            await mediator.Send(new UpdateBoardMeta.Command(
                Input.ID,
                Input.BoardID,
                null,
                null,
                null,
                new BoardMetaComment
                {
                    EnableComment = Input.EnableComment,
                    PerPage = Input.PerPage,
                    AllowLike = Input.AllowLike,
                    AllowDisLike = Input.AllowDisLike,
                    ShowMemberThumb = Input.ShowMemberThumb,
                    ShowMemberIcon = Input.ShowMemberIcon,
                    ContentPlaceholder = Input.ContentPlaceholder,
                    MinContentLength = Input.MinContentLength,
                    MaxContentLength = Input.MaxContentLength,
                    EnableEditor = Input.EnableEditor,
                    AllowSecret = Input.AllowSecret,
                    BlameHideCount = Input.BlameHideCount,
                    AllowUpdateProtection = Input.AllowUpdateProtection,
                    UpdateProtectionDays = Input.UpdateProtectionDays,
                    AllowDeleteProtection = Input.AllowDeleteProtection,
                    DeleteProtectionDays = Input.DeleteProtectionDays,
                    EnableCommentUpdateLog = Input.EnableCommentUpdateLog
                },
                null,
                null,
                null,
                null,
                null
            ), ct);

            TempData["SuccessMessage"] = "댓글 설정이 저장되었습니다.";
        }
        catch (Exception e)
        {
            TempData["ErrorMessages"] = e.Message;
        }

        return Redirect($"/Forum/Board/Meta/Comment/{Input.BoardID}{Request.QueryString}");
    }
}