import {
  Controller,
  Post,
  Delete,
  Route,
  Security,
  Tags,
  Body,
  Path,
  Request,
  Get,
  Patch,
  Query,
} from "tsoa";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import HttpSuccess from "../interfaces/http-success";
import { AppDataSource } from "../database/data-source";
import {
  CreateProfileEmployeeEdit,
  EditProfileEmployeeEdit,
  ProfileEdit,
} from "../entities/ProfileEdit";
import { ProfileEmployee } from "../entities/ProfileEmployee";
import { RequestWithUser } from "../middlewares/user";
import { Brackets } from "typeorm";
import permission from "../interfaces/permission";
import { OrgRevision } from "../entities/OrgRevision";

@Route("api/v1/org/profile-employee/edit")
@Tags("ProfileEmployeeEdit")
@Security("bearerAuth")
export class ProfileEditEmployeeController extends Controller {
  private profileEmployeeRepo = AppDataSource.getRepository(ProfileEmployee);
  private profileEditRepository = AppDataSource.getRepository(ProfileEdit);
  private orgRevisionRepository = AppDataSource.getRepository(OrgRevision);

  @Get("user")
  public async detailProfileEditUserEmp(
    @Request() request: { user: Record<string, any> },
    @Query("page") page: number = 1,
    @Query("pageSize") pageSize: number = 10,
    @Query("keyword") keyword: string = "",
    @Query("status") status: string = "",
  ) {
    const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: request.user.sub });
    if (!profile) {
      throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
    }

    let [getProfileEdit, total] = await AppDataSource.getRepository(ProfileEdit)
      .createQueryBuilder("ProfileEdit")
      .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee")
      .where((qb) => {
        if (status != "" && status != null) {
          qb.andWhere("ProfileEdit.status = :status", { status: status });
        }
        qb.andWhere("ProfileEdit.profileEmployeeId = :profileEmployeeId", {
          profileEmployeeId: profile.id,
        });
      })
      .andWhere(
        new Brackets((qb) => {
          qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", {
            keyword: `%${keyword}%`,
          })
            .orWhere(
              keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1",
              {
                keyword: `%${keyword}%`,
              },
            )
            .orWhere(
              keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1",
              {
                keyword: `%${keyword}%`,
              },
            );
        }),
      )
      .orderBy("ProfileEdit.createdAt", "DESC")
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getManyAndCount();

    const _data = getProfileEdit.map((item) => ({
      id: item.id,
      topic: item.topic,
      detail: item.detail,
      status: item.status,
      remark: item.remark,
      createdAt: item.createdAt,
      createdFullName: item.createdFullName,
      lastUpdatedAt: item.lastUpdatedAt,
      lastUpdateFullName: item.lastUpdateFullName,
      fullname:
        (item?.profileEmployee?.prefix ?? "") +
        "" +
        (item?.profileEmployee?.firstName ?? "") +
        " " +
        (item?.profileEmployee?.lastName ?? ""),
    }));
    return new HttpSuccess({ data: _data, total: total });
  }

  @Get("admin")
  public async detailProfileEditAdminEmp(
    @Request() request: RequestWithUser,
    @Query("page") page: number = 1,
    @Query("pageSize") pageSize: number = 10,
    @Query("keyword") keyword: string = "",
    @Query("status") status: string = "",
    @Query("sortBy") sortBy?: string,
    @Query("descending") descending?: boolean,
  ) {
    let data = await new permission().PermissionOrgList(request, "SYS_REGISTRY_EMP");
    const orgRevisionPublish = await this.orgRevisionRepository
      .createQueryBuilder("orgRevision")
      .where("orgRevision.orgRevisionIsDraft = false")
      .andWhere("orgRevision.orgRevisionIsCurrent = true")
      .getOne();

    let query = await AppDataSource.getRepository(ProfileEdit)
      .createQueryBuilder("ProfileEdit")
      .leftJoinAndSelect("ProfileEdit.profileEmployee", "profileEmployee")
      .leftJoinAndSelect("profileEmployee.current_holders", "current_holders")
      .leftJoinAndSelect("current_holders.orgRevision", "orgRevision")
      .where((qb) => {
        if (status != "" && status != null) {
          qb.andWhere("ProfileEdit.status = :status", { status: status });
        }
        qb.andWhere("ProfileEdit.profileEmployeeId IS NOT NULL");
      })
      .andWhere(orgRevisionPublish ? `current_holders.orgRevisionId = :revisionId` : "1=1", {
        revisionId: orgRevisionPublish?.id,
      })
      .andWhere(
        data.root != undefined && data.root != null
          ? data.root[0] != null
            ? `current_holders.orgRootId IN (:...root)`
            : `current_holders.orgRootId is null`
          : "1=1",
        {
          root: data.root,
        },
      )
      .andWhere(
        data.child1 != undefined && data.child1 != null
          ? data.child1[0] != null
            ? `current_holders.orgChild1Id IN (:...child1)`
            : `current_holders.orgChild1Id is ${data.privilege == "PARENT" ? "not null" : "null"}`
          : "1=1",
        {
          child1: data.child1,
        },
      )
      .andWhere(
        data.child2 != undefined && data.child2 != null
          ? data.child2[0] != null
            ? `current_holders.orgChild2Id IN (:...child2)`
            : `current_holders.orgChild2Id is null`
          : "1=1",
        {
          child2: data.child2,
        },
      )
      .andWhere(
        data.child3 != undefined && data.child3 != null
          ? data.child3[0] != null
            ? `current_holders.orgChild3Id IN (:...child3)`
            : `current_holders.orgChild3Id is null`
          : "1=1",
        {
          child3: data.child3,
        },
      )
      .andWhere(
        data.child4 != undefined && data.child4 != null
          ? data.child4[0] != null
            ? `current_holders.orgChild4Id IN (:...child4)`
            : `current_holders.orgChild4Id is null`
          : "1=1",
        {
          child4: data.child4,
        },
      )
      .andWhere(
        new Brackets((qb) => {
          qb.where(keyword != "" && keyword != null ? "ProfileEdit.topic LIKE :keyword" : "1=1", {
            keyword: `%${keyword}%`,
          })
            .orWhere(
              keyword != "" && keyword != null ? "ProfileEdit.detail LIKE :keyword" : "1=1",
              {
                keyword: `%${keyword}%`,
              },
            )
            .orWhere(
              keyword != "" && keyword != null ? "ProfileEdit.remark LIKE :keyword" : "1=1",
              {
                keyword: `%${keyword}%`,
              },
            )
            .orWhere(
              keyword != "" && keyword != null
                ? "CONCAT(profileEmployee.prefix, profileEmployee.firstName, ' ', profileEmployee.lastName) LIKE :keyword"
                : "1=1",
              {
                keyword: `%${keyword}%`,
              },
            );
        }),
      )
      .orderBy("ProfileEdit.createdAt", "DESC");

    if (sortBy) {
      if (sortBy == "fullname") {
        query = query.orderBy(`profileEmployee.firstName`, descending ? "DESC" : "ASC");
      } else {
        query = query.orderBy(`ProfileEdit.${sortBy}`, descending ? "DESC" : "ASC");
      }
    }

    let [getProfileEdit, total] = await query
      .skip((page - 1) * pageSize)
      .take(pageSize)
      .getManyAndCount();

    const _data = getProfileEdit.map((item) => ({
      id: item.id,
      idcard: item.profileEmployee.citizenId,
      profileId: item.profileEmployee.id,
      topic: item.topic,
      detail: item.detail,
      status: item.status,
      remark: item.remark,
      createdAt: item.createdAt,
      createdFullName: item.createdFullName,
      lastUpdatedAt: item.lastUpdatedAt,
      lastUpdateFullName: item.lastUpdateFullName,
      fullname:
        (item?.profileEmployee?.prefix ?? "") +
        "" +
        (item?.profileEmployee?.firstName ?? "") +
        " " +
        (item?.profileEmployee?.lastName ?? ""),
    }));
    return new HttpSuccess({ data: _data, total: total });
  }

  //   @Get("{Id}")
  //   public async detailProfileEmployeeByIdEdit(@Path() Id: string) {
  //     const getProfileEdit = await this.profileEditRepository.findOne({
  //       where: { id: Id },
  //     });
  //     if (!getProfileEdit) {
  //       throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
  //     }
  //     return new HttpSuccess(getProfileEdit);
  //   }

  @Get("{id}")
  public async detailProfileEditEmp(@Path() id: string) {
    const getProfileEdit = await this.profileEditRepository.findOne({
      where: { id: id },
      relations: ["profileEmployee"],
    });
    if (!getProfileEdit) {
      throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");
    }
    const _data = {
      id: getProfileEdit.id,
      topic: getProfileEdit.topic,
      detail: getProfileEdit.detail,
      citizenId: getProfileEdit?.profileEmployee?.citizenId ?? "",
      status: getProfileEdit.status,
      remark: getProfileEdit.remark,
      createdAt: getProfileEdit.createdAt,
      createdFullName: getProfileEdit.createdFullName,
      lastUpdatedAt: getProfileEdit.lastUpdatedAt,
      lastUpdateFullName: getProfileEdit.lastUpdateFullName,
      fullname:
        (getProfileEdit?.profileEmployee?.prefix ?? "") +
        "" +
        (getProfileEdit?.profileEmployee?.firstName ?? "") +
        " " +
        (getProfileEdit?.profileEmployee?.lastName ?? ""),
    };
    return new HttpSuccess(_data);
  }

  @Post()
  public async profileEdit(
    @Request() req: RequestWithUser,
    @Body() body: CreateProfileEmployeeEdit,
  ) {
    const profile = await this.profileEmployeeRepo.findOneBy({ keycloak: req.user.sub });
    if (!profile) {
      throw new HttpError(HttpStatus.BAD_REQUEST, "ไม่พบ profile ดังกล่าว");
    }

    const data = new ProfileEdit();
    const meta = {
      createdUserId: req.user.sub,
      createdFullName: req.user.name,
      lastUpdateUserId: req.user.sub,
      lastUpdateFullName: req.user.name,
      createdAt: new Date(),
      lastUpdatedAt: new Date(),
    };
    Object.assign(data, { ...body, ...meta });
    data.status = "PENDING";
    await this.profileEditRepository.save(data);

    return new HttpSuccess(data.id);
  }

  @Patch("{editId}")
  public async editProfileEdit(
    @Body() requestBody: EditProfileEmployeeEdit,
    @Request() req: RequestWithUser,
    @Path() editId: string,
  ) {
    // const record = await this.profileEditRepository.findOneBy({ id: editId });
    const record = await this.profileEditRepository.findOne({
      where: { id: editId },
    });
    if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");

    Object.assign(record, requestBody);

    record.lastUpdateFullName = req.user.name;
    record.lastUpdateUserId = req.user.sub;
    record.lastUpdatedAt = new Date();

    await Promise.all([this.profileEditRepository.save(record)]);
    return new HttpSuccess();
  }

  @Delete("{editId}")
  public async deleteProfileEdit(@Path() editId: string) {
    const result = await this.profileEditRepository.delete({ id: editId });

    if (result.affected == undefined || result.affected <= 0)
      throw new HttpError(HttpStatus.NOT_FOUND, "ไม่พบข้อมูล");

    return new HttpSuccess();
  }
}
