import { Entity, Column, OneToMany, JoinColumn, ManyToOne } from "typeorm";
import { EntityBase } from "./base/Base";
import { Command } from "./Command";
import { CommandSys } from "./CommandSys";

@Entity("commandType")
export class CommandType extends EntityBase {
  @Column({
    nullable: true,
    comment: "ลำดับความสำคัญ",
    default: null,
  })
  order: number;

  @Column({
    nullable: true,
    comment: "ชื่อคำสั่ง",
    length: 255,
    default: null,
  })
  name: string;

  @Column({
    nullable: true,
    comment: "รหัสคำสั่ง",
    length: 255,
    default: null,
  })
  code: string;

  @Column({
    nullable: true,
    comment: "รหัสประเภทของคำสั่ง",
    length: 255,
    default: null,
  })
  commandCode: string;

  @Column({
    nullable: true,
    comment: "คำสั่งเรื่อง",
    length: 255,
    default: null,
  })
  fileCover: string;

  @Column({
    nullable: true,
    comment: "คำสั่งเรื่อง",
    length: 255,
    default: null,
  })
  fileAttachment: string;

  @Column({
    nullable: true,
    comment: "เนื้อหาคำสั่งส่วนต้น",
    type: "text",
    default: null,
  })
  detailHeader: string;

  @Column({
    nullable: true,
    comment: "เนื้อหาคำสั่งส่วนกลาง",
    type: "text",
    default: null,
  })
  detailBody: string;

  @Column({
    nullable: true,
    comment: "เนื้อหาคำสั่งส่วนท้าย",
    type: "text",
    default: null,
  })
  detailFooter: string;

  @Column({
    comment: "สถานะการใช้งาน",
    default: true,
  })
  isActive: boolean;

  @Column({
    comment: "สถานะบัญชีแนบท้าย",
    default: true,
  })
  isAttachment: boolean;

  @Column({
    comment: "อัปโหลดบัญชีแนบท้าย",
    default: false,
  })
  isUploadAttachment: boolean;

  @Column({
    comment: "สถานะแก้ไขเงินเดือน",
    default: true,
  })
  isSalary: boolean;

  @Column({
    nullable: true,
    comment: "คำอธิบาย",
    type: "text",
    default: null,
  })
  subtitle: string;

  @OneToMany(() => Command, (command) => command.commandType)
  commands: Command[];

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

  @ManyToOne(() => CommandSys, (commandSys) => commandSys.commandTypes)
  @JoinColumn({ name: "commandSysId" })
  commandTypeSys: CommandSys;
}

export class CreateCommandType {
  @Column()
  name: string;

  @Column()
  isActive: boolean;

  @Column()
  commandSysId: string;

  @Column()
  subtitle: string | null;

  @Column()
  isAttachment: boolean;

  @Column()
  isUploadAttachment: boolean;

  @Column()
  isSalary?: boolean | null;
}

export type UpdateCommandType = Partial<CreateCommandType>;
