import { ViewColumn, ViewEntity } from "typeorm";

@ViewEntity({
  expression: `
    WITH Position AS (
      SELECT 
          posExecutive.posExecutiveName,
          pn.posMasterId,
          pn.positionArea,
          ROW_NUMBER() OVER (PARTITION BY pn.posMasterId) AS pn_number
      FROM position pn
      LEFT JOIN posExecutive ON pn.posExecutiveId = posExecutive.id
      WHERE pn.positionIsSelected IS TRUE
  ),
  PosMaster AS (
      SELECT 
          pn.positionArea,
          pm.current_holderId,
          pm.posMasterNo,
          pm.orgRootId,
          pm.orgChild1Id,
          pm.orgChild2Id,
          pm.orgChild3Id,
          pm.orgChild4Id,
          orgRoot.orgRootName,
          orgChild1.orgChild1Name,
          orgChild2.orgChild2Name,
          orgChild3.orgChild3Name,
          orgChild4.orgChild4Name,
          pn.posExecutiveName,
          CASE 
              WHEN pm.orgChild1Id IS NULL THEN CONCAT(orgRoot.orgRootShortName, " ", pm.posMasterNo)
              WHEN pm.orgChild2Id IS NULL THEN CONCAT(orgChild1.orgChild1ShortName, " ", pm.posMasterNo)
              WHEN pm.orgChild3Id IS NULL THEN CONCAT(orgChild2.orgChild2ShortName, " ", pm.posMasterNo)
              WHEN pm.orgChild4Id IS NULL THEN CONCAT(orgChild3.orgChild3ShortName, " ", pm.posMasterNo)
              ELSE CONCAT(orgChild4.orgChild4ShortName, " ", pm.posMasterNo)
          END AS searchShortName,
          ROW_NUMBER() OVER (PARTITION BY pm.current_holderId ORDER BY pm.posMasterNo DESC) AS pm_number
      FROM posMaster pm
      LEFT JOIN orgRevision ON orgRevision.id = pm.orgRevisionId
      LEFT JOIN orgRoot ON orgRoot.id = pm.orgRootId
      LEFT JOIN orgChild1 ON orgChild1.id = pm.orgChild1Id
      LEFT JOIN orgChild2 ON orgChild2.id = pm.orgChild2Id
      LEFT JOIN orgChild3 ON orgChild3.id = pm.orgChild3Id
      LEFT JOIN orgChild4 ON orgChild4.id = pm.orgChild4Id
      LEFT JOIN Position pn ON pm.id = pn.posMasterId AND pn.pn_number = 1
      WHERE 
          orgRevision.orgRevisionIsCurrent IS TRUE 
          AND orgRevision.orgRevisionIsDraft IS FALSE
  ),
  Educations AS (
    SELECT 
        eds.profileId,
        JSON_ARRAYAGG(
            JSON_OBJECT(
                'degree', eds.degree,
                'field', eds.field,
                'educationLevel', eds.educationLevel,
                'isEducation', eds.isEducation,
                'isHigh', eds.isHigh
            )
        ) AS Educations
    FROM (
        SELECT DISTINCT 
            eds.degree, 
            eds.field, 
            eds.educationLevel, 
            eds.isEducation, 
            eds.isHigh, 
            eds.profileId,
			eds.level
        FROM profileEducation eds
    ) AS eds
    GROUP BY eds.profileId
    ORDER BY eds.level DESC
  ),
  EducationLevels AS (
      SELECT 
		  edls.profileId,
          GROUP_CONCAT(DISTINCT edls.educationLevel ORDER BY edls.educationLevel SEPARATOR ', ') AS educationLevels
      FROM profileEducation edls
      WHERE edls.educationLevel IS NOT NULL AND edls.educationLevel != ''
      GROUP BY edls.profileId
      ORDER BY edls.level DESC
  ),
   Degrees AS (
      SELECT 
		  degs.profileId,
          GROUP_CONCAT(DISTINCT degs.degree ORDER BY degs.degree SEPARATOR ', ') AS degrees
      FROM profileEducation degs
      WHERE degs.degree IS NOT NULL AND degs.degree != ''
      GROUP BY degs.profileId
      ORDER BY degs.level DESC
  ),
  Fields  AS (
      SELECT 
		  fies.profileId,
          GROUP_CONCAT(DISTINCT fies.field ORDER BY fies.field SEPARATOR ', ') AS fields
      FROM profileEducation fies
      WHERE fies.field IS NOT NULL AND fies.field != ''
      GROUP BY fies.profileId
      ORDER BY fies.level DESC
  ),
  PositionDate AS (
      SELECT 
          vcto.Years,
          vcto.Months,
          vcto.Days,
          vcto.profileId
      FROM tenurePositionOfficer vcto
  ),
  PositionLevelDate AS (
      SELECT 
          vctlo.Years,
          vctlo.Months,
          vctlo.Days,
          vctlo.profileId
      FROM tenureLevelOfficer vctlo
  ),
  PositionExecutiveDate AS (
    SELECT 
        vcteo.Years,
        vcteo.Months,
        vcteo.Days,
        vcteo.profileId
    FROM tenurePositionExecutiveOfficer vcteo
  )
  SELECT 
      p.id as profileId,
      p.citizenId,
      p.rank,
      p.prefix,
      p.firstName,
      p.lastName,
      p.isProbation,
      p.isLeave,
      p.isRetirement,
      p.leaveType,
      pm.posMasterNo,
      pm.orgRootId,
      pm.orgChild1Id,
      pm.orgChild2Id,
      pm.orgChild3Id,
      pm.orgChild4Id,
      pm.orgRootName,
      pm.orgChild1Name,
      pm.orgChild2Name,
      pm.orgChild3Name,
      pm.orgChild4Name,
      CASE 
          WHEN pm.orgChild1Id IS NULL THEN pm.orgRootName
          WHEN pm.orgChild2Id IS NULL THEN CONCAT(pm.orgChild1Name, " ", pm.orgRootName)
          WHEN pm.orgChild3Id IS NULL THEN CONCAT(pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
          WHEN pm.orgChild4Id IS NULL THEN CONCAT(pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
          ELSE CONCAT(pm.orgChild4Name, " ", pm.orgChild3Name, " ", pm.orgChild2Name, " ", pm.orgChild1Name, " ", pm.orgRootName)
      END AS org,
      pm.searchShortName,
      pm.posExecutiveName,
      pm.positionArea,
      p.position,
      posType.posTypeName,
      posLevel.posLevelName,
      p.gender,
      p.relationship,
      p.dateAppoint,
      p.dateRetire,
      p.dateRetireLaw,
      p.birthdate,
      eds.Educations,
      edls.educationLevels,
      degs.degrees,
      fies.fields,
      TIMESTAMPDIFF(YEAR, p.birthdate, CURDATE()) AS age,
      vcto.Years,
      vcto.Months,
      vcto.Days,
      vctlo.Years AS levelYears,
      vctlo.Months AS levelMonths,
      vctlo.Days AS levelDays,
      vcteo.Years AS posExecutiveYears,
      vcteo.Months AS posExecutiveMonths,
      vcteo.Days AS posExecutiveDays
  FROM profile p
  LEFT JOIN posLevel ON p.posLevelId = posLevel.id
  LEFT JOIN posType ON p.posTypeId = posType.id
  LEFT JOIN PosMaster pm ON p.id = pm.current_holderId AND pm.pm_number = 1
  LEFT JOIN Educations eds ON p.id = eds.profileId
  LEFT JOIN EducationLevels edls ON p.id = edls.profileId
  LEFT JOIN Degrees degs ON p.id = degs.profileId
  LEFT JOIN Fields fies ON p.id = fies.profileId
  LEFT JOIN PositionDate vcto ON p.id = vcto.profileId
  LEFT JOIN PositionLevelDate vctlo ON p.id = vctlo.profileId
  LEFT JOIN PositionExecutiveDate vcteo ON p.id = vcteo.profileId
  `,
})
export class viewRegistryOfficer {
  @ViewColumn()
  profileId: string;

  @ViewColumn()
  citizenId: string;

  @ViewColumn()
  prefix: string;

  @ViewColumn()
  firstName: string;

  @ViewColumn()
  lastName: string;

  @ViewColumn()
  isProbation: boolean;

  @ViewColumn()
  isLeave: boolean;

  @ViewColumn()
  isRetirement: boolean;

  @ViewColumn()
  leaveType: string;

  @ViewColumn()
  posMasterNo: string;

  @ViewColumn()
  orgRootId: string;

  @ViewColumn()
  orgChild1Id: string;

  @ViewColumn()
  orgChild2Id: string;

  @ViewColumn()
  orgChild3Id: string;

  @ViewColumn()
  orgChild4Id: string;

  @ViewColumn()
  orgRootName: string;

  @ViewColumn()
  orgChild1Name: string;

  @ViewColumn()
  orgChild2Name: string;

  @ViewColumn()
  orgChild3Name: string;

  @ViewColumn()
  orgChild4Name: string;

  @ViewColumn()
  org: string;

  @ViewColumn()
  searchShortName: string;

  @ViewColumn()
  posExecutiveName: string;

  @ViewColumn()
  position: string;

  @ViewColumn()
  posTypeName: string;

  @ViewColumn()
  posLevelName: string;

  @ViewColumn()
  gender: string;

  @ViewColumn()
  relationship: string;

  @ViewColumn()
  dateAppoint: Date;

  @ViewColumn()
  dateRetire: Date;

  @ViewColumn()
  dateRetireLaw: Date;

  @ViewColumn()
  birthdate: Date;

  @ViewColumn()
  degrees: string;

  @ViewColumn()
  age: number;

  @ViewColumn()
  Years: number;

  @ViewColumn()
  Months: number;

  @ViewColumn()
  Days: number;

  @ViewColumn()
  levelYears: number;

  @ViewColumn()
  levelMonths: number;

  @ViewColumn()
  levelDays: number;

  @ViewColumn()
  posExecutiveYears: number;

  @ViewColumn()
  posExecutiveMonths: number;

  @ViewColumn()
  posExecutiveDays: number;

  @ViewColumn()
  positionArea: string;

  @ViewColumn()
  Educations: object;

  @ViewColumn()
  educationLevels: string;

  @ViewColumn()
  fields: string;
}
