using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels.Islands;
using Microsoft.EntityFrameworkCore;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;

namespace ClaudeDo.Ui.Tests.ViewModels;

public class DetailsIslandReviewActionsTests : IDisposable
{
    private readonly string _dbPath;

    public DetailsIslandReviewActionsTests()
    {
        _dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_review_actions_test_{Guid.NewGuid():N}.db");
        using var ctx = NewContext();
        ctx.Database.EnsureCreated();
    }

    public void Dispose()
    {
        try { File.Delete(_dbPath); } catch { }
        try { File.Delete(_dbPath + "-wal"); } catch { }
        try { File.Delete(_dbPath + "-shm"); } catch { }
    }

    private ClaudeDoDbContext NewContext()
    {
        var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
            .UseSqlite($"Data Source={_dbPath}")
            .Options;
        return new ClaudeDoDbContext(opts);
    }

    private sealed class TestDbFactory : IDbContextFactory<ClaudeDoDbContext>
    {
        private readonly Func<ClaudeDoDbContext> _create;
        public TestDbFactory(Func<ClaudeDoDbContext> create) => _create = create;
        public ClaudeDoDbContext CreateDbContext() => _create();
    }

    private sealed class NullServiceProvider : IServiceProvider
    {
        public object? GetService(Type serviceType) => null;
    }

    private sealed class StubNotesApi : ClaudeDo.Ui.Services.Interfaces.INotesApi
    {
        public Task<List<DailyNoteDto>> ListAsync(DateOnly day) =>
            Task.FromResult(new List<DailyNoteDto>());
        public Task<DailyNoteDto?> AddAsync(DateOnly day, string text) =>
            Task.FromResult<DailyNoteDto?>(null);
        public Task UpdateAsync(string id, string text) => Task.CompletedTask;
        public Task DeleteAsync(string id) => Task.CompletedTask;
    }

    private sealed class RecordingWorkerClient : StubWorkerClient
    {
        public override bool IsConnected => true;
        public string? ParkedTaskId;
        public override Task RejectReviewToIdleAsync(string taskId)
        {
            ParkedTaskId = taskId;
            return Task.CompletedTask;
        }
    }

    private DetailsIslandViewModel BuildVm(StubWorkerClient worker)
    {
        var factory = new TestDbFactory(NewContext);
        return new DetailsIslandViewModel(factory, worker, new NullServiceProvider(), new StubNotesApi(), new ClaudeDo.Ui.Services.MergeCoordinator());
    }

    [Fact]
    public async Task ParkReview_CallsRejectReviewToIdle_ForTheBoundTask()
    {
        var worker = new RecordingWorkerClient();
        var vm = BuildVm(worker);
        vm.Bind(new TaskRowViewModel { Id = "task-park-1", Status = TaskStatus.WaitingForReview });

        await vm.ParkReviewCommand.ExecuteAsync(null);

        Assert.Equal("task-park-1", worker.ParkedTaskId);
    }

    [Fact]
    public void SendBack_IsDisabledUntilFeedbackIsEntered()
    {
        var vm = BuildVm(new RecordingWorkerClient());
        vm.Bind(new TaskRowViewModel { Id = "task-fb-1", Status = TaskStatus.WaitingForReview });

        Assert.False(vm.RejectReviewCommand.CanExecute(null));

        vm.ReviewFeedback = "tighten the error handling";
        Assert.True(vm.RejectReviewCommand.CanExecute(null));

        vm.ReviewFeedback = "   ";
        Assert.False(vm.RejectReviewCommand.CanExecute(null));
    }
}
