import { Body, Controller, Example, Get, Patch, Path, Request, Route, Security, Tags } from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import HttpError from "../interfaces/http-error";
import { RequestWithUser } from "../middlewares/user";
import { Profile } from "../entities/Profile";
import { ProfileGovernment, UpdateProfileGovernment } from "../entities/ProfileGovernment";
import {
  calculateAge,
  calculateGovAge,
  calculateRetireDate,
  setLogDataDiff,
} from "../interfaces/utils";
import permission from "../interfaces/permission";
import { In } from "typeorm";
@Route("api/v1/org/profile/government")
@Tags("ProfileGovernment")
@Security("bearerAuth")
export class ProfileGovernmentHistoryController extends Controller {
  private profileRepo = AppDataSource.getRepository(Profile);
  private govRepo = AppDataSource.getRepository(ProfileGovernment);
  /**
   *
   * @summary ข้อมูลราชการ
   *
   */
  @Get("user")
  public async getGovHistoryUser(@Request() request: { user: Record<string, any> }) {
    const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
    if (!profile) {
      throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
    }
    const record = await this.profileRepo.findOne({
      where: { id: profile.id },
      relations: {
        posType: true,
        posLevel: true,
      },
    });
    if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
 
    // ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
    const data = {
      org: record.org ?? null, //สังกัด
      positionField: record.positionField ?? null, //สายงาน
      position: record.position, //ตำแหน่ง
      posLevel: record.posLevel == null ? null : record.posLevel.posLevelName, //ระดับ
      posMasterNo: record.posMasterNo ?? null, //เลขที่ตำแหน่ง
      posType: record.posType == null ? null : record.posType.posTypeName, //ประเภท
      posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
      positionArea: record.positionArea ?? null, //ด้าน/สาขา
      positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
      dateLeave: record.birthDate == null ? null : calculateRetireDate(record.birthDate),
      dateRetireLaw: record.dateRetireLaw ?? null,
      // govAge: record.dateStart == null ? null : calculateAge(record.dateStart),
      govAge: await calculateGovAge(profile.id, "OFFICER"),
      govAgeBkk: await calculateGovAge(profile.id, "OFFICER", true),
      dateAppoint: record.dateAppoint,
      dateStart: record.dateStart,
      govAgeAbsent: record.govAgeAbsent,
      govAgePlus: record.govAgePlus,
      reasonSameDate: record.reasonSameDate,
    };
 
    return new HttpSuccess(data);
  }
 
  /**
   *
   * @summary ข้อมูลราชการ
   *
   */
  @Get("{profileId}")
  @Example({})
  public async getGovHistory(@Path() profileId: string, @Request() req: RequestWithUser) {
    let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
    if (_workflow == false)
      await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
 
    // ค้นหา profile ก่อน
    const record = await this.profileRepo.findOne({
      where: { id: profileId },
      relations: ["posType", "posLevel"],
    });
 
    if (!record) {
      throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
    }
 
    // ค้นหา profileSalary แยกต่างหาก
    const profileWithSalary = await this.profileRepo.findOne({
      where: {
        id: profileId,
        profileSalary: {
          commandCode: In([
            "0",
            "9",
            "1",
            "2",
            "3",
            "4",
            "8",
            "10",
            "11",
            "12",
            "13",
            "14",
            "15",
            "16",
            "20",
          ]),
        },
      },
      relations: ["profileSalary"],
      order: {
        profileSalary: {
          order: "DESC",
          createdAt: "DESC",
        },
      },
    });
 
    // ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
    record.profileSalary = profileWithSalary?.profileSalary || [];
 
    let _OrgLeave: any = [];
    let _profileSalary: any = null;
    if (record?.isLeave && record?.profileSalary.length > 0) {
      if (record.leaveType == "RETIRE") {
        _profileSalary =
          record?.profileSalary.length > 1
            ? record?.profileSalary[1]
            : record?.profileSalary.length > 0
              ? record?.profileSalary[0]
              : null;
      } else {
        _profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null;
      }
      if (_profileSalary) {
        _OrgLeave = [
          _profileSalary.orgChild4 ?? null,
          _profileSalary.orgChild3 ?? null,
          _profileSalary.orgChild2 ?? null,
          _profileSalary.orgChild1 ?? null,
          _profileSalary.orgRoot ?? null,
        ];
      } else {
        _OrgLeave = [];
      }
    }
    const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
 
    // ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
    const data = {
      org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
      positionField: record.positionField ?? null, //สายงาน
      position: record?.position, //ตำแหน่ง
      posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
      posMasterNo:
        record?.isLeave == false
          ? record.posMasterNo ?? null
          : _profileSalary != null
            ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
            : null, //เลขที่ตำแหน่ง
      posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
      posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
      positionArea: record.positionArea ?? null, //ด้าน/สาขา
      positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
      dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
      dateRetireLaw: record?.dateRetireLaw ?? null,
      // govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
      govAge: await calculateGovAge(profileId, "OFFICER"),
      govAgeBkk: await calculateGovAge(profileId, "OFFICER", true),
      dateAppoint: record?.dateAppoint,
      dateStart: record?.dateStart,
      govAgeAbsent: record?.govAgeAbsent,
      govAgePlus: record?.govAgePlus,
      reasonSameDate: record?.reasonSameDate,
    };
 
    return new HttpSuccess(data);
  }
 
  @Get("admin/{profileId}")
  public async getGovHistoryAdmin(@Path() profileId: string) {
    // ค้นหา profile ก่อน
    const record = await this.profileRepo.findOne({
      where: { id: profileId },
      relations: ["posType", "posLevel"],
    });
 
    if (!record) {
      throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล profile");
    }
 
    // ค้นหา profileSalary แยกต่างหาก
    const profileWithSalary = await this.profileRepo.findOne({
      where: {
        id: profileId,
        profileSalary: {
          commandCode: In([
            "0",
            "9",
            "1",
            "2",
            "3",
            "4",
            "8",
            "10",
            "11",
            "12",
            "13",
            "14",
            "15",
            "16",
            "20",
          ]),
        },
      },
      relations: ["profileSalary"],
      order: {
        profileSalary: {
          order: "DESC",
          createdAt: "DESC",
        },
      },
    });
 
    // ใช้ profileSalary จาก query ที่สอง หรือ [] ถ้าไม่เจอ
    record.profileSalary = profileWithSalary?.profileSalary || [];
 
    let _OrgLeave: any = [];
    let _profileSalary: any = null;
    if (record?.isLeave && record?.profileSalary.length > 0) {
      if (record.leaveType == "RETIRE") {
        _profileSalary =
          record?.profileSalary.length > 1
            ? record?.profileSalary[1]
            : record?.profileSalary.length > 0
              ? record?.profileSalary[0]
              : null;
      } else {
        _profileSalary = record?.profileSalary.length > 0 ? record?.profileSalary[0] : null;
      }
      if (_profileSalary) {
        _OrgLeave = [
          _profileSalary.orgChild4 ?? null,
          _profileSalary.orgChild3 ?? null,
          _profileSalary.orgChild2 ?? null,
          _profileSalary.orgChild1 ?? null,
          _profileSalary.orgRoot ?? null,
        ];
      } else {
        _OrgLeave = [];
      }
    }
    const orgLeave = _OrgLeave.filter((x: any) => x !== undefined && x !== null).join("\n");
 
    // ดึงข้อมูลจาก profile ที่เก็บไว้แล้ว
    const data = {
      org: record?.isLeave == false ? (record.org ?? null) : orgLeave, //สังกัด
      positionField: record.positionField ?? null, //สายงาน
      position: record?.position, //ตำแหน่ง
      posLevel: record?.posLevel == null ? null : record?.posLevel.posLevelName, //ระดับ
      posMasterNo:
        record?.isLeave == false
          ? record.posMasterNo ?? null
          : _profileSalary != null
            ? `${_profileSalary.posNoAbb} ${_profileSalary.posNo}`
            : null, //เลขที่ตำแหน่ง
      posType: record?.posType == null ? null : record?.posType.posTypeName, //ประเภท
      posExecutive: record.posExecutive ?? null, //ตำแหน่งทางการบริหาร
      positionArea: record.positionArea ?? null, //ด้าน/สาขา
      positionExecutiveField: record.positionExecutiveField ?? null, //ด้านทางการบริหาร
      dateLeave: record?.birthDate == null ? null : calculateRetireDate(record?.birthDate),
      dateRetireLaw: record?.dateRetireLaw ?? null,
      // govAge: record?.dateStart == null ? null : calculateAge(record?.dateStart),
      govAge: await calculateGovAge(profileId, "OFFICER"),
      govAgeBkk: await calculateGovAge(profileId, "OFFICER", true),
      dateAppoint: record?.dateAppoint,
      dateStart: record?.dateStart,
      govAgeAbsent: record?.govAgeAbsent,
      govAgePlus: record?.govAgePlus,
      reasonSameDate: record?.reasonSameDate,
      isLeave: record?.isLeave,
    };
 
    return new HttpSuccess(data);
  }
 
  /**
   *
   * @summary ประวัติข้อมูลราชการ by keycloak
   *
   */
  @Get("history/user")
  public async govHistoryUser(@Request() request: RequestWithUser) {
    const profile = await this.profileRepo.findOneBy({ keycloak: request.user.sub });
    if (!profile) {
      throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
    }
    const record = await this.govRepo.find({
      order: { lastUpdatedAt: "DESC" },
      where: { profileId: profile.id },
    });
    return new HttpSuccess(record);
  }
 
  /**
   *
   * @summary ประวัติข้อมูลราชการ
   *
   */
  @Get("history/{profileId}")
  @Example({})
  public async govHistory(@Path() profileId: string, @Request() req: RequestWithUser) {
    let _workflow = await new permission().Workflow(req, profileId, "SYS_REGISTRY_OFFICER");
    if (_workflow == false)
      await new permission().PermissionOrgUserGet(req, "SYS_REGISTRY_OFFICER", profileId);
    const record = await this.govRepo.find({
      order: { lastUpdatedAt: "DESC" },
      where: { profileId: profileId },
    });
 
    // record.pop();
 
    return new HttpSuccess(record);
  }
 
  /**
   *
   * @summary แก้ไขข้อมูลราชการ
   *
   */
  @Patch("{profileId}")
  public async editGov(
    @Request() req: RequestWithUser,
    @Body() body: UpdateProfileGovernment,
    @Path() profileId: string,
  ) {
    await new permission().PermissionOrgUserUpdate(req, "SYS_REGISTRY_OFFICER", profileId);
    const record = await this.profileRepo.findOne({
      where: { id: profileId },
    });
 
    if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
    const before = structuredClone(record);
    const history = new ProfileGovernment();
 
    Object.assign(record, body);
    Object.assign(history, { ...record, id: undefined });
 
    history.profileId = profileId;
    record.lastUpdateUserId = req.user.sub;
    record.lastUpdateFullName = req.user.name;
    record.lastUpdatedAt = new Date();
    history.lastUpdateUserId = req.user.sub;
    history.lastUpdateFullName = req.user.name;
    history.createdUserId = req.user.sub;
    history.createdFullName = req.user.name;
    history.createdAt = new Date();
    history.lastUpdatedAt = new Date();
 
    await Promise.all([
      this.profileRepo.save(record, { data: req }),
      setLogDataDiff(req, { before, after: record }),
      this.govRepo.save(history, { data: req }),
    ]);
 
    return new HttpSuccess();
  }
}
 