import { Entity, Column, ManyToOne, JoinColumn, OneToMany } from "typeorm";
import { EntityBase } from "./base/Base";
import { Profile } from "./Profile";
import { ProfileEmployee } from "./ProfileEmployee";
import { LeaveType } from "./LeaveType";

@Entity("profileLeave")
export class ProfileLeave extends EntityBase {
  @Column({
    nullable: true,
    length: 40,
    comment: "คีย์นอก(FK)ของตาราง Profile",
    default: null,
  })
  profileId: string;

  @Column({
    nullable: true,
    length: 40,
    comment: "คีย์นอก(FK)ของตาราง ProfileEmployee",
    default: null,
  })
  profileEmployeeId: string;

  @Column({
    nullable: true,
    length: 40,
    comment: "คีย์นอก(FK)ของตาราง LeaveType",
    default: null,
  })
  leaveTypeId: string;

  @Column({
    nullable: true,
    type: "datetime",
    comment: "วัน เดือน ปี ที่เริ่มลา",
    default: null,
  })
  dateLeaveStart: Date;

  @Column({
    nullable: true,
    type: "datetime",
    comment: "วัน เดือน ปี ที่สิ้นสุดลา",
    default: null,
  })
  dateLeaveEnd: Date;

  @Column({
    nullable: true,
    type: "double",
    comment: "จำนวนวันลา",
    default: null,
  })
  leaveDays: number;

  @Column({
    nullable: true,
    type: "double",
    comment: "ลามาเเล้ว",
    default: null,
  })
  leaveCount: number;

  @Column({
    nullable: true,
    type: "double",
    comment: "รวมเป็น",
    default: null,
  })
  totalLeave: number;

  @Column({
    nullable: true,
    comment: "สถานะ",
    type: "text",
    default: null,
  })
  status: string;

  @Column({
    nullable: true,
    comment: "เหตุผล",
    type: "text",
    default: null,
  })
  reason: string;

  @Column({
    nullable: true,
    length: 40,
    comment: "คีย์นอก(FK)ของตาราง LeaveRequest(hrms)",
    default: null,
  })
  leaveId: string;

  @Column({
    comment: "ข้อมูลจาก Entry",
    default: false,
  })
  isEntry: boolean;

  @Column({
    nullable: false,
    comment: "สถานะลบข้อมูล",
    default: false,
  })
  isDeleted: boolean;

  @Column({
    nullable: true,
    comment: "ประเภทย่อยการลา (เช่น ศึกษาต่อ, ฝึกอบรม, ปฎอบัติการวิจัย, ดูงาน)",
    type: "varchar",
    length: 255,
    default: null,
  })
  leaveSubTypeName: string;

  @Column({
    nullable: true,
    comment: "ประเทศที่ไป",
    type: "varchar",
    length: 255,
    default: null,
  })
  coupleDayLevelCountry: string;

  @OneToMany(() => ProfileLeaveHistory, (v) => v.profileLeave)
  histories: ProfileLeaveHistory[];

  @ManyToOne(() => LeaveType, (v) => v.profileLeave)
  leaveType: LeaveType;

  @ManyToOne(() => Profile, (v) => v.profileLeaves)
  profile: Profile;

  @ManyToOne(() => ProfileEmployee, (ProfileEmployee) => ProfileEmployee.profileLeaves)
  @JoinColumn({ name: "profileEmployeeId" })
  profileEmployee: ProfileEmployee;
}

@Entity("profileLeaveHistory")
export class ProfileLeaveHistory extends ProfileLeave {
  @Column({
    nullable: true,
    length: 40,
    comment: "คีย์นอก(FK)ของตาราง ProfileLeave",
    default: null,
  })
  profileLeaveId: string;

  @Column({
    nullable: false,
    comment: "สถานะลบข้อมูล",
    default: false,
  })
  isDeleted: boolean = false;

  @ManyToOne(() => ProfileLeave, (v) => v.histories)
  profileLeave: ProfileLeave;
}

export class CreateProfileLeave {
  profileId: string | null;
  leaveTypeId: string;
  dateLeaveStart: Date | null;
  dateLeaveEnd: Date | null;
  leaveDays: number | null;
  leaveCount?: number | null;
  totalLeave?: number | null;
  status?: string | null;
  reason: string | null;
  leaveId?: string | null;
  leaveSubTypeName?: string | null;
  coupleDayLevelCountry?: string | null;
}

export class CreateProfileEmployeeLeave {
  leaveTypeId: string;
  profileEmployeeId: string | null;
  dateLeaveStart: Date | null;
  dateLeaveEnd: Date | null;
  leaveDays: number | null;
  leaveCount: number | null;
  totalLeave: number | null;
  status: string | null;
  reason: string | null;
  leaveId?: string | null;
  leaveSubTypeName?: string | null;
  coupleDayLevelCountry?: string | null;
}

export type UpdateProfileLeave = {
  leaveTypeId?: string;
  dateLeaveStart?: Date | null;
  dateLeaveEnd?: Date | null;
  leaveDays?: number | null;
  leaveCount?: number | null;
  totalLeave?: number | null;
  status?: string | null;
  reason?: string | null;
  leaveSubTypeName?: string | null;
  coupleDayLevelCountry?: string | null;
};
