using ClaudeDo.Data;
using ClaudeDo.Data.Git;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.Config;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Runner;
using ClaudeDo.Worker.Tests.Infrastructure;
using Microsoft.Extensions.Logging.Abstractions;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
using Xunit;

namespace ClaudeDo.Worker.Tests.Runner;

/// Verifies ResolveConfigAsync's model → preset resolution reaches the CLI args: a full
/// model id must hit its alias's preset row (not the hardcoded unknown-model fallback), and
/// an unknown model must still start a run, falling back no harder than AppSettings.DefaultMaxTurns.
public sealed class ModelResolutionWireTests : IDisposable
{
    private readonly DbFixture _db = new();
    private readonly string _tempDir;
    private readonly WorkerConfig _cfg;

    public ModelResolutionWireTests()
    {
        _tempDir = Path.Combine(Path.GetTempPath(), $"cd_modelwire_{Guid.NewGuid():N}");
        Directory.CreateDirectory(_tempDir);
        _cfg = new WorkerConfig { SandboxRoot = _tempDir, LogRoot = _tempDir };
    }

    public void Dispose() { _db.Dispose(); try { Directory.Delete(_tempDir, true); } catch { } }

    private (TaskRunner Runner, IReadOnlyList<string> Args) BuildRunnerCapturingArgs(out Func<IReadOnlyList<string>> getArgs)
    {
        var dbFactory = _db.CreateFactory();
        var state = TaskStateServiceBuilder.Build(dbFactory).State;
        var wt = new WorktreeManager(new GitService(), dbFactory, _cfg, NullLogger<WorktreeManager>.Instance);
        IReadOnlyList<string>? captured = null;
        var fake = new FakeClaudeProcess((_, _, args, _, _) =>
        {
            captured = args;
            return Task.FromResult(new RunResult { ExitCode = 0, ResultMarkdown = "ok" });
        });
        var runner = new TaskRunner(fake, dbFactory, new HubBroadcaster(new CapturingHubContext()), wt,
            new ClaudeArgsBuilder(), _cfg, NullLogger<TaskRunner>.Instance, state, new TaskRunTokenRegistry(),
            new AttachmentStore(), new FakeSessionSkillSeeder(), new FakeTranscriptUsageReader());
        getArgs = () => captured!;
        return (runner, Array.Empty<string>());
    }

    private async Task SeedTaskAsync(string listModel)
    {
        using var ctx = _db.CreateContext();
        ctx.Lists.Add(new ListEntity { Id = "l1", Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
        ctx.ListConfigs.Add(new ListConfigEntity { ListId = "l1", Model = listModel });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = "t1", ListId = "l1", Title = "Task", Status = TaskStatus.Idle, CreatedAt = DateTime.UtcNow,
        });
        await ctx.SaveChangesAsync();
    }

    [Fact]
    public async Task Full_model_id_resolves_to_its_alias_preset_row()
    {
        await SeedTaskAsync(listModel: "claude-sonnet-4-6");
        var (runner, _) = BuildRunnerCapturingArgs(out var getArgs);

        using (var ctx = _db.CreateContext())
            await runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync("t1"))!, "slot-1", CancellationToken.None);

        var args = getArgs().ToList();
        var sonnetPreset = ModelPresets.For(ModelPresets.Defaults, "sonnet");

        Assert.Contains("--effort", args);
        Assert.Equal(sonnetPreset.Effort, args[args.IndexOf("--effort") + 1]);
        Assert.Contains("--max-turns", args);
        Assert.Equal(sonnetPreset.MaxTurns.ToString(), args[args.IndexOf("--max-turns") + 1]);
    }

    [Fact]
    public async Task Unknown_model_still_starts_a_run_and_uses_the_global_default_max_turns()
    {
        await SeedTaskAsync(listModel: "totally-unrecognized-model");
        var (runner, _) = BuildRunnerCapturingArgs(out var getArgs);

        using (var ctx = _db.CreateContext())
        {
            var settingsRepo = new AppSettingsRepository(ctx);
            await settingsRepo.UpdateAsync(new AppSettingsEntity { DefaultMaxTurns = 50 });
        }

        Exception? thrown = null;
        try
        {
            using var ctx = _db.CreateContext();
            await runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync("t1"))!, "slot-1", CancellationToken.None);
        }
        catch (Exception ex)
        {
            thrown = ex;
        }

        Assert.Null(thrown);
        var args = getArgs().ToList();
        Assert.Contains("--max-turns", args);
        Assert.Equal("50", args[args.IndexOf("--max-turns") + 1]);
    }
}
