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

namespace Admin.Pages.Faq.List
{
    public class IndexModel(IMediator mediator) : PageModel
    {
        [BindProperty(SupportsGet = true)]
        public QueryParams Query { get; set; } = new();
        public List<(int ID, string Code, string Subject, int FaqItemRows)> Categories { get; set; } = [];

        public sealed class QueryParams
        {
            [Range(1, int.MaxValue)]
            [DisplayName("??? ??")]
            public int PageNum { get; set; } = 1;

            [Range(1, 100)]
            [DisplayName("??? ?? ?")]
            public ushort PerPage { get; set; } = 10;

            [DisplayName("FAQ ??")]
            public int? CategoryID { get; set; }

            [DisplayName("???")]
            public string? Keyword { get; set; }
        }

        public int Total { get; set; } = 0;
        public List<(
            int Num,
            int ID,
            string CategoryCode,
            string CategorySubject,
            string Question,
            string? Answer,
            short Order,
            char IsActive,
            string? UpdatedAt,
            string CreatedAt,
            string EditURL
        )> List { get; set; } = [];

        public Pagination? Pagination { get; set; }

        public async Task OnGetAsync(CancellationToken ct)
        {
            if (!ModelState.IsValid)
            {
                return;
            }

            Categories = [..(await mediator.Send(new GetFaqCategories.Query(), ct)).List.Select(c => (c.ID, c.Code, c.Subject, c.FaqItemRows))];

            var result = await mediator.Send(new SearchFaqItems.Query(Query.CategoryID, Query.Keyword, Query.PageNum, Query.PerPage), ct);
            var queryString = Query.ToQueryString();

            Total = result.Total;
            List = [..result.List.Select(c => (
                c.Num,
                c.ID,
                c.CategoryCode,
                c.CategorySubject,
                c.Question,
                c.Answer,
                c.Order,
                c.IsActive ? 'Y' : 'N',
                c.UpdatedAt.GetDateAt() ?? "-",
                c.CreatedAt.GetDateAt(),
                EditURL: $"/Faq/List/Edit/{c.ID}{Request.QueryString}"
            ))];

            Pagination = new Pagination(result.Total, Query.PageNum, Query.PerPage);
        }

        public async Task<IActionResult> OnPostDeleteAsync(int[] ids, CancellationToken ct)
        {
            try
            {
                await mediator.Send(new DeleteFaqItem.Command(ids), ct);

                TempData["SuccessMessage"] = $"{ids.Length}? FAQ? ???????.";
            }
            catch (Exception e)
            {
                TempData["ErrorMessages"] = e.Message;
            }

            return RedirectToPage("/Faq/List/Index", Query);
        }
    }
}