using HotelPosApi.Models;
using Microsoft.EntityFrameworkCore;

namespace HotelPosApi.Data;

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

    public DbSet<User> Users => Set<User>();
    public DbSet<Booking> Bookings => Set<Booking>();
    public DbSet<BookingAttribute> BookingAttributes => Set<BookingAttribute>();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<User>(e =>
        {
            e.HasIndex(x => x.Username).IsUnique();
            e.Property(x => x.Username).HasMaxLength(64).IsRequired();
            e.Property(x => x.PasswordHash).HasMaxLength(500).IsRequired();
            e.Property(x => x.Role).HasMaxLength(32).IsRequired();
        });

        modelBuilder.Entity<Booking>(e =>
        {
            e.ToTable("bookings");
            e.HasIndex(x => x.BookingCode).IsUnique();
            e.HasIndex(x => x.PricingId);
            e.Property(x => x.PricingId).HasColumnName("PricingID");
            e.Property(x => x.BookingCode).HasMaxLength(32).IsRequired();
            e.Property(x => x.GuestName).HasMaxLength(200).IsRequired();
            e.Property(x => x.Phone).HasMaxLength(50);
            e.Property(x => x.Email).HasMaxLength(120);
            e.Property(x => x.Ic).HasMaxLength(64);
            e.Property(x => x.Nationality).HasMaxLength(40).IsRequired();
            e.Property(x => x.PackageType).HasMaxLength(80);
            e.Property(x => x.Course).HasMaxLength(120);
            e.Property(x => x.Room).HasMaxLength(32).IsRequired();
            e.Property(x => x.PaxJson).HasColumnType("longtext").IsRequired();
            e.Property(x => x.AddonsJson).HasColumnType("longtext").IsRequired();
            e.Property(x => x.FeesJson).HasColumnType("longtext");
            e.Property(x => x.AgentCode).HasMaxLength(64);
            e.Property(x => x.AgentId).HasMaxLength(64);
            e.Property(x => x.PayMethod).HasMaxLength(32).IsRequired();
            e.Property(x => x.Remarks).HasMaxLength(500);
            e.Property(x => x.Status).HasMaxLength(32).IsRequired();
            e.Property(x => x.TotalAmount).HasPrecision(12, 2);
            e.Property(x => x.AmountPaid).HasPrecision(12, 2);
            e.HasMany(x => x.Attributes)
                .WithOne(x => x.Booking)
                .HasForeignKey(x => x.BookingId)
                .OnDelete(DeleteBehavior.Cascade);
        });

        modelBuilder.Entity<BookingAttribute>(e =>
        {
            e.ToTable("bookingattributes");
            e.HasKey(x => x.BookingAttributeId);
            e.HasIndex(x => new { x.BookingId, x.AttributeKey }).IsUnique();
            e.HasIndex(x => x.PricingId);
            e.Property(x => x.BookingAttributeId).HasColumnName("BookingAttributeID");
            e.Property(x => x.BookingId).HasColumnName("BookingID");
            e.Property(x => x.PricingId).HasColumnName("PricingID");
            e.Property(x => x.AttributeKey).HasMaxLength(80).IsRequired();
            e.Property(x => x.AttributeValue).HasColumnType("longtext");
        });
    }
}
