import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { EmploymentOrder } from './employment-order.entity';
import { File } from '@d8d/file-module';
import { AssetType, AssetFileType } from '../schemas/order.schema';

@Entity('order_person_asset', { comment: '订单人员资产表' })
export class OrderPersonAsset {
  @PrimaryGeneratedColumn({
    name: 'op_id',
    type: 'int',
    unsigned: true,
    comment: '关联ID'
  })
  id!: number;

  @Column({
    name: 'order_id',
    type: 'int',
    nullable: false,
    comment: '订单ID'
  })
  orderId!: number;

  @Column({
    name: 'person_id',
    type: 'int',
    nullable: false,
    comment: '残疾人ID'
  })
  personId!: number;

  @Column({
    name: 'asset_type',
    type: 'varchar',
    length: 50,
    nullable: false,
    comment: '资产类型：tax-税务, salary-薪资, job_result-工作成果, contract_sign-合同签署, disability_cert-残疾证明, other-其他'
  })
  assetType!: AssetType;

  @Column({
    name: 'asset_file_type',
    type: 'varchar',
    length: 50,
    nullable: false,
    comment: '资产文件类型：image-图片, video-视频'
  })
  assetFileType!: AssetFileType;

  @Column({
    name: 'file_id',
    type: 'int',
    nullable: false,
    comment: '文件ID，引用files表'
  })
  fileId!: number;

  @Column({
    name: 'related_time',
    type: 'timestamp',
    nullable: false,
    default: () => 'CURRENT_TIMESTAMP',
    comment: '关联时间'
  })
  relatedTime!: Date;

  @CreateDateColumn({
    name: 'create_time',
    type: 'timestamp',
    nullable: false,
    comment: '创建时间'
  })
  createTime!: Date;

  @UpdateDateColumn({
    name: 'update_time',
    type: 'timestamp',
    nullable: false,
    comment: '更新时间'
  })
  updateTime!: Date;

  // 关系定义 - 与订单的关联
  @ManyToOne(() => EmploymentOrder, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'order_id' })
  order!: EmploymentOrder;

  // 注意：移除对DisabledPerson的直接引用，改为使用personId字段
  // 原代码：
  // @ManyToOne(() => DisabledPerson, { onDelete: 'CASCADE' })
  // @JoinColumn({ name: 'person_id' })
  // person: DisabledPerson;

  // 关系定义 - 与文件的关联
  @ManyToOne(() => File, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'file_id' })
  file!: File;

  constructor(partial?: Partial<OrderPersonAsset>) {
    Object.assign(this, partial);
  }
}