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>();
    public DbSet<ClipSubmission> ClipSubmissions => Set<ClipSubmission>();
    public DbSet<SiteSettings> SiteSettings => Set<SiteSettings>();
    public DbSet<TeamMember> TeamMembers => Set<TeamMember>();
    public DbSet<TeamRolePermission> TeamRolePermissions => Set<TeamRolePermission>();

    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.ShowStreamUrl).HasMaxLength(400);
            entity.Property(item => item.CurrentPhase).HasMaxLength(60);
        });

        modelBuilder.Entity<SiteSettings>(entity =>
        {
            entity.Property(item => item.HostDisplayName).HasMaxLength(120);
            entity.Property(item => item.HostTagline).HasMaxLength(160);
            entity.Property(item => item.NewsletterUrl).HasMaxLength(400);
            entity.Property(item => item.PrivacyEmail).HasMaxLength(160);
            entity.Property(item => item.PrivacyPolicyUpdatedBy).HasMaxLength(120);
            entity.Property(item => item.ImprintUrl).HasMaxLength(400);
            entity.Property(item => item.ContactUrl).HasMaxLength(400);
            entity.Property(item => item.SponsorsUrl).HasMaxLength(400);
            entity.Property(item => item.DemoLoginEmail).HasMaxLength(180);
            entity.Property(item => item.DemoLoginPasswordHash).HasMaxLength(120);
            entity.Property(item => item.DemoLoginPasswordSalt).HasMaxLength(80);
            entity.Property(item => item.DemoLoginTwitchUserId).HasMaxLength(120);
            entity.Property(item => item.DemoLoginDisplayName).HasMaxLength(120);
            entity.Property(item => item.TwitchClientId).HasMaxLength(120);
            entity.Property(item => item.TwitchClientSecret).HasMaxLength(180);
            entity.Property(item => item.TwitchRedirectUri).HasMaxLength(400);
            entity.Property(item => item.TwitchScope).HasMaxLength(300);
            entity.Property(item => item.MaintenanceTitle).HasMaxLength(120);
            entity.Property(item => item.MaintenanceMessage).HasMaxLength(600);
        });

        modelBuilder.Entity<TeamMember>(entity =>
        {
            entity.HasIndex(item => item.Login).IsUnique();
            entity.HasIndex(item => item.BoundTwitchUserId).IsUnique();
            entity.Property(item => item.Login).HasMaxLength(80);
            entity.Property(item => item.DisplayName).HasMaxLength(120);
            entity.Property(item => item.Role).HasMaxLength(40);
            entity.Property(item => item.PasswordHash).HasMaxLength(120);
            entity.Property(item => item.PasswordSalt).HasMaxLength(80);
            entity.Property(item => item.BoundTwitchUserId).HasMaxLength(120);
            entity.Property(item => item.BoundTwitchDisplayName).HasMaxLength(120);
            entity.Property(item => item.CreatedByTwitchId).HasMaxLength(120);
            entity.Property(item => item.UpdatedByTwitchId).HasMaxLength(120);
        });

        modelBuilder.Entity<TeamRolePermission>(entity =>
        {
            entity.HasIndex(item => item.Role).IsUnique();
            entity.Property(item => item.Role).HasMaxLength(40);
            entity.Property(item => item.UpdatedByTwitchId).HasMaxLength(120);
        });

        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);
            entity.Property(item => item.StreamUrl).HasMaxLength(300);
            entity.Property(item => item.Status).HasMaxLength(20);
            entity.Property(item => item.ReviewNote).HasMaxLength(500);
            entity.Property(item => item.ReviewedByTwitchId).HasMaxLength(120);
            entity.HasIndex(item => new { item.SeasonId, item.Status });
        });

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

        modelBuilder.Entity<AwardResult>(entity =>
        {
            entity.HasIndex(item => new { item.SeasonId, item.CategoryId }).IsUnique();
            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.ReviewNote).HasMaxLength(500);
            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);
            entity.Property(item => item.CreatedFromIp).HasMaxLength(80);
            entity.Property(item => item.UserAgent).HasMaxLength(400);
        });

        modelBuilder.Entity<ClipSubmission>(entity =>
        {
            entity.Property(item => item.SubmittedByTwitchId).HasMaxLength(120);
            entity.Property(item => item.ClipUrl).HasMaxLength(500);
            entity.Property(item => item.Title).HasMaxLength(200);
            entity.Property(item => item.Creator).HasMaxLength(120);
            entity.Property(item => item.Platform).HasMaxLength(40);
            entity.Property(item => item.Status).HasMaxLength(20);
            entity.Property(item => item.ReviewNote).HasMaxLength(500);
            entity.Property(item => item.ReviewedByTwitchId).HasMaxLength(120);
            entity.Property(item => item.CreatedFromIp).HasMaxLength(80);
            entity.HasIndex(item => new { item.SeasonId, item.Status });
            entity.HasIndex(item => item.CandidateId);
            entity.HasOne(item => item.Candidate)
                .WithMany()
                .HasForeignKey(item => item.CandidateId)
                .OnDelete(DeleteBehavior.SetNull);
        });

        SeedData.Apply(modelBuilder);
    }
}
