using System;
using System.IO;
using Microsoft.Data.Sqlite;
using Xunit;
using ytLive.Models;
using ytLive.Services;

namespace ytLive.Tests;

/// <summary>
/// The layout DB is a full rewrite on every save (DELETE all scenes/sources,
/// re-insert from memory). This guards the round trip: webcam configs the user
/// removes in the UI must not come back after a save + reload.
/// </summary>
public class LayoutStorePersistenceTests
{
    [Fact]
    public void Deleted_Webcam_Config_Does_Not_Return_After_Save_And_Reload()
    {
        var path = Path.Combine(Path.GetTempPath(), $"ytLlive-layout-{Guid.NewGuid():N}.db");
        try
        {
            using var store = new LayoutStore(path);
            var webcam = new Webcam { DeviceId = "real-device", Name = "Logitech" };
            var scene = new Scene { Name = "Starting" };
            scene.Elements.Add(new WebcamSceneConfig
            {
                WebcamId = webcam.Id,
                Name = webcam.Name,
                Width = 480,
                Height = 270,
            });
            store.Save(new[] { scene }, webcam);

            var reloaded = store.Load();
            var config = Assert.Single(reloaded[0].Elements);
            Assert.IsType<WebcamSceneConfig>(config);

            reloaded[0].Elements.RemoveAt(0);
            store.Save(reloaded, store.Webcam);

            var afterDelete = store.Load();
            Assert.Empty(afterDelete[0].Elements);
        }
        finally
        {
            SqliteConnection.ClearAllPools();
            try { File.Delete(path); } catch { /* best-effort cleanup */ }
        }
    }

    // Round-to-rect restore is persisted (schema v4): a Round webcam resized to a
    // square saves its pre-Round rect dims, and a reloaded config restores them on
    // toggle-back instead of staying square.
    [Fact]
    public void Pre_Round_Rect_Dims_Survive_Save_And_Reload()
    {
        var path = Path.Combine(Path.GetTempPath(), $"ytLlive-layout-{Guid.NewGuid():N}.db");
        try
        {
            using var store = new LayoutStore(path);
            var webcam = new Webcam { DeviceId = "real-device", Name = "Logitech" };
            var scene = new Scene { Name = "Starting" };
            scene.Elements.Add(new WebcamSceneConfig
            {
                WebcamId = webcam.Id,
                Name = webcam.Name,
                Width = 400,
                Height = 400,
                ClipShape = ClipShape.Round,
                RectWidth = 480,
                RectHeight = 270,
            });
            store.Save(new[] { scene }, webcam);

            var reloaded = store.Load();
            var config = Assert.IsType<WebcamSceneConfig>(Assert.Single(reloaded[0].Elements));

            config.ToggleClipShape();
            Assert.Equal(ClipShape.Traditional, config.ClipShape);
            Assert.Equal(480, config.Width);
            Assert.Equal(270, config.Height);
            Assert.Null(config.RectWidth);
            Assert.Null(config.RectHeight);
        }
        finally
        {
            SqliteConnection.ClearAllPools();
            try { File.Delete(path); } catch { /* best-effort cleanup */ }
        }
    }
}
