import { Entity, Column, BeforeInsert } from "typeorm";
import { AppDataSource } from "../database/data-source";
import { EntityBase } from "./base/Base";

@Entity("issues")
export class Issues extends EntityBase {
  @Column({
    type: "varchar",
    nullable: false,
    length: 20,
    comment: "รหัส issue เช่น ISS20260127001",
  })
  codeIssue: string;

  @Column({ type: "varchar", nullable: false, length: 255, comment: "หัวข้อ" })
  title: string;

  @Column({ type: "text", nullable: false, comment: "รายละเอียดของปัญหา" })
  description: string | null;

  @Column({ type: "varchar", nullable: false, length: 50, comment: "ระบบ" })
  system: string;

  @Column({ type: "varchar", nullable: false, length: 255, comment: "เมนู" })
  menu: string | null;

  @Column({ type: "varchar", nullable: true, length: 500, comment: "สังกัด" })
  org: string | null;

  @Column({ type: "text", nullable: true, comment: "หมายเหตุ" })
  remark: string | null;

  @Column({ type: "varchar", nullable: true, length: 255, comment: "อีเมลผู้รายงาน" })
  email: string | null;

  @Column({ type: "varchar", nullable: true, length: 20, comment: "เบอร์โทรผู้รายงาน" })
  phone: string | null;

  @Column({
    type: "enum",
    enum: ["NEW", "IN_PROGRESS", "RESOLVED", "CLOSED", "HELPDESK_IN_PROGRESS", "REPLIED"],
    default: "NEW",
    comment: "สถานะการแก้ไขปัญหา",
  })
  status: "NEW" | "IN_PROGRESS" | "RESOLVED" | "CLOSED" | "HELPDESK_IN_PROGRESS" | "REPLIED";

  @BeforeInsert()
  async generateCodeIssue() {
    const today = new Date();
    const dateStr = today.toISOString().slice(0, 10).replace(/-/g, "");
    const prefix = `ISS${dateStr}`;

    const repository = AppDataSource.getRepository(Issues);
    const lastIssue = await repository
      .createQueryBuilder("issue")
      .where("issue.codeIssue LIKE :prefix", { prefix: `${prefix}%` })
      .orderBy("issue.codeIssue", "DESC")
      .getOne();

    let runningNumber = 1;
    if (lastIssue) {
      const lastNumber = parseInt(lastIssue.codeIssue.slice(-3), 10);
      runningNumber = lastNumber + 1;
    }

    this.codeIssue = `${prefix}${runningNumber.toString().padStart(3, "0")}`;
  }
}

// Interface สำหรับ TSOA Response
export interface IssueResponse {
  id: string;
  codeIssue: string;
  title: string;
  description: string | null;
  system: string;
  menu: string | null;
  org: string | null;
  remark: string | null;
  status: "NEW" | "IN_PROGRESS" | "RESOLVED" | "CLOSED" | "HELPDESK_IN_PROGRESS" | "REPLIED";
  createdAt: Date;
  lastUpdatedAt: Date;
  createdFullName: string;
  lastUpdateFullName: string;
  email: string | null;
  phone: string | null;
}

export interface CreateIssueRequest {
  title: string;
  description?: string;
  system: string;
  status?: "NEW" | "IN_PROGRESS" | "RESOLVED" | "CLOSED" | "HELPDESK_IN_PROGRESS" | "REPLIED";
  menu?: string;
  org?: string;
  email?: string;
  phone?: string;
}

export interface UpdateIssueRequest {
  status?: "NEW" | "IN_PROGRESS" | "RESOLVED" | "CLOSED" | "HELPDESK_IN_PROGRESS" | "REPLIED";
  remark?: string;
}
