using Rag.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace Rag.Data;

public sealed class RagDbContext : DbContext
{
    public const string SchemaName = MigrationConstants.SchemaName;
    public const string MigrationTableName = MigrationConstants.MigrationTableName;

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

    public DbSet<RagDocumentEntity> RagDocuments => Set<RagDocumentEntity>();
    public DbSet<RagChunkEntity> RagChunks => Set<RagChunkEntity>();
    public DbSet<RagEmbeddingCacheEntity> RagEmbeddingCache => Set<RagEmbeddingCacheEntity>();
    public DbSet<RagChatCompletionCacheEntity> RagChatCompletionCache => Set<RagChatCompletionCacheEntity>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);
        // Configure migration history table to use schema-qualified name: [rag].[_Migrations]
        optionsBuilder.UseSqlServer(x => x.MigrationsHistoryTable(MigrationTableName, SchemaName));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultSchema(SchemaName);

        modelBuilder.Entity<RagDocumentEntity>(entity =>
        {
            entity.ToTable("Documents");
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Id).HasMaxLength(64);
            entity.Property(x => x.DocumentType).HasMaxLength(80).IsRequired();
            entity.Property(x => x.Title).HasMaxLength(300).IsRequired();
            entity.Property(x => x.SourceUrl).HasMaxLength(1200);
            entity.Property(x => x.RawText).IsRequired();
            entity.Property(x => x.TextHash).HasMaxLength(64).IsRequired();
            entity.Property(x => x.MetadataJson).HasDefaultValue("{}").IsRequired();
            entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()");
            entity.HasIndex(x => x.TextHash);
            entity.HasIndex(x => x.DocumentType);
        });

        modelBuilder.Entity<RagChunkEntity>(entity =>
        {
            entity.ToTable("Chunks");
            entity.HasKey(x => x.Id);
            entity.Property(x => x.Id).HasMaxLength(64);
            entity.Property(x => x.DocumentId).HasMaxLength(64).IsRequired();
            entity.Property(x => x.Text).IsRequired();
            entity.Property(x => x.Embedding).IsRequired();
            entity.HasOne(x => x.Document)
                .WithMany(x => x.Chunks)
                .HasForeignKey(x => x.DocumentId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<RagEmbeddingCacheEntity>(entity =>
        {
            entity.ToTable("EmbeddingCache");
            entity.HasKey(x => x.CacheKey);
            entity.Property(x => x.CacheKey).HasMaxLength(64);
            entity.Property(x => x.Model).HasMaxLength(120).IsRequired();
            entity.Property(x => x.TextHash).HasMaxLength(64).IsRequired();
            entity.Property(x => x.Vector).IsRequired();
            entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()");
            entity.HasIndex(x => x.TextHash);
        });

        modelBuilder.Entity<RagChatCompletionCacheEntity>(entity =>
        {
            entity.ToTable("ChatCompletionCache");
            entity.HasKey(x => x.CacheKey);
            entity.Property(x => x.CacheKey).HasMaxLength(64);
            entity.Property(x => x.Model).HasMaxLength(120).IsRequired();
            entity.Property(x => x.Temperature).HasColumnType("decimal(4,2)");
            entity.Property(x => x.ResponseText).IsRequired();
            entity.Property(x => x.CreatedAt).HasDefaultValueSql("SYSUTCDATETIME()");
        });
    }
}
