import { Controller, Route, Security, Tags, SuccessResponse, Response, Get } from "tsoa";
import { AppDataSource } from "../database/data-source";
import HttpSuccess from "../interfaces/http-success";
import HttpStatus from "../interfaces/http-status";
import { BloodGroup } from "../entities/BloodGroup";
import { Gender } from "../entities/Gender";
import { Prefixe } from "../entities/Prefixe";
import { Relationship } from "../entities/Relationship";
import { Religion } from "../entities/Religion";
import { Rank } from "../entities/Rank";
import { EducationLevel } from "../entities/EducationLevel";
import { Province } from "../entities/Province";
import { District } from "../entities/District";
import { SubDistrict } from "../entities/SubDistrict";

@Route("api/v1/org/metadata")
@Tags("Profile")
@Security("bearerAuth")
@Response(
  HttpStatus.INTERNAL_SERVER_ERROR,
  "เกิดข้อผิดพลาด ไม่สามารถแสดงรายการได้ กรุณาลองใหม่ในภายหลัง",
)
@SuccessResponse(HttpStatus.OK, "สำเร็จ")
export class MainController extends Controller {
  private bloodGroupRepo = AppDataSource.getRepository(BloodGroup);
  private genderRepo = AppDataSource.getRepository(Gender);
  private prefixeRepo = AppDataSource.getRepository(Prefixe);
  private relationshipRepo = AppDataSource.getRepository(Relationship);
  private religionRepo = AppDataSource.getRepository(Religion);
  private rankRepo = AppDataSource.getRepository(Rank);
  private educationLevelRepo = AppDataSource.getRepository(EducationLevel);
  private provinceRepo = AppDataSource.getRepository(Province);
  private districtRepo = AppDataSource.getRepository(District);
  private subDistrictRepo = AppDataSource.getRepository(SubDistrict);
  /**
   * API ข้อมูลหลัก
   *
   * @summary ORG_065 - ข้อมูลหลัก
   *
   */
  @Get("main/person")
  async getMainPerson() {
    const bloodGroups = await this.bloodGroupRepo.find({order: { name: "ASC" }});
    const genders = await this.genderRepo.find({order: { name: "ASC" }});
    const prefixs = await this.prefixeRepo.find({order: { name: "ASC" }});
    const relationships = await this.relationshipRepo.find({order: { name: "ASC" }});
    const religions = await this.religionRepo.find({order: { name: "ASC" }});
    const rank = await this.rankRepo.find({order: { name: "ASC" }});
    const educationLevels = await this.educationLevelRepo.find({order: { rank: "ASC" }});
    const provinces = await this.provinceRepo.find({order: { name: "ASC" }});
    const districts = await this.districtRepo.find({order: { name: "ASC" }});
    const subDistricts = await this.subDistrictRepo.find({order: { name: "ASC" }});

    return new HttpSuccess({
      bloodGroups,
      genders,
      prefixs,
      relationships,
      religions,
      rank,
      educationLevels,
      provinces,
      districts,
      subDistricts,
    });
  }
}
