-- Master Data untuk Pengelolaan Laporan Pengukuran Iklim Kerja Suhu
-- Berdasarkan PMK RI No.2 Tahun 2023

-- Master Tables

-- Tabel Master Regulasi
CREATE TABLE master_regulations (
    MasterRegulationID INT PRIMARY KEY AUTO_INCREMENT,
    MasterRegulationCode VARCHAR(50) NOT NULL,
    MasterRegulationName VARCHAR(200) NOT NULL,
    MasterRegulationIssuer VARCHAR(100) NOT NULL,
    MasterRegulationYear INT NOT NULL,
    MasterRegulationEffectiveDate DATE,
    MasterRegulationDescription TEXT,
    MasterRegulationFilePath VARCHAR(255),
    MasterRegulationIsActive BOOLEAN DEFAULT TRUE,
    MasterRegulationCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterRegulationCreatedUserID INT,
    MasterRegulationUpdatedAt DATETIME,
    MasterRegulationUpdatedUserID INT,
    MasterRegulationDeletedAt DATETIME,
    MasterRegulationDeletedUserID INT
);

-- Tabel Master Parameter
CREATE TABLE master_parameters (
    MasterParameterID INT PRIMARY KEY AUTO_INCREMENT,
    MasterParameterCode VARCHAR(50) NOT NULL,
    MasterParameterName VARCHAR(100) NOT NULL,
    MasterParameterGroup VARCHAR(50),
    MasterParameterUnit VARCHAR(20),
    MasterParameterMethod VARCHAR(100),
    MasterParameterDescription TEXT,
    MasterParameterPrice DECIMAL(12,2),
    MasterParameterIsActive BOOLEAN DEFAULT TRUE,
    MasterParameterCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterParameterCreatedUserID INT,
    MasterParameterUpdatedAt DATETIME,
    MasterParameterUpdatedUserID INT,
    MasterParameterDeletedAt DATETIME,
    MasterParameterDeletedUserID INT
);

-- Tabel Master Nilai Ambang Batas
CREATE TABLE master_thresholds (
    MasterThresholdID INT PRIMARY KEY AUTO_INCREMENT,
    MasterRegulationID INT,
    MasterParameterID INT,
    MasterThresholdRoomType VARCHAR(100) NOT NULL,
    MasterThresholdMinValue DECIMAL(8,2),
    MasterThresholdMaxValue DECIMAL(8,2),
    MasterThresholdUnit VARCHAR(20),
    MasterThresholdRemarks TEXT,
    MasterThresholdIsActive BOOLEAN DEFAULT TRUE,
    MasterThresholdCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterThresholdCreatedUserID INT,
    MasterThresholdUpdatedAt DATETIME,
    MasterThresholdUpdatedUserID INT,
    MasterThresholdDeletedAt DATETIME,
    MasterThresholdDeletedUserID INT
);

-- Tabel Master Pelanggan
CREATE TABLE master_customers (
    MasterCustomerID INT PRIMARY KEY AUTO_INCREMENT,
    MasterCustomerCode VARCHAR(50) NOT NULL,
    MasterCustomerName VARCHAR(200) NOT NULL,
    MasterCustomerAddress TEXT,
    MasterCustomerCity VARCHAR(100),
    MasterCustomerProvince VARCHAR(100),
    MasterCustomerPostalCode VARCHAR(20),
    MasterCustomerPhone VARCHAR(50),
    MasterCustomerEmail VARCHAR(100),
    MasterCustomerContactPerson VARCHAR(100),
    MasterCustomerContactPosition VARCHAR(100),
    MasterCustomerBusinessField VARCHAR(100),
    MasterCustomerIsActive BOOLEAN DEFAULT TRUE,
    MasterCustomerCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterCustomerCreatedUserID INT,
    MasterCustomerUpdatedAt DATETIME,
    MasterCustomerUpdatedUserID INT,
    MasterCustomerDeletedAt DATETIME,
    MasterCustomerDeletedUserID INT
);

-- Tabel Master Personel
CREATE TABLE master_personnel (
    MasterPersonnelID INT PRIMARY KEY AUTO_INCREMENT,
    MasterPersonnelNIK VARCHAR(50) NOT NULL,
    MasterPersonnelName VARCHAR(100) NOT NULL,
    MasterPersonnelPosition VARCHAR(100),
    MasterPersonnelDepartment VARCHAR(100),
    MasterPersonnelEmail VARCHAR(100),
    MasterPersonnelPhone VARCHAR(50),
    MasterPersonnelCertification TEXT,
    MasterPersonnelIsActive BOOLEAN DEFAULT TRUE,
    MasterPersonnelCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterPersonnelCreatedUserID INT,
    MasterPersonnelUpdatedAt DATETIME,
    MasterPersonnelUpdatedUserID INT,
    MasterPersonnelDeletedAt DATETIME,
    MasterPersonnelDeletedUserID INT
);

-- Tabel Master Peralatan
CREATE TABLE master_equipment (
    MasterEquipmentID INT PRIMARY KEY AUTO_INCREMENT,
    MasterEquipmentCode VARCHAR(50) NOT NULL,
    MasterEquipmentName VARCHAR(100) NOT NULL,
    MasterEquipmentBrand VARCHAR(100),
    MasterEquipmentModel VARCHAR(100),
    MasterEquipmentSerialNumber VARCHAR(100),
    MasterEquipmentCalibrationDate DATE,
    MasterEquipmentNextCalibrationDate DATE,
    MasterParameterID INT,
    MasterEquipmentSpecification TEXT,
    MasterEquipmentIsActive BOOLEAN DEFAULT TRUE,
    MasterEquipmentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterEquipmentCreatedUserID INT,
    MasterEquipmentUpdatedAt DATETIME,
    MasterEquipmentUpdatedUserID INT,
    MasterEquipmentDeletedAt DATETIME,
    MasterEquipmentDeletedUserID INT
);

-- Tabel Master Lokasi Sampling
CREATE TABLE master_sampling_locations (
    MasterSamplingLocationID INT PRIMARY KEY AUTO_INCREMENT,
    MasterCustomerID INT,
    MasterSamplingLocationName VARCHAR(200) NOT NULL,
    MasterSamplingLocationAddress TEXT,
    MasterSamplingLocationLatitude DECIMAL(10,8),
    MasterSamplingLocationLongitude DECIMAL(11,8),
    MasterSamplingLocationRoomType VARCHAR(100),
    MasterSamplingLocationDescription TEXT,
    MasterSamplingLocationIsActive BOOLEAN DEFAULT TRUE,
    MasterSamplingLocationCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterSamplingLocationCreatedUserID INT,
    MasterSamplingLocationUpdatedAt DATETIME,
    MasterSamplingLocationUpdatedUserID INT,
    MasterSamplingLocationDeletedAt DATETIME,
    MasterSamplingLocationDeletedUserID INT
);

-- Transaction Tables

-- Tabel Permintaan Pengukuran
CREATE TABLE measurement_requests (
    MeasurementRequestID INT PRIMARY KEY AUTO_INCREMENT,
    MeasurementRequestNumber VARCHAR(50) NOT NULL,
    MasterCustomerID INT,
    MeasurementRequestDate DATE,
    MeasurementRequestPlanDate DATE,
    MeasurementRequestContactPerson VARCHAR(100),
    MeasurementRequestStatus VARCHAR(50),
    MeasurementRequestNotes TEXT,
    MeasurementRequestCreatedBy INT,
    MeasurementRequestApprovedBy INT,
    MeasurementRequestCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MeasurementRequestCreatedUserID INT,
    MeasurementRequestUpdatedAt DATETIME,
    MeasurementRequestUpdatedUserID INT,
    MeasurementRequestDeletedAt DATETIME,
    MeasurementRequestDeletedUserID INT
);

-- Tabel Detail Permintaan Pengukuran
CREATE TABLE measurement_request_details (
    MeasurementRequestDetailID INT PRIMARY KEY AUTO_INCREMENT,
    MeasurementRequestID INT,
    MasterParameterID INT,
    MasterSamplingLocationID INT,
    MeasurementRequestDetailPointCount INT DEFAULT 1,
    MeasurementRequestDetailUnitPrice DECIMAL(12,2),
    MeasurementRequestDetailSubtotal DECIMAL(12,2),
    MeasurementRequestDetailCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MeasurementRequestDetailCreatedUserID INT,
    MeasurementRequestDetailUpdatedAt DATETIME,
    MeasurementRequestDetailUpdatedUserID INT,
    MeasurementRequestDetailDeletedAt DATETIME,
    MeasurementRequestDetailDeletedUserID INT
);

-- Tabel Jadwal Pengukuran
CREATE TABLE measurement_schedules (
    MeasurementScheduleID INT PRIMARY KEY AUTO_INCREMENT,
    MeasurementScheduleNumber VARCHAR(50) NOT NULL,
    MeasurementRequestID INT,
    MeasurementScheduleDate DATE,
    MasterPersonnelID INT,
    MeasurementScheduleStatus VARCHAR(50),
    MeasurementScheduleNotes TEXT,
    MeasurementScheduleCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MeasurementScheduleCreatedUserID INT,
    MeasurementScheduleUpdatedAt DATETIME,
    MeasurementScheduleUpdatedUserID INT,
    MeasurementScheduleDeletedAt DATETIME,
    MeasurementScheduleDeletedUserID INT
);

-- Tabel Laporan Hasil Uji
CREATE TABLE test_reports (
    TestReportID INT PRIMARY KEY AUTO_INCREMENT,
    TestReportNumber VARCHAR(50) NOT NULL,
    MeasurementRequestID INT,
    MeasurementScheduleID INT,
    TestReportMeasurementDate DATE,
    TestReportIssueDate DATE,
    MasterCustomerID INT,
    MasterPersonnelID INT,
    TestReportVerifiedBy INT,
    TestReportApprovedBy INT,
    TestReportStatus VARCHAR(50),
    TestReportPageCount INT DEFAULT 1,
    TestReportNotes TEXT,
    TestReportCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    TestReportCreatedUserID INT,
    TestReportUpdatedAt DATETIME,
    TestReportUpdatedUserID INT,
    TestReportDeletedAt DATETIME,
    TestReportDeletedUserID INT
);

-- Tabel Hasil Pengukuran Suhu
CREATE TABLE temperature_measurements (
    TemperatureMeasurementID INT PRIMARY KEY AUTO_INCREMENT,
    TestReportID INT,
    MasterSamplingLocationID INT,
    TemperatureMeasurementCode VARCHAR(50) NOT NULL,
    TemperatureMeasurementStartTime TIME,
    TemperatureMeasurementEndTime TIME,
    MasterParameterID INT,
    TemperatureMeasurementValue DECIMAL(8,2),
    TemperatureMeasurementUnit VARCHAR(20),
    TemperatureMeasurementMethod VARCHAR(100),
    MasterThresholdID INT,
    TemperatureMeasurementRemarks TEXT,
    MasterEquipmentID INT,
    TemperatureMeasurementCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    TemperatureMeasurementCreatedUserID INT,
    TemperatureMeasurementUpdatedAt DATETIME,
    TemperatureMeasurementUpdatedUserID INT,
    TemperatureMeasurementDeletedAt DATETIME,
    TemperatureMeasurementDeletedUserID INT
);

-- ================================
-- INSERT DATA MASTER
-- ================================

-- Insert Master Regulasi
INSERT INTO master_regulations (MasterRegulationID, MasterRegulationCode, MasterRegulationName, MasterRegulationIssuer, MasterRegulationYear, MasterRegulationEffectiveDate, MasterRegulationDescription, MasterRegulationFilePath, MasterRegulationIsActive) VALUES
(1, 'PMK-02-2023', 'Peraturan Menteri Kesehatan Republik Indonesia Nomor 2 Tahun 2023', 'Kementerian Kesehatan RI', 2023, '2023-02-01', 'Peraturan mengenai Standar Baku Mutu Kesehatan Lingkungan dan Persyaratan Kesehatan', '/documents/regulasi/PMK_02_2023.pdf', TRUE),
(2, 'PERMENAKER-05-2018', 'Peraturan Menteri Ketenagakerjaan Republik Indonesia Nomor 5 Tahun 2018', 'Kementerian Ketenagakerjaan RI', 2018, '2018-05-10', 'Peraturan mengenai Keselamatan dan Kesehatan Kerja Lingkungan Kerja', '/documents/regulasi/PERMENAKER_05_2018.pdf', TRUE);

-- Insert Master Parameter
INSERT INTO master_parameters (MasterParameterID, MasterParameterCode, MasterParameterName, MasterParameterGroup, MasterParameterUnit, MasterParameterMethod, MasterParameterDescription, MasterParameterPrice, MasterParameterIsActive) VALUES
(1, 'SUHU', 'Suhu', 'Fisika', '°C', 'SNI 7062: 2019', 'Parameter iklim kerja untuk pengukuran suhu ruangan', 75000.00, TRUE),
(2, 'KELEMBABAN', 'Kelembaban', 'Fisika', '%RH', 'SNI 7062: 2019', 'Parameter iklim kerja untuk pengukuran kelembaban relatif', 75000.00, TRUE),
(3, 'ISBB', 'Indeks Suhu Basah dan Bola', 'Fisika', '°C', 'SNI 7062: 2019', 'Parameter iklim kerja untuk pengukuran ISBB indoor/outdoor', 100000.00, TRUE),
(4, 'KECUDARA', 'Kecepatan Aliran Udara', 'Fisika', 'm/s', 'SNI 7062: 2019', 'Parameter iklim kerja untuk pengukuran kecepatan aliran udara', 75000.00, TRUE);

-- Insert Master Nilai Ambang Batas untuk Parameter Suhu
INSERT INTO master_thresholds (MasterThresholdID, MasterRegulationID, MasterParameterID, MasterThresholdRoomType, MasterThresholdMinValue, MasterThresholdMaxValue, MasterThresholdUnit, MasterThresholdRemarks, MasterThresholdIsActive) VALUES
(1, 1, 1, 'Administrasi', 20.00, 25.00, '°C', 'Ruang kerja administrasi dan perkantoran', TRUE),
(2, 1, 1, 'Ruangan Khusus', 18.00, 24.00, '°C', 'Ruang server, laboratorium, dll', TRUE),
(3, 1, 1, 'Produksi', 23.00, 26.00, '°C', 'Ruang produksi', TRUE),
(4, 1, 1, 'Ruang Umum', 22.00, 28.00, '°C', 'Ruang umum, kantin, lobi', TRUE),
(5, 1, 1, 'Toilet', 22.00, 28.00, '°C', 'Kamar mandi, toilet', TRUE),
(6, 1, 1, 'Gudang', 16.00, 30.00, '°C', 'Ruang penyimpanan, gudang', TRUE);

-- Insert Master Pelanggan
INSERT INTO master_customers (MasterCustomerID, MasterCustomerCode, MasterCustomerName, MasterCustomerAddress, MasterCustomerCity, MasterCustomerProvince, MasterCustomerPostalCode, MasterCustomerPhone, MasterCustomerEmail, MasterCustomerContactPerson, MasterCustomerContactPosition, MasterCustomerBusinessField, MasterCustomerIsActive) VALUES
(1, 'PLG-001', 'PT. Industri Manufaktur Indonesia', 'Jl. Industri Raya No. 123', 'Jakarta', 'DKI Jakarta', '12950', '021-5551234', 'contact@manufakturindonesia.com', 'Budi Santoso', 'HSE Manager', 'Manufaktur', TRUE),
(2, 'PLG-002', 'PT. Teknologi Digital Nusantara', 'Jl. Gatot Subroto Kav. 52-53', 'Jakarta', 'DKI Jakarta', '12710', '021-5552345', 'hrd@tdn.co.id', 'Siti Rahma', 'HR Director', 'Teknologi Informasi', TRUE),
(3, 'PLG-003', 'RSUD Kota Sehat', 'Jl. Kesehatan No. 45', 'Bandung', 'Jawa Barat', '40112', '022-7654321', 'info@rsudkotasehat.go.id', 'dr. Ahmad Hidayat', 'Direktur', 'Kesehatan', TRUE);

-- Insert Master Personel
INSERT INTO master_personnel (MasterPersonnelID, MasterPersonnelNIK, MasterPersonnelName, MasterPersonnelPosition, MasterPersonnelDepartment, MasterPersonnelEmail, MasterPersonnelPhone, MasterPersonnelCertification, MasterPersonnelIsActive) VALUES
(1, 'EMP-001', 'Hendra Wijaya', 'Supervisor Pengujian', 'Laboratorium', 'hendra@lab-env.co.id', '081234567890', 'Sertifikasi Pengambilan Contoh Uji Lingkungan Kerja', TRUE),
(2, 'EMP-002', 'Ratna Dewi', 'Analis Senior', 'Laboratorium', 'ratna@lab-env.co.id', '081234567891', 'Sertifikasi Analis Laboratorium, Ahli K3 Umum', TRUE),
(3, 'EMP-003', 'Deni Hermawan', 'Petugas Sampling', 'Laboratorium', 'deni@lab-env.co.id', '081234567892', 'Sertifikasi Pengambilan Contoh Uji', TRUE),
(4, 'EMP-004', 'Farida Nurhasanah', 'Manager Laboratorium', 'Laboratorium', 'farida@lab-env.co.id', '081234567893', 'Sertifikasi Manager Mutu, Sertifikasi Ahli K3', TRUE);

-- Insert Master Peralatan
INSERT INTO master_equipment (MasterEquipmentID, MasterEquipmentCode, MasterEquipmentName, MasterEquipmentBrand, MasterEquipmentModel, MasterEquipmentSerialNumber, MasterEquipmentCalibrationDate, MasterEquipmentNextCalibrationDate, MasterParameterID, MasterEquipmentSpecification, MasterEquipmentIsActive) VALUES
(1, 'EQP-TH001', 'Thermohygrometer', 'Lutron', 'PHB-318', 'LT12345678', '2023-11-10', '2024-05-10', 1, 'Range suhu: -20°C hingga 60°C, akurasi ±0.8°C', TRUE),
(2, 'EQP-TH002', 'Thermohygrometer', 'Extech', 'RHT50', 'EX87654321', '2023-10-15', '2024-04-15', 1, 'Range suhu: -30°C hingga 70°C, akurasi ±0.5°C', TRUE),
(3, 'EQP-WBGT001', 'WBGT Meter', 'Lutron', 'WBGT-2010SD', 'LW12348765', '2023-11-05', '2024-05-05', 3, 'Range WBGT: 15°C hingga 59°C, akurasi ±0.8°C', TRUE),
(4, 'EQP-ANM001', 'Anemometer', 'Lutron', 'AM-4214SD', 'LA23456789', '2023-09-20', '2024-03-20', 4, 'Range kecepatan udara: 0.2 m/s hingga 35 m/s, akurasi ±2%', TRUE);

-- Insert Master Lokasi Sampling untuk PT. Industri Manufaktur Indonesia
INSERT INTO master_sampling_locations (MasterSamplingLocationID, MasterCustomerID, MasterSamplingLocationName, MasterSamplingLocationAddress, MasterSamplingLocationLatitude, MasterSamplingLocationLongitude, MasterSamplingLocationRoomType, MasterSamplingLocationDescription, MasterSamplingLocationIsActive) VALUES
(1, 1, 'Ruang Meeting Utama', 'Lantai 2, Gedung Utama', 0.00000000, 0.00000000, 'Administrasi', 'Ruang meeting dengan kapasitas 20 orang', TRUE),
(2, 1, 'Ruang Kerja HRD', 'Lantai 2, Gedung Utama', 0.00000000, 0.00000000, 'Administrasi', 'Ruang kerja staff HRD', TRUE),
(3, 1, 'Ruang Server', 'Lantai 1, Gedung Utama', 0.00000000, 0.00000000, 'Ruangan Khusus', 'Ruang server dengan pendingin khusus', TRUE),
(4, 1, 'Area Produksi Line A', 'Lantai 1, Gedung Produksi', 0.00000000, 0.00000000, 'Produksi', 'Area produksi utama line A', TRUE),
(5, 1, 'Area Kantin', 'Lantai 1, Gedung Pendukung', 0.00000000, 0.00000000, 'Ruang Umum', 'Kantin karyawan dengan kapasitas 100 orang', TRUE);

-- ================================
-- CONTOH DATA TRANSAKSI
-- ================================

-- Contoh Permintaan Pengukuran
INSERT INTO measurement_requests (MeasurementRequestID, MeasurementRequestNumber, MasterCustomerID, MeasurementRequestDate, MeasurementRequestPlanDate, MeasurementRequestContactPerson, MeasurementRequestStatus, MeasurementRequestNotes, MeasurementRequestCreatedBy, MeasurementRequestApprovedBy) VALUES
(1, 'REQ/2024/04/001', 1, '2024-04-05', '2024-04-25', 'Budi Santoso', 'approved', 'Pengukuran rutin triwulanan untuk parameter iklim kerja', 3, 4);

-- Contoh Detail Permintaan Pengukuran
INSERT INTO measurement_request_details (MeasurementRequestDetailID, MeasurementRequestID, MasterParameterID, MasterSamplingLocationID, MeasurementRequestDetailPointCount, MeasurementRequestDetailUnitPrice, MeasurementRequestDetailSubtotal) VALUES
(1, 1, 1, 1, 1, 75000.00, 75000.00),
(2, 1, 1, 2, 1, 75000.00, 75000.00),
(3, 1, 1, 3, 1, 75000.00, 75000.00),
(4, 1, 1, 4, 1, 75000.00, 75000.00),
(5, 1, 1, 5, 1, 75000.00, 75000.00);

-- Contoh Jadwal Pengukuran
INSERT INTO measurement_schedules (MeasurementScheduleID, MeasurementScheduleNumber, MeasurementRequestID, MeasurementScheduleDate, MasterPersonnelID, MeasurementScheduleStatus, MeasurementScheduleNotes) VALUES
(1, 'SCH/2024/04/001', 1, '2024-04-25', 3, 'completed', 'Pengukuran dilakukan pada jam operasional normal');

-- Contoh Laporan Hasil Uji
INSERT INTO test_reports (TestReportID, TestReportNumber, MeasurementRequestID, MeasurementScheduleID, TestReportMeasurementDate, TestReportIssueDate, MasterCustomerID, MasterPersonnelID, TestReportVerifiedBy, TestReportApprovedBy, TestReportStatus, TestReportPageCount, TestReportNotes) VALUES
(1, 'LHU/KLIM/04/2024/001', 1, 1, '2024-04-25', '2024-04-28', 1, 3, 2, 4, 'published', 2, 'Hasil pengukuran menunjukkan semua parameter dalam batas normal');

-- Contoh Hasil Pengukuran Suhu
INSERT INTO temperature_measurements (TemperatureMeasurementID, TestReportID, MasterSamplingLocationID, TemperatureMeasurementCode, TemperatureMeasurementStartTime, TemperatureMeasurementEndTime, MasterParameterID, TemperatureMeasurementValue, TemperatureMeasurementUnit, TemperatureMeasurementMethod, MasterThresholdID, TemperatureMeasurementRemarks, MasterEquipmentID) VALUES
(1, 1, 1, 'C2504090001', '09:00:00', '09:15:00', 1, 21.00, '°C', 'SNI 7062: 2019', 1, NULL, 1),
(2, 1, 2, 'C2504090002', '09:30:00', '09:45:00', 1, 22.00, '°C', 'SNI 7062: 2019', 1, NULL, 1),
(3, 1, 3, 'C2504090003', '10:00:00', '10:15:00', 1, 19.00, '°C', 'SNI 7062: 2019', 2, NULL, 1),
(4, 1, 4, 'C2504090004', '10:30:00', '10:45:00', 1, 26.00, '°C', 'SNI 7062: 2019', 3, NULL, 1),
(5, 1, 5, 'C2504090005', '11:00:00', '11:15:00', 1, 24.00, '°C', 'SNI 7062: 2019', 4, NULL, 1); 