using Microsoft.EntityFrameworkCore;
using FutureMailAPI.Models;

namespace FutureMailAPI.Data
{
    public class FutureMailDbContext : DbContext
    {
        public FutureMailDbContext(DbContextOptions<FutureMailDbContext> options) : base(options)
        {
        }
        
        public DbSet<User> Users { get; set; }
        public DbSet<SentMail> SentMails { get; set; }
        public DbSet<ReceivedMail> ReceivedMails { get; set; }
        public DbSet<TimeCapsule> TimeCapsules { get; set; }
        public DbSet<OAuthClient> OAuthClients { get; set; }
        public DbSet<OAuthToken> OAuthTokens { get; set; }
        
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            
            // 配置User实体
            modelBuilder.Entity<User>(entity =>
            {
                entity.HasIndex(e => e.Email).IsUnique();
                entity.HasIndex(e => e.Username).IsUnique();
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
            
            // 配置SentMail实体
            modelBuilder.Entity<SentMail>(entity =>
            {
                entity.HasOne(e => e.Sender)
                    .WithMany(u => u.SentMails)
                    .HasForeignKey(e => e.SenderId)
                    .OnDelete(DeleteBehavior.Restrict);
                
                entity.HasOne<User>()
                    .WithMany()
                    .HasForeignKey(e => e.RecipientId)
                    .OnDelete(DeleteBehavior.SetNull);
                
                entity.Property(e => e.SentAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
            
            // 配置ReceivedMail实体
            modelBuilder.Entity<ReceivedMail>(entity =>
            {
                entity.HasOne(e => e.SentMail)
                    .WithMany()
                    .HasForeignKey(e => e.SentMailId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.HasOne<User>()
                    .WithMany()
                    .HasForeignKey(e => e.RecipientId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.Property(e => e.ReceivedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
            
            // 配置TimeCapsule实体
            modelBuilder.Entity<TimeCapsule>(entity =>
            {
                entity.HasOne(e => e.User)
                    .WithMany(u => u.TimeCapsules)
                    .HasForeignKey(e => e.UserId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.HasOne<SentMail>()
                    .WithMany()
                    .HasForeignKey(e => e.SentMailId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
            
            // 配置OAuthClient实体
            modelBuilder.Entity<OAuthClient>(entity =>
            {
                entity.HasIndex(e => e.ClientId).IsUnique();
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
                entity.Property(e => e.UpdatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
            
            // 配置OAuthToken实体
            modelBuilder.Entity<OAuthToken>(entity =>
            {
                entity.HasOne(e => e.User)
                    .WithMany()
                    .HasForeignKey(e => e.UserId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.HasOne(e => e.Client)
                    .WithMany(c => c.Tokens)
                    .HasForeignKey(e => e.ClientId)
                    .OnDelete(DeleteBehavior.Cascade);
                
                entity.HasIndex(e => e.AccessToken).IsUnique();
                entity.HasIndex(e => e.RefreshToken).IsUnique();
                entity.Property(e => e.CreatedAt).HasDefaultValueSql("CURRENT_TIMESTAMP");
            });
        }
    }
}