using Scry.Core.Models;

namespace Scry.Services;

/// <summary>
/// Mock implementation that returns random MTG cards for testing.
/// Replace with RealCardRecognitionService for production use.
/// </summary>
public class MockCardRecognitionService : ICardRecognitionService
{
    private static readonly Card[] SampleCards =
    [
        new Card
        {
            Id = "4cbc6901-6a4a-4d0a-83ea-7eefa3b35021",
            OracleId = "orb-sol-ring",
            SetId = "set-c21",
            Name = "Sol Ring",
            SetCode = "C21",
            SetName = "Commander 2021",
            CollectorNumber = "263",
            ImageUri = "https://cards.scryfall.io/normal/front/4/c/4cbc6901-6a4a-4d0a-83ea-7eefa3b35021.jpg",
            ManaCost = "{1}",
            TypeLine = "Artifact",
            OracleText = "{T}: Add {C}{C}.",
            Rarity = "uncommon",
            PricesUsd = 1.50m
        },
        new Card
        {
            Id = "e3285e6b-3e79-4d7c-bf96-d920f973b122",
            OracleId = "orb-lightning-bolt",
            SetId = "set-2xm",
            Name = "Lightning Bolt",
            SetCode = "2XM",
            SetName = "Double Masters",
            CollectorNumber = "129",
            ImageUri = "https://cards.scryfall.io/normal/front/e/3/e3285e6b-3e79-4d7c-bf96-d920f973b122.jpg",
            ManaCost = "{R}",
            TypeLine = "Instant",
            OracleText = "Lightning Bolt deals 3 damage to any target.",
            Rarity = "uncommon",
            PricesUsd = 2.00m
        },
        new Card
        {
            Id = "ce30f926-bc06-46ee-9f35-0c32659a1b1c",
            OracleId = "orb-counterspell",
            SetId = "set-cmr",
            Name = "Counterspell",
            SetCode = "CMR",
            SetName = "Commander Legends",
            CollectorNumber = "395",
            ImageUri = "https://cards.scryfall.io/normal/front/c/e/ce30f926-bc06-46ee-9f35-0c32659a1b1c.jpg",
            ManaCost = "{U}{U}",
            TypeLine = "Instant",
            OracleText = "Counter target spell.",
            Rarity = "uncommon",
            PricesUsd = 1.25m
        },
        new Card
        {
            Id = "73542c66-eb3a-46e8-a8f6-5f02087b28cf",
            OracleId = "orb-llanowar-elves",
            SetId = "set-m19",
            Name = "Llanowar Elves",
            SetCode = "M19",
            SetName = "Core Set 2019",
            CollectorNumber = "314",
            ImageUri = "https://cards.scryfall.io/normal/front/7/3/73542c66-eb3a-46e8-a8f6-5f02087b28cf.jpg",
            ManaCost = "{G}",
            TypeLine = "Creature — Elf Druid",
            OracleText = "{T}: Add {G}.",
            Rarity = "common",
            PricesUsd = 0.25m
        },
        new Card
        {
            Id = "b8dcd4a6-5c0b-4e1e-8e1d-2e2e5c1d6a1e",
            OracleId = "orb-swords-to-plowshares",
            SetId = "set-cmr",
            Name = "Swords to Plowshares",
            SetCode = "CMR",
            SetName = "Commander Legends",
            CollectorNumber = "387",
            ImageUri = "https://cards.scryfall.io/normal/front/b/8/b8dcd4a6-5c0b-4e1e-8e1d-2e2e5c1d6a1e.jpg",
            ManaCost = "{W}",
            TypeLine = "Instant",
            OracleText = "Exile target creature. Its controller gains life equal to its power.",
            Rarity = "uncommon",
            PricesUsd = 3.50m
        },
        new Card
        {
            Id = "bd8fa327-dd41-4737-8f19-2cf5eb1f7c2e",
            OracleId = "orb-black-lotus",
            SetId = "set-lea",
            Name = "Black Lotus",
            SetCode = "LEA",
            SetName = "Limited Edition Alpha",
            CollectorNumber = "232",
            ImageUri = "https://cards.scryfall.io/normal/front/b/d/bd8fa327-dd41-4737-8f19-2cf5eb1f7c2e.jpg",
            ManaCost = "{0}",
            TypeLine = "Artifact",
            OracleText = "{T}, Sacrifice Black Lotus: Add three mana of any one color.",
            Rarity = "rare",
            PricesUsd = 500000.00m
        }
    ];

    private readonly Random _random = new();

    public async Task<ScanResult> RecognizeCardAsync(Stream imageStream, CancellationToken cancellationToken = default)
    {
        // Simulate processing delay
        await Task.Delay(500 + _random.Next(500), cancellationToken);

        // 90% success rate
        if (_random.NextDouble() < 0.1)
        {
            return ScanResult.Failed("Could not recognize card. Please try again with better lighting.");
        }

        var card = SampleCards[_random.Next(SampleCards.Length)];
        var confidence = 0.75f + (float)_random.NextDouble() * 0.24f; // 75-99% confidence

        return ScanResult.Matched(card, confidence, 10, TimeSpan.FromMilliseconds(500));
    }
}
