using SharedKernel.Extensions;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Admin.Pages.Channel.List
{
    public class EditModel(IMediator mediator) : PageModel
    {
        [BindProperty]
        public string? QueryString { get; set; }

        public string? ReturnUrl { get; set; }

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

        public string? MemberInfo { get; set; }

        public sealed class InputModel
        {
            [Required(ErrorMessage = "ID는 필수입니다.")]
            public int ID { get; set; }

            public int MemberID { get; set; }

            [DisplayName("이름")]
            [Required(ErrorMessage = "{0}은 필수입니다.")]
            [StringLength(200, ErrorMessage = "{0}은 {1}자 이내로 입력하세요.")]
            public string Name { get; set; } = default!;

            [DisplayName("핸들")]
            [StringLength(30, ErrorMessage = "{0}은 {1}자 이내로 입력하세요.")]
            public string? Handle { get; set; }

            [DisplayName("YouTube 주소")]
            [Required(ErrorMessage = "{0}은 필수입니다.")]
            [StringLength(255, ErrorMessage = "{0}은 {1}자 이내로 입력하세요.")]
            public string YouTubeUrl { get; set; } = default!;

            [DisplayName("수수료(%)")]
            [Range(0, 100, ErrorMessage = "{0}은 {1}~{2} 사이여야 합니다.")]
            public decimal PlatformFeeRate { get; set; }

            [DisplayName("인증 여부")]
            public bool IsVerified { get; set; }

            [DisplayName("사용 여부")]
            public bool IsActive { get; set; }

            public string? UpdatedAt { get; set; }
            public string CreatedAt { get; set; } = default!;
        }

        public async Task OnGetAsync(int id, CancellationToken ct)
        {
            var referer = Request.Headers.Referer.ToString();
            ReturnUrl = string.IsNullOrEmpty(referer) ? null : referer;

            var result = await mediator.Send(new GetChannel.Query(id), ct);
            if (result is null)
            {
                return;
            }

            MemberInfo = $"{result.MemberEmail}, {result.MemberName ?? result.MemberSID ?? "-"}";

            Input = new InputModel
            {
                ID = result.ID,
                MemberID = result.MemberID,
                Name = result.Name,
                Handle = result.Handle ?? "-",
                YouTubeUrl = result.YouTubeUrl,
                PlatformFeeRate = result.PlatformFeeRate,
                IsVerified = result.IsVerified,
                IsActive = result.IsActive,
                UpdatedAt = result.UpdatedAt.GetDateAt() ?? "-",
                CreatedAt = result.CreatedAt.GetDateAt()
            };

            QueryString = Request.QueryString.ToString();
        }

        public async Task<IActionResult> OnPostAsync(CancellationToken ct)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Page();
                }

                await mediator.Send(new UpdateChannel.Command(
                    Input.ID,
                    Input.Name,
                    Input.Handle,
                    Input.YouTubeUrl,
                    Input.PlatformFeeRate,
                    Input.IsVerified,
                    Input.IsActive
                ), ct);

                TempData["SuccessMessage"] = "채널이 수정되었습니다.";
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect($"/Channel/List/Edit/{Input.ID}{Request.QueryString}");
        }

        public async Task<IActionResult> OnPostDeleteAsync(CancellationToken ct)
        {
            try
            {
                await mediator.Send(new DeleteChannel.Command([Input.ID]), ct);

                TempData["SuccessMessage"] = "채널이 삭제되었습니다.";

                return RedirectToPage("/Channel/List/Index");
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;

                return Redirect($"/Channel/List/Edit/{Input.ID}{Request.QueryString}");
            }
        }
    }
}