import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, Index } from 'typeorm';
import { EmploymentOrder } from './employment-order.entity';
import { DisabledPerson } from '@d8d/allin-disability-module/entities';
import { WorkStatus } from '@d8d/allin-enums';

@Entity('order_person', { comment: '订单人员关联表' })
@Index(['personId', 'workStatus']) // 人才工作历史查询优化
@Index(['orderId']) // 通过订单查询优化
@Index(['joinDate']) // 近期分配人才查询优化
@Index(['orderId', 'joinDate']) // 订单关联查询优化
@Index(['personId', 'orderId']) // 企业人才查询优化
@Index(['personId', 'joinDate', 'workStatus']) // 优化最新状态查询
export class OrderPerson {
  @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: 'join_date',
    type: 'date',
    nullable: false,
    comment: '入职日期'
  })
  joinDate!: Date;

  @Column({
    name: 'actual_start_date',
    type: 'date',
    nullable: true,
    comment: '实际入职日期'
  })
  actualStartDate?: Date;

  @Column({
    name: 'leave_date',
    type: 'date',
    nullable: true,
    comment: '离职日期'
  })
  leaveDate?: Date;

  @Column({
    name: 'work_status',
    type: 'enum',
    enum: WorkStatus,
    default: WorkStatus.NOT_WORKING,
    comment: '工作状态：not_working-未就业, pre_working-待就业, working-已就业, resigned-已离职'
  })
  workStatus!: WorkStatus;

  @Column({
    name: 'salary_detail',
    type: 'decimal',
    precision: 10,
    scale: 2,
    nullable: false,
    comment: '个人薪资'
  })
  salaryDetail!: number;

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

  // 与残疾人员的关联
  @ManyToOne(() => DisabledPerson, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'person_id' })
  person!: DisabledPerson;

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