import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  JoinColumn,
  Index,
} from 'typeorm';
import { Student } from './student.entity';

@Entity('ding_leave_raw')
@Index(['workDate'])
@Index(['matchStatus'])
export class DingLeaveRaw {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'ding_user_id', length: 100 })
  dingUserId: string;

  @Column({ name: 'user_name', length: 100 })
  userName: string;

  @Column({ name: 'work_date', type: 'date' })
  workDate: string;

  @Column({ name: 'ding_id', length: 100, unique: true })
  dingId: string;

  @Column({ name: 'leave_type', length: 100 })
  leaveType: string;

  @Column({ name: 'tag_name', length: 50 })
  tagName: string;

  @Column({ name: 'start_time', type: 'datetime', nullable: true })
  startTime: Date | null;

  @Column({ name: 'end_time', type: 'datetime', nullable: true })
  endTime: Date | null;

  @Column({ name: 'approved_at', type: 'datetime', nullable: true })
  approvedAt: Date | null;

  @Column({ length: 20 })
  duration: string;

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

  @Column({ name: 'match_status', length: 20, default: 'unmatched' })
  matchStatus: string;

  @Column({ name: 'matched_student_id', type: 'integer', nullable: true })
  matchedStudentId: number;

  @ManyToOne(() => Student, { onDelete: 'SET NULL', nullable: true })
  @JoinColumn({ name: 'matched_student_id' })
  matchedStudent: Student;

  @Column({ name: 'raw_data', type: 'text', nullable: true })
  rawData: string;

  @CreateDateColumn({ name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ name: 'updated_at' })
  updatedAt: Date;
}
