import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn, UpdateDateColumn, OneToMany, Index } from 'typeorm';
import { OrderPerson } from './order-person.entity';
import { OrderStatus, WorkStatus } from '@d8d/allin-enums';

@Entity('employment_order', { comment: '用工订单表' })
@Index(['companyId', 'orderStatus']) // 企业统计查询优化
@Index(['companyId', 'workStatus']) // 企业统计查询优化
export class EmploymentOrder {
  @PrimaryGeneratedColumn({
    name: 'order_id',
    type: 'int',
    unsigned: true,
    comment: '订单ID'
  })
  id!: number;

  @Column({
    name: 'order_name',
    type: 'varchar',
    length: 50,
    nullable: true,
    comment: '订单名称'
  })
  orderName?: string;

  @Column({
    name: 'platform_id',
    type: 'int',
    nullable: false,
    comment: '用人平台ID'
  })
  platformId!: number;

  @Column({
    name: 'company_id',
    type: 'int',
    nullable: false,
    comment: '用人单位ID'
  })
  companyId!: number;

  @Column({
    name: 'channel_id',
    type: 'int',
    nullable: true,
    comment: '渠道ID'
  })
  channelId?: number;

  @Column({
    name: 'expected_start_date',
    type: 'date',
    nullable: true,
    comment: '预计开始日期'
  })
  expectedStartDate?: Date;

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

  @Column({
    name: 'actual_end_date',
    type: 'date',
    nullable: true,
    comment: '实际结束日期'
  })
  actualEndDate?: Date;

  @Column({
    name: 'order_status',
    type: 'enum',
    enum: OrderStatus,
    default: OrderStatus.DRAFT,
    comment: '订单状态：draft-草稿, confirmed-已确认, in_progress-进行中, completed-已完成, cancelled-已取消'
  })
  orderStatus!: OrderStatus;

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

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

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

  // 关系定义
  @OneToMany(() => OrderPerson, (orderPerson) => orderPerson.order)
  orderPersons!: OrderPerson[];

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