using IRaCIS.Core.Domain.Models;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace IRaCIS.Core.Infra.EFCore.EntityConfigration;

/// <summary>
/// 医学影像  数据量大，主键是 SeqId  ,Id 是自己算的，也可以作为Id，但是不是有序的，数据量大，会导致性能很差，所以是逻辑主键
/// </summary>

public class DicomStudyConfigration : IEntityTypeConfiguration<DicomStudy>
{
    public void Configure(EntityTypeBuilder<DicomStudy> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasMany(s => s.InstanceList).WithOne(se => se.DicomStudy).HasForeignKey(se => se.StudyId).HasPrincipalKey(st => st.Id);

        builder.HasMany(s => s.SeriesList).WithOne(se => se.DicomStudy).HasForeignKey(se => se.StudyId).HasPrincipalKey(st => st.Id);

        builder.HasMany(s => s.DicomStudyMonitorList).WithOne(sm => sm.DicomStudy).HasForeignKey(sm => sm.StudyId).HasPrincipalKey(se => se.Id);

        builder.HasMany(s => s.SubjectCriteriaEvaluationVisitStudyFilterList).WithOne(sm => sm.Study).HasForeignKey(sm => sm.StudyId).HasPrincipalKey(se => se.Id);
    }
}

public class DicomSeriesConfigration : IEntityTypeConfiguration<DicomSeries>
{
    public void Configure(EntityTypeBuilder<DicomSeries> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasMany(s => s.DicomInstanceList).WithOne(di => di.DicomSerie).HasForeignKey(t => t.SeriesId).HasPrincipalKey(se => se.Id);

        builder.HasMany(s => s.SubjectCriteriaEvaluationVisitStudyFilterList).WithOne(di => di.Series).HasForeignKey(t => t.SeriesId).HasPrincipalKey(se => se.Id);
        
    }
}

public class DicomInstanceConfigration : IEntityTypeConfiguration<DicomInstance>
{
    public void Configure(EntityTypeBuilder<DicomInstance> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasOne(e => e.DicomStudy).WithMany(c=>c.InstanceList).HasForeignKey(t=>t.StudyId).HasPrincipalKey(st => st.Id);

        builder.HasMany(s => s.ReadingTableAnswerRowInfoList).WithOne(di => di.Instance).HasForeignKey(t => t.InstanceId).HasPrincipalKey(se => se.Id);
        
    }
}

public class TaskStudyConfigration : IEntityTypeConfiguration<TaskStudy>
{
    public void Configure(EntityTypeBuilder<TaskStudy> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasMany(s => s.SeriesList).WithOne(se => se.TaskStudy).HasForeignKey(se => se.StudyId).HasPrincipalKey(st => st.Id);

    }
}

public class TaskSeriesConfigration : IEntityTypeConfiguration<TaskSeries>
{
    public void Configure(EntityTypeBuilder<TaskSeries> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasMany(s => s.InstanceList).WithOne(di => di.TaskSeries).HasForeignKey(t => t.SeriesId).HasPrincipalKey(se => se.Id);
    }
}

public class TaskInstanceConfigration : IEntityTypeConfiguration<TaskInstance>
{
    public void Configure(EntityTypeBuilder<TaskInstance> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasOne(e => e.TaskStudy).WithMany(t=>t.InstanceList).HasForeignKey(t => t.StudyId).HasPrincipalKey(st => st.Id);
    }
}


public class SCPStudyConfigration : IEntityTypeConfiguration<SCPStudy>
{
    public void Configure(EntityTypeBuilder<SCPStudy> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasMany(s => s.SeriesList).WithOne(se => se.SCPStudy).HasForeignKey(se => se.StudyId).HasPrincipalKey(st => st.Id);

    }
}

public class SCPSeriesConfigration : IEntityTypeConfiguration<SCPSeries>
{
    public void Configure(EntityTypeBuilder<SCPSeries> builder)
    {
        builder.HasKey(e => e.SeqId);
        builder.HasMany(s => s.SCPInstanceList).WithOne(di => di.SCPSeries).HasForeignKey(t => t.SeriesId).HasPrincipalKey(se => se.Id);
    }
}

public class SCPInstanceConfigration : IEntityTypeConfiguration<SCPInstance>
{
    public void Configure(EntityTypeBuilder<SCPInstance> builder)
    {
        builder.HasKey(e => e.SeqId);

        builder.HasOne(e => e.SCPStudy).WithMany(t => t.InstanceList).HasForeignKey(t => t.StudyId).HasPrincipalKey(st => st.Id);
    }
}


public class ReadingTableAnswerRowInfoConfigration : IEntityTypeConfiguration<ReadingTableAnswerRowInfo>
{
    public void Configure(EntityTypeBuilder<ReadingTableAnswerRowInfo> builder)
    {
       

        builder.HasOne(e => e.DicomSeries).WithMany().HasForeignKey(t => t.SeriesId).HasPrincipalKey(st => st.Id);

        //builder.HasOne(e => e.Instance).WithMany().HasForeignKey(t => t.InstanceId).HasPrincipalKey(st => st.Id);
    }
}




