using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Domain.Entities.Content;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace CMSMicroservice.Application.SitePageSettingsCQ.Queries.GetPageSettings;

// ── Query ──
public class GetPageSettingsQuery : IRequest<PageSettingsDto?>
{
    public string PageKey { get; set; } = default!;
}

// ── Handler ──
public class GetPageSettingsQueryHandler : IRequestHandler<GetPageSettingsQuery, PageSettingsDto?>
{
    private readonly IApplicationDbContext _context;

    public GetPageSettingsQueryHandler(IApplicationDbContext context)
    {
        _context = context;
    }

    public async Task<PageSettingsDto?> Handle(GetPageSettingsQuery request, CancellationToken cancellationToken)
    {
        var entity = await _context.SitePageSettingsEntities
            .Include(x => x.Images.Where(i => !i.IsDeleted && i.IsActive).OrderBy(i => i.SortOrder))
            .FirstOrDefaultAsync(x => x.PageKey == request.PageKey && !x.IsDeleted, cancellationToken);

        if (entity == null)
            return null;

        return MapToDto(entity);
    }

    public static PageSettingsDto MapToDto(SitePageSettings entity) => new()
    {
        Id = entity.Id,
        PageKey = entity.PageKey,
        Title = entity.Title,
        MetaDescription = entity.MetaDescription,
        HeroTitle = entity.HeroTitle,
        HeroSubtitle = entity.HeroSubtitle,
        HeroImagePath = entity.HeroImagePath,
        IsActive = entity.IsActive,
        SettingsJson = entity.SettingsJson,
        Images = entity.Images.Select(i => new PageImageDto
        {
            Id = i.Id,
            ImageGroup = i.ImageGroup,
            Title = i.Title,
            Subtitle = i.Subtitle,
            Description = i.Description,
            ImagePath = i.ImagePath,
            ThumbnailPath = i.ThumbnailPath,
            LinkUrl = i.LinkUrl,
            IconName = i.IconName,
            SortOrder = i.SortOrder,
            IsActive = i.IsActive
        }).ToList()
    };
}

// ── DTOs ──
public class PageSettingsDto
{
    public long Id { get; set; }
    public string PageKey { get; set; } = string.Empty;
    public string Title { get; set; } = string.Empty;
    public string? MetaDescription { get; set; }
    public string? HeroTitle { get; set; }
    public string? HeroSubtitle { get; set; }
    public string? HeroImagePath { get; set; }
    public bool IsActive { get; set; }
    public string? SettingsJson { get; set; }
    public List<PageImageDto> Images { get; set; } = new();
}

public class PageImageDto
{
    public long Id { get; set; }
    public string ImageGroup { get; set; } = string.Empty;
    public string? Title { get; set; }
    public string? Subtitle { get; set; }
    public string? Description { get; set; }
    public string ImagePath { get; set; } = string.Empty;
    public string? ThumbnailPath { get; set; }
    public string? LinkUrl { get; set; }
    public string? IconName { get; set; }
    public int SortOrder { get; set; }
    public bool IsActive { get; set; }
}
