// PRE-GENERATED (design session). Stub so the DB manager popup renders & animates NOW,
// without the real SQLite/AutoEQ layer. Replace with the real implementation in M2.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DansoriEQ.Core.Profiles;

namespace DansoriEQ.App.Design;

public sealed class DesignProfileDbService : IProfileDbService
{
    public DbInfo GetInfo() => Build(updated: false);

    private static readonly string[] KnownNames =
    {
        "Truthear × Crinacle Zero", "Moondrop Blessing 2", "7Hz Timeless", "Etymotic ER2XR",
        "Sennheiser HD 600", "HIFIMAN Sundara", "Focal Clear"
    };

    public ProfileRef? FindMatch(string name, string? brand, string type)
    {
        if (string.IsNullOrWhiteSpace(name)) return null;
        foreach (var n in KnownNames)
            if (n.Equals(name, StringComparison.OrdinalIgnoreCase)
                || n.Contains(name, StringComparison.OrdinalIgnoreCase)
                || name.Contains(n, StringComparison.OrdinalIgnoreCase))
                return new ProfileRef { Id = n, Name = n, Type = type };
        return null;
    }

    public async Task<DbInfo> UpdateAsync(IProgress<DbUpdateProgress>? progress = null, CancellationToken ct = default)
    {
        string[] steps =
        {
            "AutoEQ 저장소 최신 커밋 확인",
            "변경분 다운로드",
            "ParametricEQ 프로파일 파싱",
            "SQLite 병합(upsert)",
            "인덱스 갱신"
        };
        for (int i = 0; i < steps.Length; i++)
        {
            ct.ThrowIfCancellationRequested();
            progress?.Report(new DbUpdateProgress { Message = steps[i] + "…", Fraction = (double)i / steps.Length });
            await Task.Delay(500, ct);
        }
        progress?.Report(new DbUpdateProgress { Message = "완료", Fraction = 1.0 });
        return Build(updated: true);
    }

    private static DbInfo Build(bool updated)
    {
        var stamp = new DateTimeOffset(2026, 6, 28, 9, 12, 0, TimeSpan.Zero);
        return new DbInfo
        {
            TotalProfiles = updated ? 4271 : 4213,
            IemCount = updated ? 2171 : 2140,
            HeadphoneCount = updated ? 2100 : 2073,
            DbPath = @"%LOCALAPPDATA%\DansoriEQ\dansorieq.db",
            DbSizeBytes = 7_340_032,
            SchemaVersion = 1,
            LastUpdatedUtc = stamp,
            Sources = new List<DbSourceInfo>
            {
                new() { Name = "AutoEQ", License = "MIT / mixed", Distributable = true,
                        Url = "github.com/jaakkopasanen/AutoEq", Commit = "a1b2c3d", ProfileCount = 3600,
                        Status = updated ? SourceStatus.UpToDate : SourceStatus.UpdateAvailable, UpdatedUtc = stamp },
                new() { Name = "oratory1990", License = "restricted", Distributable = false,
                        Url = "reddit.com/u/oratory1990", ProfileCount = 480,
                        Status = SourceStatus.UpToDate, UpdatedUtc = new DateTimeOffset(2026, 5, 2, 0, 0, 0, TimeSpan.Zero) },
                new() { Name = "Crinacle (squig)", License = "restricted", Distributable = false,
                        Url = "squig.link", ProfileCount = 133,
                        Status = SourceStatus.UpToDate, UpdatedUtc = new DateTimeOffset(2026, 4, 18, 0, 0, 0, TimeSpan.Zero) },
            }
        };
    }
}
