import { BaseModel } from 'src/common/entities/base.entity';
import { GenomeRequestModel } from './genome-request.entity';
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

/**
 * 유전체 형질 상세 정보 (tb_genome_trait_detail)
 * 1개체당 35개 형질 → 35개 행으로 저장
 */
@Entity({ name: 'tb_genome_trait_detail' })
@Index('idx_genome_trait_cow_id', ['cowId'])
@Index('idx_genome_trait_name', ['traitName'])
@Index('idx_genome_trait_cow_trait', ['cowId', 'traitName'])
export class GenomeTraitDetailModel extends BaseModel {
  @PrimaryGeneratedColumn({
    name: 'pk_trait_detail_no',
    type: 'int',
    comment: '형질상세번호 PK',
  })
  pkTraitDetailNo: number;

  @Column({
    name: 'fk_request_no',
    type: 'int',
    nullable: true,
    comment: '의뢰번호 FK',
  })
  fkRequestNo: number;

  @Column({
    name: 'cow_id',
    type: 'varchar',
    length: 20,
    nullable: true,
    comment: '개체식별번호 (KOR)',
  })
  cowId: string;

  @Column({
    name: 'trait_name',
    type: 'varchar',
    length: 100,
    nullable: true,
    comment: '형질명 (예: "12개월령체중", "도체중", "등심단면적")',
  })
  traitName: string;

  @Column({
    name: 'trait_val',
    type: 'decimal',
    precision: 15,
    scale: 6,
    nullable: true,
    comment: '실측값',
  })
  traitVal: number;

  @Column({
    name: 'trait_ebv',
    type: 'decimal',
    precision: 15,
    scale: 6,
    nullable: true,
    comment: '표준화육종가 (EBV: Estimated Breeding Value)',
  })
  traitEbv: number;

  @Column({
    name: 'trait_percentile',
    type: 'decimal',
    precision: 10,
    scale: 4,
    nullable: true,
    comment: '백분위수 (전국 대비 순위)',
  })
  traitPercentile: number;

  // Relations
  @ManyToOne(() => GenomeRequestModel, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'fk_request_no' })
  genomeRequest: GenomeRequestModel;
}
