using Microsoft.EntityFrameworkCore;
using OTSSignsOrchestrator.Server.Data.Entities;

namespace OTSSignsOrchestrator.Server.Data;

public class OrchestratorDbContext : DbContext
{
    public OrchestratorDbContext(DbContextOptions<OrchestratorDbContext> options)
        : base(options) { }

    public DbSet<Customer> Customers => Set<Customer>();
    public DbSet<Instance> Instances => Set<Instance>();
    public DbSet<Job> Jobs => Set<Job>();
    public DbSet<JobStep> JobSteps => Set<JobStep>();
    public DbSet<HealthEvent> HealthEvents => Set<HealthEvent>();
    public DbSet<AuditLog> AuditLogs => Set<AuditLog>();
    public DbSet<StripeEvent> StripeEvents => Set<StripeEvent>();
    public DbSet<ScreenSnapshot> ScreenSnapshots => Set<ScreenSnapshot>();
    public DbSet<OauthAppRegistry> OauthAppRegistries => Set<OauthAppRegistry>();
    public DbSet<AuthentikMetrics> AuthentikMetrics => Set<AuthentikMetrics>();
    public DbSet<Operator> Operators => Set<Operator>();
    public DbSet<RefreshToken> RefreshTokens => Set<RefreshToken>();
    public DbSet<ByoiConfig> ByoiConfigs => Set<ByoiConfig>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // ── Snake-case naming convention ─────────────────────────────────
        foreach (var entity in modelBuilder.Model.GetEntityTypes())
        {
            entity.SetTableName(ToSnakeCase(entity.GetTableName()!));

            foreach (var property in entity.GetProperties())
                property.SetColumnName(ToSnakeCase(property.GetColumnName()));

            foreach (var key in entity.GetKeys())
                key.SetName(ToSnakeCase(key.GetName()!));

            foreach (var fk in entity.GetForeignKeys())
                fk.SetConstraintName(ToSnakeCase(fk.GetConstraintName()!));

            foreach (var index in entity.GetIndexes())
                index.SetDatabaseName(ToSnakeCase(index.GetDatabaseName()!));
        }

        // ── Customer ────────────────────────────────────────────────────
        modelBuilder.Entity<Customer>(e =>
        {
            e.HasKey(c => c.Id);
            e.Property(c => c.Abbreviation).HasMaxLength(8);
            e.Property(c => c.Plan).HasConversion<string>();
            e.Property(c => c.Status).HasConversion<string>();
            e.Property(c => c.FailedPaymentCount).HasDefaultValue(0);
            e.HasIndex(c => c.Abbreviation).IsUnique();
            e.HasIndex(c => c.StripeCustomerId).IsUnique();
        });

        // ── Instance ────────────────────────────────────────────────────
        modelBuilder.Entity<Instance>(e =>
        {
            e.HasKey(i => i.Id);
            e.Property(i => i.HealthStatus).HasConversion<string>();
            e.HasIndex(i => i.CustomerId);
            e.HasIndex(i => i.DockerStackName).IsUnique();
            e.HasOne(i => i.Customer)
                .WithMany(c => c.Instances)
                .HasForeignKey(i => i.CustomerId);
        });

        // ── Job ─────────────────────────────────────────────────────────
        modelBuilder.Entity<Job>(e =>
        {
            e.HasKey(j => j.Id);
            e.Property(j => j.Status).HasConversion<string>();
            e.Property(j => j.Parameters).HasColumnType("text");
            e.HasIndex(j => j.CustomerId);
            e.HasOne(j => j.Customer)
                .WithMany(c => c.Jobs)
                .HasForeignKey(j => j.CustomerId);
        });

        // ── JobStep ─────────────────────────────────────────────────────
        modelBuilder.Entity<JobStep>(e =>
        {
            e.HasKey(s => s.Id);
            e.Property(s => s.Status).HasConversion<string>();
            e.Property(s => s.LogOutput).HasColumnType("text");
            e.HasIndex(s => s.JobId);
            e.HasOne(s => s.Job)
                .WithMany(j => j.Steps)
                .HasForeignKey(s => s.JobId);
        });

        // ── HealthEvent ─────────────────────────────────────────────────
        modelBuilder.Entity<HealthEvent>(e =>
        {
            e.HasKey(h => h.Id);
            e.Property(h => h.Status).HasConversion<string>();
            e.HasIndex(h => h.InstanceId);
            e.HasOne(h => h.Instance)
                .WithMany(i => i.HealthEvents)
                .HasForeignKey(h => h.InstanceId);
        });

        // ── AuditLog ────────────────────────────────────────────────────
        modelBuilder.Entity<AuditLog>(e =>
        {
            e.HasKey(a => a.Id);
            e.Property(a => a.Detail).HasColumnType("text");
            e.HasIndex(a => a.InstanceId);
            e.HasIndex(a => a.OccurredAt);
        });

        // ── StripeEvent ─────────────────────────────────────────────────
        modelBuilder.Entity<StripeEvent>(e =>
        {
            e.HasKey(s => s.StripeEventId);
            e.Property(s => s.Payload).HasColumnType("text");
        });

        // ── ScreenSnapshot ──────────────────────────────────────────────
        modelBuilder.Entity<ScreenSnapshot>(e =>
        {
            e.HasKey(s => s.Id);
            e.HasIndex(s => s.InstanceId);
            e.HasOne(s => s.Instance)
                .WithMany(i => i.ScreenSnapshots)
                .HasForeignKey(s => s.InstanceId);
        });

        // ── OauthAppRegistry ────────────────────────────────────────────
        modelBuilder.Entity<OauthAppRegistry>(e =>
        {
            e.HasKey(o => o.Id);
            e.HasIndex(o => o.InstanceId);
            e.HasIndex(o => o.ClientId).IsUnique();
            e.HasOne(o => o.Instance)
                .WithMany(i => i.OauthAppRegistries)
                .HasForeignKey(o => o.InstanceId);
        });

        // ── AuthentikMetrics ────────────────────────────────────────────
        modelBuilder.Entity<AuthentikMetrics>(e =>
        {
            e.HasKey(a => a.Id);
            e.Property(a => a.Status).HasConversion<string>();
        });

        // ── Operator ────────────────────────────────────────────────────
        modelBuilder.Entity<Operator>(e =>
        {
            e.HasKey(o => o.Id);
            e.Property(o => o.Role).HasConversion<string>();
            e.HasIndex(o => o.Email).IsUnique();
        });

        // ── RefreshToken ────────────────────────────────────────────────
        modelBuilder.Entity<RefreshToken>(e =>
        {
            e.HasKey(r => r.Id);
            e.HasIndex(r => r.Token).IsUnique();
            e.HasIndex(r => r.OperatorId);
            e.HasOne(r => r.Operator)
                .WithMany(o => o.RefreshTokens)
                .HasForeignKey(r => r.OperatorId);
        });

        // ── ByoiConfig ──────────────────────────────────────────────────
        modelBuilder.Entity<ByoiConfig>(e =>
        {
            e.HasKey(b => b.Id);
            e.Property(b => b.CertPem).HasColumnType("text");
            e.HasIndex(b => b.InstanceId);
            e.HasIndex(b => b.Slug).IsUnique();
            e.HasOne(b => b.Instance)
                .WithMany(i => i.ByoiConfigs)
                .HasForeignKey(b => b.InstanceId);
        });
    }

    private static string ToSnakeCase(string name)
    {
        return string.Concat(
            name.Select((c, i) =>
                i > 0 && char.IsUpper(c) && !char.IsUpper(name[i - 1])
                    ? "_" + char.ToLowerInvariant(c)
                    : char.ToLowerInvariant(c).ToString()));
    }
}
