using Microsoft.AspNetCore.Mvc;
using SVSim.Database.Enums;
using SVSim.EmulatedEntrypoint.Models.Dtos;
using SVSim.Database.Repositories.Globals;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Common;
using SVSim.EmulatedEntrypoint.Models.Dtos.Requests.Practice;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Deck;
using SVSim.EmulatedEntrypoint.Models.Dtos.Responses.Practice;
using SVSim.EmulatedEntrypoint.Services;

namespace SVSim.EmulatedEntrypoint.Controllers;

public class PracticeController : SVSimController
{
    private readonly IGlobalsRepository _globalsRepository;
    private readonly IMissionProgressService _missionProgress;
    private readonly IDeckListBuilder _deckListBuilder;

    public PracticeController(
        IGlobalsRepository globalsRepository,
        IMissionProgressService missionProgress,
        IDeckListBuilder deckListBuilder)
    {
        _globalsRepository = globalsRepository;
        _missionProgress = missionProgress;
        _deckListBuilder = deckListBuilder;
    }

    /// <summary>
    /// /practice/info — returns the AI opponent catalog. Response data is a JSON array
    /// directly (not wrapped in an object), per spec. Backed by PracticeOpponents table,
    /// seeded by SVSim.Bootstrap from seeds/practice-opponents.json.
    /// </summary>
    [HttpPost("info")]
    public async Task<List<PracticeOpponent>> Info(BaseRequest request)
    {
        var rows = await _globalsRepository.GetPracticeOpponents();
        return rows.Select(e => new PracticeOpponent
        {
            PracticeId = e.PracticeId,
            TextId = e.TextId,
            ClassId = e.ClassId,
            CharaId = e.CharaId,
            DegreeId = e.DegreeId,
            AiDeckLevel = e.AiDeckLevel,
            AiLogicLevel = e.AiLogicLevel,
            AiMaxLife = e.AiMaxLife,
            Battle3dFieldId = e.Battle3dFieldId,
            IsMaintenance = e.IsMaintenance,
            IsCampaignPractice = e.IsCampaignPractice,
        }).ToList();
    }

    /// <summary>
    /// /practice/deck_list — same wire shape as /deck/info (the client parses both via
    /// DeckGroupListData), so it shares <see cref="IDeckListBuilder"/>. Always All-format per spec.
    /// Unlike /deck/info this is a deck *select* screen, so empty "New Deck" slots are NOT padded
    /// (padEmptySlots: false) — prod's practice capture returns the viewer's real decks unpadded,
    /// plus the 8 per-class default decks and per-class leader-skin settings. The builder loads
    /// decks via IDeckRepository (DeckCard.Card Included), so card_id_array carries real ids rather
    /// than the 40 zeros that NRE the client's SBattleLoad.InitPlayer.
    /// </summary>
    [HttpPost("deck_list")]
    public async Task<ActionResult<DeckListResponse>> DeckList(DeckFormatRequest request)
    {
        if (!TryGetViewerId(out long viewerId)) return Unauthorized();
        return await _deckListBuilder.BuildAsync(viewerId, Format.All, padEmptySlots: false);
    }

    /// <summary>
    /// /practice/start — server is essentially a no-op for practice. Spec: empty body
    /// response is fine; client tolerates missing mission_parameter.
    /// </summary>
    [HttpPost("start")]
    public Task<PracticeStartResponse> Start(BaseRequest request)
    {
        return Task.FromResult(new PracticeStartResponse());
    }

    /// <summary>
    /// /practice/finish — accept the recovery_data blob without validation; return zero
    /// XP / no rewards. Class XP bookkeeping is deferred until a per-class XP store exists.
    /// </summary>
    [HttpPost("finish")]
    public async Task<PracticeFinishResponse> Finish(PracticeFinishRequest request)
    {
        // Mission/achievement progress hook. Catalog rows for practice_win achievements use
        // opponent NAMES (e.g. "practice_win:elite:arisa") — we only have numeric class_id /
        // difficulty here, so we emit numeric forms. Bridging numeric→name to match captured
        // catalog rows is a follow-up; the infrastructure is in place.
        if (request.IsWin == 1 && TryGetViewerId(out long viewerId))
        {
            await _missionProgress.RecordEventAsync(viewerId, new[]
            {
                "practice_win",
                $"practice_win:{request.Difficulty}",
                $"practice_win:{request.Difficulty}:{request.EnemyClassId}",
            });
        }

        return new PracticeFinishResponse
        {
            GetClassExperience = 0,
            ClassExperience = 0,
            ClassLevel = 1,
            AchievedInfo = new Dictionary<string, object>(),
            RewardList = new List<Models.Dtos.Common.Reward>()
        };
    }
}
