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

@Entity("profileInsignia")
export class ProfileInsignia 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,
    comment: "ปีที่ยื่นขอ",
    default: null,
  })
  year: number;

  @Column({
    nullable: true,
    length: 20,
    comment: "ลำดับที่",
    default: null,
  })
  no: string;

  @Column({
    nullable: true,
    length: 30,
    comment: "เล่ม",
    default: null,
  })
  volume: string;

  @Column({
    nullable: true,
    length: 30,
    comment: "ตอน",
    default: null,
  })
  section: string;

  @Column({
    nullable: true,
    length: 30,
    comment: "หน้า",
    default: null,
  })
  page: string;

  @Column({
    nullable: true,
    type: "datetime",
    comment: "ลงวันที่",
    default: null,
  })
  receiveDate: Date;

  @Column({
    nullable: true,
    type: "datetime",
    comment: "วันที่ประกาศในราชกิจจาฯ",
    default: null,
  })
  dateAnnounce: Date;

  @Column({
    nullable: true,
    length: 300,
    comment: "ราชกิจจาฯ ฉบับที่",
    default: null,
  })
  issue: string;

  @Column({
    nullable: true,
    length: 30,
    comment: "เล่มที่",
    default: null,
  })
  volumeNo: string;

  @Column({
    nullable: true,
    type: "datetime",
    comment: "เอกสารอ้างอิง (ลงวันที่)",
    default: null,
  })
  refCommandDate: Date;

  @Column({
    nullable: true,
    comment: "เอกสารอ้างอิง (เลขที่คำสั่ง)",
    type: "text",
    default: null,
  })
  refCommandNo: string;

  @Column({
    nullable: true,
    comment: "หมายเหตุ",
    default: null,
  })
  note: string;

  @Column({
    comment: "แนบไฟล์เอกสาร",
    default: false,
  })
  isUpload: boolean;

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

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

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

  @ManyToOne(() => Insignia, (v) => v.profileInsignias)
  insignia: Insignia;

  @OneToMany(
    () => ProfileInsigniaHistory,
    (profileInsigniaHistory) => profileInsigniaHistory.histories,
  )
  profileInsigniaHistories: ProfileInsigniaHistory[];

  @ManyToOne(() => Profile, (profile) => profile.profileInsignias)
  @JoinColumn({ name: "profileId" })
  profile: Profile;

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

export class CreateProfileInsignia {
  profileId: string | null;
  year: number;
  no: string | null;
  volume: string | null;
  section: string | null;
  page: string | null;
  receiveDate: Date | null;
  insigniaId: string;
  dateAnnounce: Date | null;
  issue: string | null;
  volumeNo: string | null;
  refCommandDate: Date | null;
  refCommandNo: string | null;
  note: string | null;
  isUpload?: boolean | null;
}

export class CreateProfileEmployeeInsignia {
  profileEmployeeId: string | null;
  year: number;
  no: string | null;
  volume: string | null;
  section: string | null;
  page: string | null;
  receiveDate: Date | null;
  insigniaId: string;
  dateAnnounce: Date | null;
  issue: string | null;
  volumeNo: string | null;
  refCommandDate: Date | null;
  refCommandNo: string | null;
  note: string | null;
  isUpload?: boolean | null;
}

export type UpdateProfileInsignia = {
  year?: number;
  no?: string | null;
  volume?: string | null;
  section?: string | null;
  page?: string | null;
  receiveDate?: Date | null;
  insigniaId?: string;
  insigniaType?: string | null;
  dateAnnounce?: Date | null;
  issue?: string | null;
  volumeNo?: string | null;
  refCommandDate?: Date | null;
  refCommandNo?: string | null;
  note?: string | null;
  isUpload?: boolean | null;
};
