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 ViewMetaModel(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 AllowBookmark { get; set; }
        public bool AllowLike { get; set; }
        public bool AllowDislike { get; set; }
        public bool AllowPrint { get; set; }
        public bool AllowSnsShare { get; set; }
        public bool AllowPrevNextBotton { get; set; }
        public bool AllowBlame { get; set; }
        public ushort BlameHideCount { get; set; }
        public bool AllowContentLinkTargetBlank { get; set; }
        public bool AllowPostUrlCopy { get; set; }
        public bool AllowPostUrlQrCode { get; set; }
        public bool ShowMemberThumb { get; set; }
        public bool ShowMemberIcon { get; set; }
        public bool ShowMemberRegDate { get; set; }
        public bool ShowMemberSummary { 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,
            AllowBookmark = meta.View.AllowBookmark,
            AllowLike = meta.View.AllowLike,
            AllowDislike = meta.View.AllowDislike,
            AllowPrint = meta.View.AllowPrint,
            AllowSnsShare = meta.View.AllowSnsShare,
            AllowPrevNextBotton = meta.View.AllowPrevNextBotton,
            AllowBlame = meta.View.AllowBlame,
            BlameHideCount = meta.View.BlameHideCount,
            AllowContentLinkTargetBlank = meta.View.AllowContentLinkTargetBlank,
            AllowPostUrlCopy = meta.View.AllowPostUrlCopy,
            AllowPostUrlQrCode = meta.View.AllowPostUrlQrCode,
            ShowMemberThumb = meta.View.ShowMemberThumb,
            ShowMemberIcon = meta.View.ShowMemberIcon,
            ShowMemberRegDate = meta.View.ShowMemberRegDate,
            ShowMemberSummary = meta.View.ShowMemberSummary
        };
    }

    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,
                new BoardMetaView
                {
                    AllowBookmark = Input.AllowBookmark,
                    AllowLike = Input.AllowLike,
                    AllowDislike = Input.AllowDislike,
                    AllowPrint = Input.AllowPrint,
                    AllowSnsShare = Input.AllowSnsShare,
                    AllowPrevNextBotton = Input.AllowPrevNextBotton,
                    AllowBlame = Input.AllowBlame,
                    BlameHideCount = Input.BlameHideCount,
                    AllowContentLinkTargetBlank = Input.AllowContentLinkTargetBlank,
                    AllowPostUrlCopy = Input.AllowPostUrlCopy,
                    AllowPostUrlQrCode = Input.AllowPostUrlQrCode,
                    ShowMemberThumb = Input.ShowMemberThumb,
                    ShowMemberIcon = Input.ShowMemberIcon,
                    ShowMemberRegDate = Input.ShowMemberRegDate,
                    ShowMemberSummary = Input.ShowMemberSummary
                },
                null, null, null, null, null, null, null), ct);

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

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