using Domain.Entities.Forum.Boards;
using Domain.Entities.Forum.ValueObject;
using SharedKernel.Extensions;
using Application.Abstractions.Messaging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Forum.Board.Meta;

public class ListModel(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 ShowHeader { get; set; }
        public string? HeaderContent { get; set; }
        public bool ShowFooter { get; set; }
        public string? FooterContent { get; set; }
        public BoardLayout? Layout { get; set; }
        public BoardSort? Sort { get; set; }
        public byte PerPage { get; set; }
        public bool AlwaysShowWriteButton { get; set; }
        public bool ShowFooterListView { get; set; }
        public bool IsNewIcon { get; set; }
        public bool IsHotIcon { get; set; }
        public bool ExceptNotice { get; set; }
        public bool ExceptSpeaker { 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,
            ShowHeader = meta.List.ShowHeader,
            HeaderContent = meta.List.HeaderContent,
            ShowFooter = meta.List.ShowFooter,
            FooterContent = meta.List.FooterContent,
            Layout = meta.List.Layout,
            Sort = meta.List.Sort,
            PerPage = meta.List.PerPage,
            AlwaysShowWriteButton = meta.List.AlwaysShowWriteButton,
            ShowFooterListView = meta.List.ShowFooterListView,
            IsNewIcon = meta.List.IsNewIcon,
            IsHotIcon = meta.List.IsHotIcon,
            ExceptNotice = meta.List.ExceptNotice,
            ExceptSpeaker = meta.List.ExceptSpeaker
        };
    }

    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,
                new BoardMetaList
                {
                    ShowHeader = Input.ShowHeader,
                    HeaderContent = Input.HeaderContent,
                    ShowFooter = Input.ShowFooter,
                    FooterContent = Input.FooterContent,
                    Layout = Input.Layout,
                    Sort = Input.Sort,
                    PerPage = Input.PerPage,
                    AlwaysShowWriteButton = Input.AlwaysShowWriteButton,
                    ShowFooterListView = Input.ShowFooterListView,
                    IsNewIcon = Input.IsNewIcon,
                    IsHotIcon = Input.IsHotIcon,
                    ExceptNotice = Input.ExceptNotice,
                    ExceptSpeaker = Input.ExceptSpeaker
                },
                null,
                null,
                null,
                null,
                null,
                null,
                null,
                null
            ), ct);

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

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