using Microsoft.EntityFrameworkCore;
using WarehouseAppSHIPS.Models;

namespace WarehouseAppSHIPS.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<AppUser> AppUsers { get; set; }
        public DbSet<Material> Materials { get; set; }
        public DbSet<InboundTransaction> InboundTransactions { get; set; }
        public DbSet<OutboundTransaction> OutboundTransactions { get; set; }
        public DbSet<MaterialRequest> MaterialRequests { get; set; }
        public DbSet<StockOpname> StockOpnames { get; set; }
        public DbSet<ProcurementRequest> ProcurementRequests { get; set; }
        public DbSet<MtcRequest> MtcRequests { get; set; }
        public DbSet<TransactionHistory> TransactionHistories { get; set; }
        public DbSet<AppNotification> AppNotifications { get; set; }

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

            modelBuilder.Entity<MaterialRequest>()
                .HasOne(m => m.Requester)
                .WithMany(u => u.RequestsMade)
                .HasForeignKey(m => m.RequesterId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<MaterialRequest>()
                .HasOne(m => m.Approver)
                .WithMany(u => u.RequestsApproved)
                .HasForeignKey(m => m.ApproverId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<InboundTransaction>()
                .HasOne(i => i.Receiver)
                .WithMany(u => u.InboundTransactions)
                .HasForeignKey(i => i.ReceiverId)
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<StockOpname>()
                .HasOne(s => s.Auditor)
                .WithMany(u => u.StockOpnames)
                .HasForeignKey(s => s.AuditorId)
                .OnDelete(DeleteBehavior.Restrict);
        }
    }
}