using ColaFlow.Modules.Identity.Domain.Aggregates.Tenants;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Text.Json;

namespace ColaFlow.Modules.Identity.Infrastructure.Persistence.Configurations;

public class TenantConfiguration : IEntityTypeConfiguration<Tenant>
{
    public void Configure(EntityTypeBuilder<Tenant> builder)
    {
        builder.ToTable("tenants", "identity");

        // Primary Key
        builder.HasKey(t => t.Id);

        builder.Property(t => t.Id)
            .HasConversion(
                id => (Guid)id,
                value => TenantId.Create(value))
            .HasColumnName("id");

        // Value Object mappings
        builder.Property(t => t.Name)
            .HasConversion(
                name => name.Value,
                value => TenantName.Create(value))
            .HasMaxLength(100)
            .IsRequired()
            .HasColumnName("name");

        builder.Property(t => t.Slug)
            .HasConversion(
                slug => slug.Value,
                value => TenantSlug.Create(value))
            .HasMaxLength(50)
            .IsRequired()
            .HasColumnName("slug");

        builder.HasIndex(t => t.Slug)
            .IsUnique()
            .HasDatabaseName("ix_tenants_slug");

        // Enum mappings
        builder.Property(t => t.Status)
            .HasConversion<string>()
            .HasMaxLength(50)
            .IsRequired()
            .HasColumnName("status");

        builder.Property(t => t.Plan)
            .HasConversion<string>()
            .HasMaxLength(50)
            .IsRequired()
            .HasColumnName("plan");

        // SSO Configuration (stored as JSON)
        builder.Property(t => t.SsoConfig)
            .HasConversion(
                config => config != null ? JsonSerializer.Serialize(config, (JsonSerializerOptions?)null) : null,
                json => json != null ? JsonSerializer.Deserialize<SsoConfiguration>(json, (JsonSerializerOptions?)null) : null)
            .HasColumnType("jsonb")
            .HasColumnName("sso_config");

        // Timestamps
        builder.Property(t => t.CreatedAt)
            .IsRequired()
            .HasColumnName("created_at");

        builder.Property(t => t.UpdatedAt)
            .HasColumnName("updated_at");

        builder.Property(t => t.SuspendedAt)
            .HasColumnName("suspended_at");

        builder.Property(t => t.SuspensionReason)
            .HasMaxLength(500)
            .HasColumnName("suspension_reason");

        // Settings
        builder.Property(t => t.MaxUsers)
            .IsRequired()
            .HasColumnName("max_users");

        builder.Property(t => t.MaxProjects)
            .IsRequired()
            .HasColumnName("max_projects");

        builder.Property(t => t.MaxStorageGB)
            .IsRequired()
            .HasColumnName("max_storage_gb");

        // Ignore domain events (not stored in database)
        builder.Ignore(t => t.DomainEvents);
    }
}
