using JSMR.Application.Integrations.Chobit.Models;
using JSMR.Application.Integrations.DLSite.Models;
using JSMR.Domain.Enums;
using JSMR.Domain.ValueObjects;

namespace JSMR.Application.Scanning.Contracts;

public sealed record VoiceWorkIngest
{
    public required string MakerId { get; init; }
    public required string MakerName { get; init; }
    public required string ProductId { get; init; }
    public required string Title { get; init; }
    public required string Description { get; init; }
    public ICollection<string> Tags { get; init; } = [];
    public ICollection<string> Creators { get; init; } = [];
    public int WishlistCount { get; init; }
    public int Downloads { get; init; }
    public bool HasTrial { get; init; }
    public bool HasChobit { get; init; }
    public byte? StarRating { get; init; }
    public int? Votes { get; init; }
    public AgeRating AgeRating { get; init; }
    public bool HasImage { get; init; }
    public IReadOnlyList<SupportedLanguage> SupportedLanguages { get; init; } = [];
    public DateOnly? ExpectedDate { get; init; }
    public DateOnly? SalesDate { get; init; }
    public DateTime? RegistrationDate { get; init; }
    public AIGeneration AI { get; init; }
    public VoiceWorkSeries? Series { get; init; }
    public VoiceWorkTranslation? Translation { get; init; }
    public VoiceWorkLocalizationIngest[] Localizations { get; init; } = [];

    public static VoiceWorkIngest From(DLSiteWork work, VoiceWorkDetails? details, ChobitResult? chobit)
    {
        return new VoiceWorkIngest()
        {
            MakerId = work.MakerId,
            MakerName = work.Maker,
            ProductId = work.ProductId,
            Title = details?.Title ?? work.ProductName,
            Description = work.Description ?? string.Empty,
            Tags = work.Tags,
            Creators = work.Creators,
            WishlistCount = details?.WishlistCount ?? 0,
            Downloads = Math.Max(work.Downloads, details?.DownloadCount ?? 0),
            HasTrial = work.HasTrial || (details?.HasTrial ?? false),
            HasChobit = chobit?.Count > 0,
            StarRating = work.StarRating,
            Votes = work.Votes,
            AgeRating = details?.AgeRating ?? work.AgeRating,
            HasImage = !string.IsNullOrEmpty(work.ImageUrl) && !work.ImageUrl.Contains("no_img", StringComparison.OrdinalIgnoreCase),
            SupportedLanguages = details?.SupportedLanguages ?? [],
            ExpectedDate = work.ExpectedDate,
            SalesDate = work.SalesDate,
            RegistrationDate = details?.RegistrationDate,
            AI = details?.AI ?? AIGeneration.None,
            Series = details?.Series,
            Translation = details?.Translation
        };
    }
}