using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using Microsoft.EntityFrameworkCore;

namespace ClaudeDo.Data.Tests;

public sealed class TicketFieldsTests : IDisposable
{
    private readonly string _dbPath;

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

    public void Dispose()
    {
        foreach (var suffix in new[] { "", "-wal", "-shm" })
            try { File.Delete(_dbPath + suffix); } catch { }
    }

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

    [Fact]
    public async Task TicketRef_and_TicketProjectId_round_trip()
    {
        await using var ctx = NewContext();
        var list = new ListEntity { Id = "l1", Name = "L", DefaultCommitType = "feat", CreatedAt = DateTime.UtcNow };
        ctx.Lists.Add(list);
        ctx.ListConfigs.Add(new ListConfigEntity { ListId = "l1", TicketProjectId = 393 });
        ctx.Tasks.Add(new TaskEntity
        {
            Id = "t1", ListId = "l1", Title = "T", CreatedAt = DateTime.UtcNow, TicketRef = "bandel:1234",
        });
        await ctx.SaveChangesAsync();

        await using var read = NewContext();
        Assert.Equal("bandel:1234", (await read.Tasks.SingleAsync()).TicketRef);
        Assert.Equal(393, (await read.ListConfigs.SingleAsync()).TicketProjectId);
    }

    [Fact]
    public async Task TicketFields_default_to_null()
    {
        await using var ctx = NewContext();
        ctx.Lists.Add(new ListEntity { Id = "l1", Name = "L", DefaultCommitType = "feat", CreatedAt = DateTime.UtcNow });
        ctx.ListConfigs.Add(new ListConfigEntity { ListId = "l1" });
        ctx.Tasks.Add(new TaskEntity { Id = "t1", ListId = "l1", Title = "T", CreatedAt = DateTime.UtcNow });
        await ctx.SaveChangesAsync();

        await using var read = NewContext();
        Assert.Null((await read.Tasks.SingleAsync()).TicketRef);
        Assert.Null((await read.ListConfigs.SingleAsync()).TicketProjectId);
    }
}
