using Backend.Domain;
using Microsoft.EntityFrameworkCore;

namespace Backend.Data;

public sealed class AwardsDbContext(DbContextOptions<AwardsDbContext> options) : DbContext(options)
{
    public DbSet<Season> Seasons => Set<Season>();
    public DbSet<Category> Categories => Set<Category>();
    public DbSet<Candidate> Candidates => Set<Candidate>();
    public DbSet<AwardResult> Results => Set<AwardResult>();
    public DbSet<Nomination> Nominations => Set<Nomination>();
    public DbSet<VoteBallot> VoteBallots => Set<VoteBallot>();
    public DbSet<VoteEntry> VoteEntries => Set<VoteEntry>();
    public DbSet<UserSession> UserSessions => Set<UserSession>();
    public DbSet<RiskFlag> RiskFlags => Set<RiskFlag>();
    public DbSet<AdminAuditEntry> AdminAuditEntries => Set<AdminAuditEntry>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Season>(entity =>
        {
            entity.HasIndex(item => item.Year).IsUnique();
            entity.Property(item => item.Name).HasMaxLength(160);
            entity.Property(item => item.CurrentPhase).HasMaxLength(60);
        });

        modelBuilder.Entity<Category>(entity =>
        {
            entity.HasIndex(item => new { item.SeasonId, item.Slug }).IsUnique();
            entity.Property(item => item.GroupName).HasMaxLength(80);
            entity.Property(item => item.Name).HasMaxLength(120);
            entity.Property(item => item.Description).HasMaxLength(400);
        });

        modelBuilder.Entity<Candidate>(entity =>
        {
            entity.Property(item => item.DisplayName).HasMaxLength(120);
            entity.Property(item => item.ChannelSlug).HasMaxLength(120);
            entity.Property(item => item.Platform).HasMaxLength(40);
        });

        modelBuilder.Entity<Nomination>(entity =>
        {
            entity.Property(item => item.SubmittedByTwitchId).HasMaxLength(120);
            entity.Property(item => item.CandidateText).HasMaxLength(120);
        });

        modelBuilder.Entity<VoteBallot>(entity =>
        {
            entity.Property(item => item.SubmittedByTwitchId).HasMaxLength(120);
            entity.Property(item => item.Status).HasMaxLength(30);
        });

        modelBuilder.Entity<AwardResult>(entity =>
        {
            entity.Property(item => item.CategoryName).HasMaxLength(120);
        });

        modelBuilder.Entity<UserSession>(entity =>
        {
            entity.HasIndex(item => item.SessionToken).IsUnique();
            entity.Property(item => item.SessionToken).HasMaxLength(120);
            entity.Property(item => item.TwitchUserId).HasMaxLength(120);
            entity.Property(item => item.DisplayName).HasMaxLength(120);
            entity.Property(item => item.Role).HasMaxLength(40);
            entity.Property(item => item.CreatedFromIp).HasMaxLength(80);
            entity.Property(item => item.UserAgent).HasMaxLength(400);
        });

        modelBuilder.Entity<RiskFlag>(entity =>
        {
            entity.Property(item => item.TwitchUserId).HasMaxLength(120);
            entity.Property(item => item.Source).HasMaxLength(80);
            entity.Property(item => item.Type).HasMaxLength(80);
            entity.Property(item => item.Severity).HasMaxLength(20);
            entity.Property(item => item.Status).HasMaxLength(20);
            entity.Property(item => item.Summary).HasMaxLength(240);
            entity.Property(item => item.CreatedFromIp).HasMaxLength(80);
            entity.Property(item => item.UserAgent).HasMaxLength(400);
            entity.Property(item => item.ReviewedByTwitchId).HasMaxLength(120);
        });

        modelBuilder.Entity<AdminAuditEntry>(entity =>
        {
            entity.Property(item => item.AdminTwitchUserId).HasMaxLength(120);
            entity.Property(item => item.ActionType).HasMaxLength(80);
            entity.Property(item => item.EntityType).HasMaxLength(80);
            entity.Property(item => item.EntityId).HasMaxLength(120);
            entity.Property(item => item.Summary).HasMaxLength(240);
        });

        SeedData.Apply(modelBuilder);
    }
}
