import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  OneToMany,
  CreateDateColumn,
  UpdateDateColumn,
  JoinColumn,
} from 'typeorm';
import { Parents } from './parents.entity';
import { Children } from './children.entity';
import { StatutDossierType } from './dossiers.entity';

/** Un dossier = une famille, N enfants (texte de motivation unique, liste d'enfants). */
@Entity('dossier_famille')
export class DossierFamille {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column({ name: 'numero_dossier', length: 20 })
  numero_dossier: string;

  @ManyToOne(() => Parents, { onDelete: 'CASCADE', nullable: false })
  @JoinColumn({ name: 'id_parent', referencedColumnName: 'user_id' })
  parent: Parents;

  @Column({ type: 'text', nullable: true })
  presentation?: string;

  @Column({
    type: 'enum',
    enum: StatutDossierType,
    enumName: 'statut_dossier_type',
    default: StatutDossierType.ENVOYE,
    name: 'statut',
  })
  statut: StatutDossierType;

  @CreateDateColumn({ name: 'cree_le', type: 'timestamptz' })
  cree_le: Date;

  @UpdateDateColumn({ name: 'modifie_le', type: 'timestamptz' })
  modifie_le: Date;

  @OneToMany(() => DossierFamilleEnfant, (dfe) => dfe.dossier_famille)
  enfants: DossierFamilleEnfant[];
}

@Entity('dossier_famille_enfants')
export class DossierFamilleEnfant {
  @Column({ name: 'id_dossier_famille', primary: true })
  id_dossier_famille: string;

  @Column({ name: 'id_enfant', primary: true })
  id_enfant: string;

  @ManyToOne(() => DossierFamille, (df) => df.enfants, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'id_dossier_famille' })
  dossier_famille: DossierFamille;

  @ManyToOne(() => Children, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'id_enfant' })
  enfant: Children;
}
