using Microsoft.EntityFrameworkCore;
using Npgsql;
using UniVerse.Domain.Entities;
using UniVerse.Domain.Enums;

namespace UniVerse.Infrastructure.Data;

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

    public DbSet<User> Users { get; set; } = null!;
    public DbSet<UserRoleAssignment> UserRoles { get; set; } = null!;
    public DbSet<StudentProfile> StudentProfiles { get; set; } = null!;
    public DbSet<TeacherProfile> TeacherProfiles { get; set; } = null!;
    public DbSet<Course> Courses { get; set; } = null!;
    public DbSet<Lecture> Lectures { get; set; } = null!;
    public DbSet<Location> Locations { get; set; } = null!;
    public DbSet<Tag> Tags { get; set; } = null!;
    public DbSet<CourseTag> CourseTags { get; set; } = null!;
    public DbSet<LectureEnrollment> LectureEnrollments { get; set; } = null!;
    public DbSet<Review> Reviews { get; set; } = null!;
    public DbSet<ReviewPromptSetting> ReviewPromptSettings { get; set; } = null!;
    public DbSet<Achievement> Achievements { get; set; } = null!;
    public DbSet<UserAchievement> UserAchievements { get; set; } = null!;
    public DbSet<CoinTransaction> CoinTransactions { get; set; } = null!;
    public DbSet<LevelThreshold> LevelThresholds { get; set; } = null!;
    public DbSet<UserNotification> UserNotifications { get; set; } = null!;
    public DbSet<RefreshToken> RefreshTokens { get; set; } = null!;

    static AppDbContext()
    {
        NpgsqlConnection.GlobalTypeMapper.MapEnum<UserRole>("user_role");
        NpgsqlConnection.GlobalTypeMapper.MapEnum<TagType>("tag_type");
        NpgsqlConnection.GlobalTypeMapper.MapEnum<ReviewLlmStatus>("review_llm_status");
        NpgsqlConnection.GlobalTypeMapper.MapEnum<ReviewSentiment>("review_sentiment");
        NpgsqlConnection.GlobalTypeMapper.MapEnum<CoinTransactionType>("coin_transaction_type");
    }

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

        modelBuilder.HasPostgresEnum<UserRole>("user_role");
        modelBuilder.HasPostgresEnum<TagType>("tag_type");
        modelBuilder.HasPostgresEnum<ReviewLlmStatus>("review_llm_status");
        modelBuilder.HasPostgresEnum<ReviewSentiment>("review_sentiment");
        modelBuilder.HasPostgresEnum<CoinTransactionType>("coin_transaction_type");

        modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
        
        // Basic PK setups if they weren't fully in assembly
        modelBuilder.Entity<CourseTag>().HasKey(ct => new { ct.CourseId, ct.TagId });
        modelBuilder.Entity<LectureEnrollment>().HasKey(le => new { le.LectureId, le.UserId });
        modelBuilder.Entity<UserAchievement>().HasKey(ua => new { ua.UserId, ua.AchievementId });

        foreach (var entity in modelBuilder.Model.GetEntityTypes())
        {
            entity.SetTableName(entity.GetTableName()?.ToLowerInvariant());
            foreach (var property in entity.GetProperties())
            {
                property.SetColumnName(string.Concat(property.Name.Select((x, i) => i > 0 && char.IsUpper(x) ? "_" + x.ToString() : x.ToString())).ToLowerInvariant());
            }
        }
    }
}
