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 NotifyTemplateModel(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 string? PostWriteEmailNotifySubject { get; set; }
        public string? PostWriteEmailNotifyContent { get; set; }
        public string? CommentWriteEmailNotifySubject { get; set; }
        public string? CommentWriteEmailNotifyContent { get; set; }
        public string? ReplyWriteEmailNotifySubject { get; set; }
        public string? ReplyWriteEmailNotifyContent { 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,
            PostWriteEmailNotifySubject = meta.NotifyTemplate.PostWriteEmailNotifySubject,
            PostWriteEmailNotifyContent = meta.NotifyTemplate.PostWriteEmailNotifyContent,
            CommentWriteEmailNotifySubject = meta.NotifyTemplate.CommentWriteEmailNotifySubject,
            CommentWriteEmailNotifyContent = meta.NotifyTemplate.CommentWriteEmailNotifyContent,
            ReplyWriteEmailNotifySubject = meta.NotifyTemplate.ReplyWriteEmailNotifySubject,
            ReplyWriteEmailNotifyContent = meta.NotifyTemplate.ReplyWriteEmailNotifyContent
        };
    }

    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, null, null, null, null,
                new BoardMetaNotifyTemplate
                {
                    PostWriteEmailNotifySubject = Input.PostWriteEmailNotifySubject,
                    PostWriteEmailNotifyContent = Input.PostWriteEmailNotifyContent,
                    CommentWriteEmailNotifySubject = Input.CommentWriteEmailNotifySubject,
                    CommentWriteEmailNotifyContent = Input.CommentWriteEmailNotifyContent,
                    ReplyWriteEmailNotifySubject = Input.ReplyWriteEmailNotifySubject,
                    ReplyWriteEmailNotifyContent = Input.ReplyWriteEmailNotifyContent
                },
                null), ct);

            TempData["SuccessMessage"] = "알림 양식이 저장되었습니다.";
        }
        catch (Exception e)
        {
            TempData["ErrorMessages"] = e.Message;
        }

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