// EN: Entity type configuration for Appointment.
// VI: Cấu hình entity type cho Appointment.

using BookingService.Domain.AggregatesModel.AppointmentAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace BookingService.Infrastructure.EntityConfigurations;

/// <summary>
/// EN: Entity type configuration for Appointment aggregate root.
/// VI: Cấu hình entity type cho aggregate root Appointment.
/// </summary>
public class AppointmentEntityTypeConfiguration : IEntityTypeConfiguration<Appointment>
{
    public void Configure(EntityTypeBuilder<Appointment> builder)
    {
        builder.ToTable("appointments");
        
        builder.HasKey(a => a.Id);
        builder.Property(a => a.Id)
            .HasColumnName("id")
            .ValueGeneratedNever();

        builder.Property(a => a.ShopId)
            .HasField("_shopId")
            .HasColumnName("shop_id")
            .IsRequired();

        builder.Property(a => a.CustomerId)
            .HasField("_customerId")
            .HasColumnName("customer_id");

        builder.Property(a => a.StaffId)
            .HasField("_staffId")
            .HasColumnName("staff_id");

        builder.Property(a => a.ResourceId)
            .HasField("_resourceId")
            .HasColumnName("resource_id");

        builder.Property(a => a.ServiceId)
            .HasField("_serviceId")
            .HasColumnName("service_id")
            .IsRequired();

        builder.Property(a => a.StartTime)
            .HasField("_startTime")
            .HasColumnName("start_time")
            .IsRequired();

        builder.Property(a => a.EndTime)
            .HasField("_endTime")
            .HasColumnName("end_time")
            .IsRequired();

        builder.Property(a => a.Status)
            .HasField("_status")
            .HasColumnName("status")
            .HasMaxLength(50)
            .IsRequired();

        builder.Property(a => a.Notes)
            .HasField("_notes")
            .HasColumnName("notes")
            .HasMaxLength(1000);

        builder.Property(a => a.CreatedAt)
            .HasField("_createdAt")
            .HasColumnName("created_at")
            .IsRequired();

        // EN: Indexes / VI: Indexes
        builder.HasIndex(a => a.ShopId)
            .HasDatabaseName("ix_appointments_shop_id");

        builder.HasIndex(a => a.CustomerId)
            .HasDatabaseName("ix_appointments_customer_id");

        builder.HasIndex(a => a.StaffId)
            .HasDatabaseName("ix_appointments_staff_id");

        builder.HasIndex(a => a.StartTime)
            .HasDatabaseName("ix_appointments_start_time");
    }
}
