import { MigrationInterface, QueryRunner } from "typeorm"

export class CreatePayrollCycleTables1764574028337 implements MigrationInterface {
  name = "CreatePayrollCycleTables1764574028337"

  public async up(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `CREATE TABLE "salary_structures" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "company_id" integer NOT NULL, "employee_id" integer NOT NULL, "department_id" integer NOT NULL, "effective_from" date NOT NULL, "effective_to" date, "status" smallint NOT NULL DEFAULT '1', CONSTRAINT "PK_1800f745fd1ebe08981cd422acd" PRIMARY KEY ("id")); COMMENT ON COLUMN "salary_structures"."status" IS '1=Active, 0=Inactive'`,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_c948796c4c939f52fa6b0df475" ON "salary_structures" ("company_id", "employee_id") `,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_66a2d8539b4cc3d37ba5d62040" ON "salary_structures" ("employee_id", "effective_from", "effective_to") `,
    )
    await queryRunner.query(
      `CREATE TABLE "payroll_cycles" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "company_id" integer NOT NULL, "month" integer NOT NULL, "year" integer NOT NULL, "status" "public"."payroll_cycles_status_enum" NOT NULL DEFAULT '0', "locked_by" integer, "locked_at" TIMESTAMP, CONSTRAINT "PK_44b44291611618e516ab6b56d5b" PRIMARY KEY ("id")); COMMENT ON COLUMN "payroll_cycles"."month" IS '1-12'; COMMENT ON COLUMN "payroll_cycles"."year" IS 'e.g., 2025'; COMMENT ON COLUMN "payroll_cycles"."status" IS '0=Draft, 1=Finalized, 2=Paid'`,
    )
    await queryRunner.query(
      `CREATE UNIQUE INDEX "IDX_418781ce99979a9f1fe2ff5bfc" ON "payroll_cycles" ("company_id", "month", "year") `,
    )
    await queryRunner.query(
      `CREATE TABLE "payroll_entries" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "company_id" integer NOT NULL, "payroll_cycle_id" integer NOT NULL, "employee_id" integer NOT NULL, "working_days" integer NOT NULL, "present_days" integer NOT NULL, "paid_leaves" integer NOT NULL DEFAULT '0', "unpaid_leaves" integer NOT NULL DEFAULT '0', "total_earnings" numeric(12,2) NOT NULL, "total_deductions" numeric(12,2) NOT NULL, "net_payable" numeric(12,2) NOT NULL, CONSTRAINT "PK_749b8c91480734ee1e616d4456d" PRIMARY KEY ("id"))`,
    )
    await queryRunner.query(
      `CREATE TABLE "payroll_entry_components" ("created_by" integer, "updated_by" integer, "deleted_by" integer, "created_at" TIMESTAMP NOT NULL DEFAULT NOW(), "updated_at" TIMESTAMP NOT NULL DEFAULT NOW(), "deleted_at" TIMESTAMP, "id" SERIAL NOT NULL, "company_id" integer NOT NULL, "payroll_entry_id" integer NOT NULL, "employee_id" integer NOT NULL, "component_key" character varying(255) NOT NULL, "component_label" character varying(255) NOT NULL, "component_type" "public"."payroll_entry_components_component_type_enum" NOT NULL, "calculation_type" "public"."payroll_entry_components_calculation_type_enum" NOT NULL, "calculation_value" numeric(12,2) NOT NULL, "amount" numeric(12,2) NOT NULL, CONSTRAINT "PK_ecca443f79f28170aac513d520e" PRIMARY KEY ("id")); COMMENT ON COLUMN "payroll_entry_components"."calculation_value" IS 'value defined in salary_settings'; COMMENT ON COLUMN "payroll_entry_components"."amount" IS 'final calculated amount'`,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_0f59289dd064008fe15a7e38d1" ON "payroll_entry_components" ("company_id", "employee_id") `,
    )
    await queryRunner.query(
      `CREATE INDEX "IDX_e15b6199d5acf13ead09d5050f" ON "payroll_entry_components" ("payroll_entry_id", "component_key") `,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" ADD CONSTRAINT "FK_4265f50091f39403a676bee7b6c" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" ADD CONSTRAINT "FK_e77e23919f090442d593192aeb8" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" ADD CONSTRAINT "FK_1bde4ad4c7ae255baf6e5432dc8" FOREIGN KEY ("department_id") REFERENCES "departments"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_cycles" ADD CONSTRAINT "FK_e91e2cf9850d0ebc27f59c3a56d" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" ADD CONSTRAINT "FK_cd425e99a7b71347e08ecbb42f1" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" ADD CONSTRAINT "FK_90fdf2632c623c3338f4026c3bb" FOREIGN KEY ("payroll_cycle_id") REFERENCES "payroll_cycles"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" ADD CONSTRAINT "FK_d89bb38d97d8b4aa20afabece3e" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" ADD CONSTRAINT "FK_ee5a99f015d589095389863bfa8" FOREIGN KEY ("company_id") REFERENCES "companies"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" ADD CONSTRAINT "FK_52f5895998399de4874a55554e1" FOREIGN KEY ("payroll_entry_id") REFERENCES "payroll_entries"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" ADD CONSTRAINT "FK_e2593b6abaabae5444a9168ce5d" FOREIGN KEY ("employee_id") REFERENCES "employees"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
    )
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" DROP CONSTRAINT "FK_e2593b6abaabae5444a9168ce5d"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" DROP CONSTRAINT "FK_52f5895998399de4874a55554e1"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entry_components" DROP CONSTRAINT "FK_ee5a99f015d589095389863bfa8"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" DROP CONSTRAINT "FK_d89bb38d97d8b4aa20afabece3e"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" DROP CONSTRAINT "FK_90fdf2632c623c3338f4026c3bb"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_entries" DROP CONSTRAINT "FK_cd425e99a7b71347e08ecbb42f1"`,
    )
    await queryRunner.query(
      `ALTER TABLE "payroll_cycles" DROP CONSTRAINT "FK_e91e2cf9850d0ebc27f59c3a56d"`,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" DROP CONSTRAINT "FK_1bde4ad4c7ae255baf6e5432dc8"`,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" DROP CONSTRAINT "FK_e77e23919f090442d593192aeb8"`,
    )
    await queryRunner.query(
      `ALTER TABLE "salary_structures" DROP CONSTRAINT "FK_4265f50091f39403a676bee7b6c"`,
    )
    await queryRunner.query(
      `DROP INDEX "public"."IDX_e15b6199d5acf13ead09d5050f"`,
    )
    await queryRunner.query(
      `DROP INDEX "public"."IDX_0f59289dd064008fe15a7e38d1"`,
    )
    await queryRunner.query(`DROP TABLE "payroll_entry_components"`)
    await queryRunner.query(`DROP TABLE "payroll_entries"`)
    await queryRunner.query(
      `DROP INDEX "public"."IDX_418781ce99979a9f1fe2ff5bfc"`,
    )
    await queryRunner.query(`DROP TABLE "payroll_cycles"`)
    await queryRunner.query(
      `DROP INDEX "public"."IDX_66a2d8539b4cc3d37ba5d62040"`,
    )
    await queryRunner.query(
      `DROP INDEX "public"."IDX_c948796c4c939f52fa6b0df475"`,
    )
    await queryRunner.query(`DROP TABLE "salary_structures"`)
  }
}
