using Domain.Entities.Crypto;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace Infrastructure.Persistence.Configurations.Crypto;

public sealed class CoinConfiguration : IEntityTypeConfiguration<Coin>
{
    public void Configure(EntityTypeBuilder<Coin> builder)
    {
        builder.HasIndex(x => x.Symbol).IsUnique();
        builder.HasIndex(x => x.IsActive);
        builder.HasIndex(x => x.IsWarning);
        builder.HasIndex(x => x.IsNew);
        builder.HasIndex(x => x.IsDelisted);
        builder.HasIndex(x => new { x.Symbol, x.IsActive });

        builder.HasMany(x => x.CoinCategoryMap).WithOne(x => x.Coin).HasForeignKey(x => x.CoinID).OnDelete(DeleteBehavior.Cascade);
        builder.HasMany(x => x.CoinMarket).WithOne(x => x.Coin).HasForeignKey(x => x.CoinID).OnDelete(DeleteBehavior.Cascade);

        builder.ToTable(nameof(Coin), t => t.HasComment("코인/토큰"));
        builder.HasKey(x => x.ID);
        builder.Property(x => x.ID).ValueGeneratedOnAdd().HasComment("PK");
        builder.Property(x => x.Symbol).HasMaxLength(30).IsRequired().HasComment("심볼 (BTC, ETH 등)");
        builder.Property(x => x.KorName).HasMaxLength(200).IsRequired().HasComment("한글 이름");
        builder.Property(x => x.EngName).HasMaxLength(200).IsRequired().HasComment("영문 이름");
        builder.Property(x => x.LogoImage).HasMaxLength(500).HasComment("로고 이미지");
        builder.Property(x => x.Description).HasColumnType("nvarchar(max)").HasComment("설명");
        builder.Property(x => x.ContractAddress).HasMaxLength(100).HasComment("컨트랙트 주소");
        builder.Property(x => x.WebsiteUrl).HasMaxLength(500).HasComment("홈페이지 URL");
        builder.Property(x => x.WhitepaperUrl).HasMaxLength(500).HasComment("백서 URL");
        builder.Property(x => x.TwitterUrl).HasMaxLength(500).HasComment("트위터 URL");
        builder.Property(x => x.TelegramUrl).HasMaxLength(500).HasComment("텔레그램 URL");
        builder.Property(x => x.IsActive).IsRequired().HasComment("사용 여부");
        builder.Property(x => x.IsWarning).IsRequired().HasComment("위험 경고");
        builder.Property(x => x.IsNew).IsRequired().HasComment("신규 상장");
        builder.Property(x => x.IsDelisted).IsRequired().HasComment("상장 폐지");
        builder.Property(x => x.IsFeatured).IsRequired().HasComment("주요 코인 (메인 노출)");
        builder.Property(x => x.DisplayOrder).IsRequired().HasComment("메인 노출 순서");
        builder.HasIndex(x => x.IsFeatured).HasDatabaseName("IX_Coin_IsFeatured");
        builder.HasIndex(x => x.DisplayOrder).HasDatabaseName("IX_Coin_DisplayOrder");
        builder.Property(x => x.UpdatedAt).HasComment("수정 일시");
        builder.Property(x => x.CreatedAt).IsRequired().HasComment("등록 일시");
    }
}