using Microsoft.EntityFrameworkCore;
using RacePlannerApi.Models;

namespace RacePlannerApi.Data;

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

    public DbSet<User> Users { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<Registration> Registrations { get; set; }
    public DbSet<Payment> Payments { get; set; }
    public DbSet<Announcement> Announcements { get; set; }

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

        // User configurations
        modelBuilder.Entity<User>(entity =>
        {
            entity.HasIndex(u => u.Email).IsUnique();
            entity.Property(u => u.Role).HasConversion<string>();
        });

        // Event configurations
        modelBuilder.Entity<Event>(entity =>
        {
            entity.HasIndex(e => e.EventDate);
            entity.HasIndex(e => e.Status);
            entity.HasIndex(e => e.Category);
            entity.Property(e => e.Status).HasConversion<string>();
            entity.Property(e => e.Tags).HasColumnType("text[]");
            
            entity.HasOne(e => e.Organizer)
                .WithMany(u => u.OrganizedEvents)
                .HasForeignKey(e => e.OrganizerId)
                .OnDelete(DeleteBehavior.Restrict);
        });

        // Registration configurations
        modelBuilder.Entity<Registration>(entity =>
        {
            entity.HasIndex(r => new { r.EventId, r.ParticipantId }).IsUnique();
            entity.HasIndex(r => r.Status);
            entity.Property(r => r.Status).HasConversion<string>();
            
            entity.HasOne(r => r.Event)
                .WithMany(e => e.Registrations)
                .HasForeignKey(r => r.EventId)
                .OnDelete(DeleteBehavior.Cascade);
            
            entity.HasOne(r => r.Participant)
                .WithMany(u => u.Registrations)
                .HasForeignKey(r => r.ParticipantId)
                .OnDelete(DeleteBehavior.Restrict);
        });

        // Payment configurations
        modelBuilder.Entity<Payment>(entity =>
        {
            entity.HasIndex(p => p.RegistrationId);
            entity.HasIndex(p => p.PaymentDate);
            entity.Property(p => p.Method).HasConversion<string>();
            entity.Property(p => p.Amount).HasPrecision(18, 2); // For decimal Amount
            
            entity.HasOne(p => p.Registration)
                .WithMany(r => r.Payments)
                .HasForeignKey(p => p.RegistrationId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        // Announcement configurations
        modelBuilder.Entity<Announcement>(entity =>
        {
            entity.HasIndex(a => a.EventId);
            entity.HasIndex(a => a.CreatedAt);
            
            entity.HasOne(a => a.Event)
                .WithMany(e => e.Announcements)
                .HasForeignKey(a => a.EventId)
                .OnDelete(DeleteBehavior.Cascade);
            
            entity.HasOne(a => a.Author)
                .WithMany()
                .HasForeignKey(a => a.AuthorId)
                .OnDelete(DeleteBehavior.Restrict);
        });
    }
}