using SharedKernel;
using SharedKernel.Extensions;
using Application.Abstractions.Messaging;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Options;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace Admin.Pages.Document
{
    public class EditModel(IMediator mediator, IOptions<AppSettings> settings) : PageModel
    {
        [BindProperty]
        public string? QueryString { get; set; }
        public string SiteUrl { get; private set; } = settings.Value.App.FrontURL;

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

        public sealed class InputMdeol
        {
            [DisplayName("ID")]
            [Required(ErrorMessage = "{0}? ?????.")]
            public int ID { get; set; }

            [DisplayName("??")]
            [DataType(DataType.Text)]
            [Required(ErrorMessage = "{0}? ?????.")]
            [StringLength(30, ErrorMessage = "{0}? {1}? ??? ?????.")]
            [RegularExpression(@"^[a-zA-Z0-9]+$", ErrorMessage = "??? ?? ? ???? ????? ???.")]
            public string Code { get; set; } = default!;

            [DisplayName("??")]
            [DataType(DataType.Text)]
            [Required(ErrorMessage = "{0}? ?????.")]
            [StringLength(120, ErrorMessage = "{0}? {1}? ??? ?????.")]
            public string Subject { get; set; } = default!;

            [DisplayName("??")]
            [DataType(DataType.Html)]
            public string? Content { get; set; }

            [DisplayName("?? ??")]
            public bool IsActive { get; set; } = false;

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

        public async Task OnGetAsync(int id, CancellationToken ct)
        {
            QueryString = HttpContext.Request.QueryString.HasValue ? HttpContext.Request.QueryString.Value!.TrimStart('?') : "";

            var document =  await mediator.Send(new GetDocument.Query(id), ct);
            if (document != null)
            {
                Input.ID = document.ID;
                Input.Code = document.Code;
                Input.Subject = document.Subject;
                Input.Content = document.Content;
                Input.IsActive = document.IsActive;
                Input.UpdatedAt = document.UpdatedAt.GetDateAt() ?? "-";
                Input.CreatedAt = document.CreatedAt.GetDateAt();
            }
        }

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

                var command = new UpdateDocument.Command(
                    Input.ID,
                    Input.Code,
                    Input.Subject,
                    Input.Content,
                    Input.IsActive
                );

                await mediator.Send(command, ct);

                TempData["SuccessMessage"] = $"{Input.Subject} ??? ???????.";
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
            }

            return Redirect($"/Document/Edit/{Input.ID}?{QueryString}");
        }
    }
}