using Domain.Entities.Forum.Boards;
using Domain.Entities.Forum.ValueObject;
using SharedKernel.Extensions;
using MediatR;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Admin.Pages.Forum.Board.Meta;

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

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

    public sealed class InputModel
    {
        public int ID { get; set; }
        public int BoardID { get; set; }
        public short BoardAccess { get; set; }
        public short PostView { get; set; }
        public short PostWrite { get; set; }
        public short CommentView { get; set; }
        public short CommentWrite { get; set; }
        public short ReplyWrite { get; set; }
        public short FileUpload { get; set; }
        public short FileDownload { get; set; }
    }

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

        PermissionList = [..Enum.GetValues<BoardPermission>().Select(e => new SelectListItem
        {
            Value = ((short)e).ToString(),
            Text = e.GetType().GetMember(e.ToString()).FirstOrDefault() ?.GetCustomAttribute<DisplayAttribute>()?.Name ?? e.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,
            BoardAccess = meta.Permission.BoardAccess,
            PostView = meta.Permission.PostView,
            PostWrite = meta.Permission.PostWrite,
            CommentView = meta.Permission.CommentView,
            CommentWrite = meta.Permission.CommentWrite,
            ReplyWrite = meta.Permission.ReplyWrite,
            FileUpload = meta.Permission.FileUpload,
            FileDownload = meta.Permission.FileDownload
        };
    }

    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,
                new BoardMetaPermission
                {
                    BoardAccess = Input.BoardAccess,
                    PostView = Input.PostView,
                    PostWrite = Input.PostWrite,
                    CommentView = Input.CommentView,
                    CommentWrite = Input.CommentWrite,
                    ReplyWrite = Input.ReplyWrite,
                    FileUpload = Input.FileUpload,
                    FileDownload = Input.FileDownload
                },
                null, null, null), ct);

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

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