using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using SolutionErp.Domain.Office;

namespace SolutionErp.Infrastructure.Persistence.Configurations;

// EF Mig 36 G-O2 (S36) — Booking phòng họp.
// FK Restrict cho Room (preserve history) + Restrict User Drafter (denorm name).
// Index composite (RoomId, StartAt) cho range query overlap check + calendar fetch.
public class MeetingBookingConfiguration : IEntityTypeConfiguration<MeetingBooking>
{
    public void Configure(EntityTypeBuilder<MeetingBooking> e)
    {
        e.ToTable("MeetingBookings");

        e.Property(x => x.BookedByFullName).HasMaxLength(200).IsRequired();
        e.Property(x => x.Title).HasMaxLength(200).IsRequired();
        e.Property(x => x.Description).HasMaxLength(1000);
        e.Property(x => x.Note).HasMaxLength(500);
        e.Property(x => x.Status).HasConversion<int>();

        e.HasOne(x => x.Room)
            .WithMany()
            .HasForeignKey(x => x.RoomId)
            .OnDelete(DeleteBehavior.Restrict);

        e.HasMany(x => x.Attendees)
            .WithOne(a => a.Booking)
            .HasForeignKey(a => a.BookingId)
            .OnDelete(DeleteBehavior.Cascade);

        // Range query + overlap check (RoomId + StartAt)
        e.HasIndex(x => new { x.RoomId, x.StartAt });
        // BookedByUserId lookup "my bookings"
        e.HasIndex(x => x.BookedByUserId);
    }
}
