using SharedKernel.Extensions;
using MediatR;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Admin.Pages.Forum.Posts.List
{
    public class ViewModel(IMediator mediator) : PageModel
    {
        public int ID { get; set; }
        public int BoardID { get; set; }
        public string BoardName { get; set; } = default!;
        public int? BoardPrefixID { get; set; }
        public string? BoardPrefixName { get; set; }
        public string? BoardPrefixColor { get; set; }
        public string Subject { get; set; } = default!;
        public new string Content { get; set; } = default!;
        public string? Thumbnail { get; set; }
        public string? Name { get; set; }
        public string? SID { get; set; }
        public bool IsNotice { get; set; }
        public bool IsSecret { get; set; }
        public bool IsAnonymous { get; set; }
        public bool IsSpeaker { get; set; }
        public bool IsDeleted { get; set; }
        public bool IsQnA { get; set; }
        public int Views { get; set; }
        public int Likes { get; set; }
        public int Dislikes { get; set; }
        public int CommentCount { get; set; }
        public string? UpdatedAt { get; set; }
        public string CreatedAt { get; set; } = default!;

        public List<FileItemView> Files { get; set; } = [];
        public List<ImageItemView> Images { get; set; } = [];
        public List<TagItemView> Tags { get; set; } = [];

        public string? ReturnUrl { get; set; }
        public string? QueryString { get; set; }

        public sealed record FileItemView(int ID, string FileName, string Url, string? Extension, long? Size, int Downloads);
        public sealed record ImageItemView(int ID, string FileName, string Url);
        public sealed record TagItemView(int TagID, string Name);

        public async Task OnGetAsync(int id, CancellationToken ct)
        {
            var result = await mediator.Send(new GetPost.Query(id), ct);

            ID = result.ID;
            BoardID = result.BoardID;
            BoardName = result.BoardName;
            BoardPrefixID = result.BoardPrefixID;
            BoardPrefixName = result.BoardPrefixName;
            BoardPrefixColor = result.BoardPrefixColor;
            Subject = result.Subject;
            Content = result.Content;
            Thumbnail = result.Thumbnail;
            Name = result.Name;
            SID = result.SID;
            IsNotice = result.IsNotice;
            IsSecret = result.IsSecret;
            IsAnonymous = result.IsAnonymous;
            IsSpeaker = result.IsSpeaker;
            IsDeleted = result.IsDeleted;
            IsQnA = result.IsQnA;
            Views = result.Views;
            Likes = result.Likes;
            Dislikes = result.Dislikes;
            CommentCount = result.Comments;
            UpdatedAt = result.UpdatedAt.GetDateAt();
            CreatedAt = result.CreatedAt.GetDateAt();

            Files = [..result.Files.Where(f => !f.IsDisabled).Select(f => new FileItemView(f.ID, f.FileName, f.Url, f.Extension, f.Size, f.Downloads))];
            Images = [..result.Images.Where(f => !f.IsDisabled).Select(f => new ImageItemView(f.ID, f.FileName, f.Url))];
            Tags = [..result.Tags.Select(t => new TagItemView(t.TagID, t.Name))];

            ReturnUrl = Request.Headers.Referer.ToString();
            QueryString = Request.QueryString.ToString();
        }
    }
}
