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

namespace Admin.Pages.Forum.Board.Meta;

public class WriteModel(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 string? DefaultSubject { get; set; }
        public string? DefaultContent { get; set; }
        public bool AllowEditor { get; set; }
        public bool AllowPrefix { get; set; }
        public bool RequiredPrefix { get; set; }
        public bool AllowSecret { get; set; }
        public bool AllowTag { get; set; }
        public byte TagLimit { get; set; }
        public bool AllowImage { get; set; }
        public byte ImageUploadLimit { get; set; }
        public int ImageUploadMaxSize { get; set; }
        public bool AllowMedia { get; set; }
        public byte MediaUploadLimit { get; set; }
        public bool AllowFile { get; set; }
        public byte FileUploadLimit { get; set; }
        public int FileUploadMaxSize { get; set; }
        public string? FileUploadExtension { 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.Write.ShowHeader,
            HeaderContent = meta.Write.HeaderContent,
            ShowFooter = meta.Write.ShowFooter,
            FooterContent = meta.Write.FooterContent,
            DefaultSubject = meta.Write.DefaultSubject,
            DefaultContent = meta.Write.DefaultContent,
            AllowEditor = meta.Write.AllowEditor,
            AllowPrefix = meta.Write.AllowPrefix,
            RequiredPrefix = meta.Write.RequiredPrefix,
            AllowSecret = meta.Write.AllowSecret,
            AllowTag = meta.Write.AllowTag,
            TagLimit = meta.Write.TagLimit,
            AllowImage = meta.Write.AllowImage,
            ImageUploadLimit = meta.Write.ImageUploadLimit,
            ImageUploadMaxSize = meta.Write.ImageUploadMaxSize,
            AllowMedia = meta.Write.AllowMedia,
            MediaUploadLimit = meta.Write.MediaUploadLimit,
            AllowFile = meta.Write.AllowFile,
            FileUploadLimit = meta.Write.FileUploadLimit,
            FileUploadMaxSize = meta.Write.FileUploadMaxSize,
            FileUploadExtension = meta.Write.FileUploadExtension
        };
    }

    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,
                new BoardMetaWrite
                {
                    ShowHeader = Input.ShowHeader,
                    HeaderContent = Input.HeaderContent,
                    ShowFooter = Input.ShowFooter,
                    FooterContent = Input.FooterContent,
                    DefaultSubject = Input.DefaultSubject,
                    DefaultContent = Input.DefaultContent,
                    AllowEditor = Input.AllowEditor,
                    AllowPrefix = Input.AllowPrefix,
                    RequiredPrefix = Input.RequiredPrefix,
                    AllowSecret = Input.AllowSecret,
                    AllowTag = Input.AllowTag,
                    TagLimit = Input.TagLimit,
                    AllowImage = Input.AllowImage,
                    ImageUploadLimit = Input.ImageUploadLimit,
                    ImageUploadMaxSize = Input.ImageUploadMaxSize,
                    AllowMedia = Input.AllowMedia,
                    MediaUploadLimit = Input.MediaUploadLimit,
                    AllowFile = Input.AllowFile,
                    FileUploadLimit = Input.FileUploadLimit,
                    FileUploadMaxSize = Input.FileUploadMaxSize,
                    FileUploadExtension = Input.FileUploadExtension
                },
                null, null, null, null, null, null), ct);

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

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