-- ============================================================
-- MIGRATION: Payroll Adjustment v1
-- Date: 2026-05-11
-- Adds: tb_payroll_komponen_adjustment, tb_payroll_adjustment_audit
-- ============================================================

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+07:00";

CREATE TABLE IF NOT EXISTS `tb_payroll_komponen_adjustment` (
  `id_adjustment`     BIGINT(20)   NOT NULL AUTO_INCREMENT,
  `id_periode`        INT(11)      NOT NULL,
  `id_payroll`        BIGINT(20)   NOT NULL,
  `id_pegawai`        INT(11)      NOT NULL,
  `nama_komponen`     VARCHAR(100) NOT NULL,
  `jenis`             ENUM('TUNJANGAN','POTONGAN') NOT NULL,
  `tipe_nilai`        ENUM('NOMINAL') NOT NULL DEFAULT 'NOMINAL',
  `nilai`             DECIMAL(15,2) NOT NULL DEFAULT 0.00,
  `catatan`           VARCHAR(255) NULL,
  `is_active`         TINYINT(1)   NOT NULL DEFAULT 1,
  `created_by`        INT(11)      NULL,
  `created_at`        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_by`        INT(11)      NULL,
  `updated_at`        DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_adjustment`),
  KEY `idx_adjustment_periode_payroll` (`id_periode`, `id_payroll`),
  KEY `idx_adjustment_pegawai` (`id_pegawai`),
  KEY `idx_adjustment_active` (`is_active`),
  CONSTRAINT `fk_adjustment_periode` FOREIGN KEY (`id_periode`) REFERENCES `tb_payroll_periode` (`id_periode`) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_adjustment_payroll` FOREIGN KEY (`id_payroll`) REFERENCES `tb_payroll` (`id_payroll`) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_adjustment_pegawai` FOREIGN KEY (`id_pegawai`) REFERENCES `tb_pegawai` (`id_pegawai`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `tb_payroll_adjustment_audit` (
  `id_audit`          BIGINT(20)   NOT NULL AUTO_INCREMENT,
  `id_adjustment`     BIGINT(20)   NULL,
  `id_periode`        INT(11)      NOT NULL,
  `id_payroll`        BIGINT(20)   NOT NULL,
  `id_pegawai`        INT(11)      NOT NULL,
  `aksi`              ENUM('CREATE','UPDATE','DELETE','RECALCULATE') NOT NULL,
  `before_payload`    LONGTEXT     NULL,
  `after_payload`     LONGTEXT     NULL,
  `performed_by`      INT(11)      NULL,
  `performed_at`      DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_audit`),
  KEY `idx_adjustment_audit_payroll` (`id_periode`, `id_payroll`),
  KEY `idx_adjustment_audit_pegawai` (`id_pegawai`),
  CONSTRAINT `fk_adjustment_audit_periode` FOREIGN KEY (`id_periode`) REFERENCES `tb_payroll_periode` (`id_periode`) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_adjustment_audit_payroll` FOREIGN KEY (`id_payroll`) REFERENCES `tb_payroll` (`id_payroll`) ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT `fk_adjustment_audit_pegawai` FOREIGN KEY (`id_pegawai`) REFERENCES `tb_pegawai` (`id_pegawai`) ON UPDATE CASCADE ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
