using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;

namespace AuthorBuddy.Web.Data;

public class AppDbContext : DbContext
{
    public DbSet<ProjectEntity> Projects => Set<ProjectEntity>();
    public DbSet<SettingEntity> Settings => Set<SettingEntity>();
    public DbSet<DocumentMetaEntity> DocumentMeta => Set<DocumentMetaEntity>();
    public DbSet<UserEntity> Users => Set<UserEntity>();

    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ProjectEntity>(entity =>
        {
            entity.ToTable("Projects");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Name).IsRequired();
            entity.Property(e => e.RootPath).IsRequired();
            entity.Property(e => e.LastOpened);
        });

        modelBuilder.Entity<SettingEntity>(entity =>
        {
            entity.ToTable("Settings");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Key).HasColumnName("key").IsRequired();
            entity.Property(e => e.Value).HasColumnName("value").IsRequired();
            entity.Property(e => e.UserId).HasColumnName("user_id");
            entity.Property(e => e.ProjectId).HasColumnName("project_id");
        });

        modelBuilder.Entity<UserEntity>(entity =>
        {
            entity.ToTable("Users");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.Name).HasColumnName("name").IsRequired();
            entity.Property(e => e.IsCurrent).HasColumnName("is_current");
        });

        modelBuilder.Entity<DocumentMetaEntity>(entity =>
        {
            entity.ToTable("DocumentMeta");
            entity.HasKey(e => e.Id);
            entity.Property(e => e.FilePath).HasColumnName("file_path").IsRequired();
            entity.Property(e => e.LastModified).HasColumnName("last_modified");
            entity.Property(e => e.FileType).HasColumnName("file_type");
            entity.Property(e => e.ProjectId).HasColumnName("project_id");
        });
    }

    public static void SetupVecExtension(string connectionString)
    {
        using var connection = new SqliteConnection(connectionString);
        connection.Open();
        connection.EnableExtensions(true);

        try
        {
            connection.LoadExtension("vec0");
            using var testCmd = connection.CreateCommand();
            testCmd.CommandText = "SELECT vec_version();";
            var version = testCmd.ExecuteScalar();
            Console.WriteLine($"sqlite-vec Version: {version}");
        }
        catch (SqliteException ex)
        {
            Console.WriteLine($"Fehler beim Laden von vec0: {ex.Message}");
        }

        using var createVecCmd = connection.CreateCommand();
        createVecCmd.CommandText = @"
            CREATE VIRTUAL TABLE IF NOT EXISTS vec_documents USING vec0(
                embedding float[1024]
            );";
        createVecCmd.ExecuteNonQuery();
    }
}
