-- Master Data for Work Climate Temperature Measurement System
-- Based on Ministry of Health Regulation No. 2 Year 2023

-- Master Regulations Table
CREATE TABLE master_regulations (
    MasterRegulationID INT PRIMARY KEY AUTO_INCREMENT,
    MasterRegulationCode VARCHAR(50) NOT NULL,
    MasterRegulationName VARCHAR(200) NOT NULL,
    MasterRegulationIssuingAuthority VARCHAR(100) NOT NULL,
    MasterRegulationType VARCHAR(50),
    MasterRegulationIssueDate DATE,
    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
);

-- Master Climate Parameters Table
CREATE TABLE master_climate_parameters (
    MasterClimateParameterID INT PRIMARY KEY AUTO_INCREMENT,
    MasterClimateParameterCode VARCHAR(50) NOT NULL,
    MasterClimateParameterName VARCHAR(100) NOT NULL,
    MasterClimateParameterGroup VARCHAR(50),
    MasterClimateParameterUnit VARCHAR(20),
    MasterClimateParameterMethod VARCHAR(100),
    MasterClimateParameterDescription TEXT,
    MasterClimateParameterPrice DECIMAL(12,2),
    MasterClimateParameterIsActive BOOLEAN DEFAULT TRUE,
    MasterClimateParameterCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterClimateParameterCreatedUserID INT,
    MasterClimateParameterUpdatedAt DATETIME,
    MasterClimateParameterUpdatedUserID INT,
    MasterClimateParameterDeletedAt DATETIME,
    MasterClimateParameterDeletedUserID INT
);

-- Master Threshold Values Table
CREATE TABLE master_threshold_values (
    MasterThresholdValueID INT PRIMARY KEY AUTO_INCREMENT,
    MasterRegulationID INT,
    MasterClimateParameterID INT,
    MasterThresholdValueRoomType VARCHAR(100) NOT NULL,
    MasterThresholdValueMin DECIMAL(8,2),
    MasterThresholdValueMax DECIMAL(8,2),
    MasterThresholdValueUnit VARCHAR(20),
    MasterThresholdValueDescription TEXT,
    MasterThresholdValueIsActive BOOLEAN DEFAULT TRUE,
    MasterThresholdValueCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterThresholdValueCreatedUserID INT,
    MasterThresholdValueUpdatedAt DATETIME,
    MasterThresholdValueUpdatedUserID INT,
    MasterThresholdValueDeletedAt DATETIME,
    MasterThresholdValueDeletedUserID INT
);

-- Master Clients Table
CREATE TABLE master_clients (
    MasterClientID INT PRIMARY KEY AUTO_INCREMENT,
    MasterClientCode VARCHAR(50) NOT NULL,
    MasterClientName VARCHAR(200) NOT NULL,
    MasterClientAddress TEXT,
    MasterClientCity VARCHAR(100),
    MasterClientProvince VARCHAR(100),
    MasterClientPostalCode VARCHAR(20),
    MasterClientPhoneNumber VARCHAR(50),
    MasterClientEmail VARCHAR(100),
    MasterClientContactPerson VARCHAR(100),
    MasterClientContactPosition VARCHAR(100),
    MasterClientBusinessField VARCHAR(100),
    MasterClientIsActive BOOLEAN DEFAULT TRUE,
    MasterClientCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterClientCreatedUserID INT,
    MasterClientUpdatedAt DATETIME,
    MasterClientUpdatedUserID INT,
    MasterClientDeletedAt DATETIME,
    MasterClientDeletedUserID INT
);

-- Master Personnel Table
CREATE TABLE master_personnel (
    MasterPersonnelID INT PRIMARY KEY AUTO_INCREMENT,
    MasterPersonnelEmployeeID VARCHAR(50) NOT NULL,
    MasterPersonnelName VARCHAR(100) NOT NULL,
    MasterPersonnelPosition VARCHAR(100),
    MasterPersonnelDepartment VARCHAR(100),
    MasterPersonnelEmail VARCHAR(100),
    MasterPersonnelPhoneNumber VARCHAR(50),
    MasterPersonnelCertifications TEXT,
    MasterPersonnelIsActive BOOLEAN DEFAULT TRUE,
    MasterPersonnelCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterPersonnelCreatedUserID INT,
    MasterPersonnelUpdatedAt DATETIME,
    MasterPersonnelUpdatedUserID INT,
    MasterPersonnelDeletedAt DATETIME,
    MasterPersonnelDeletedUserID INT
);

-- Master Equipment Table
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,
    MasterClimateParameterID INT,
    MasterEquipmentSpecifications TEXT,
    MasterEquipmentIsActive BOOLEAN DEFAULT TRUE,
    MasterEquipmentCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MasterEquipmentCreatedUserID INT,
    MasterEquipmentUpdatedAt DATETIME,
    MasterEquipmentUpdatedUserID INT,
    MasterEquipmentDeletedAt DATETIME,
    MasterEquipmentDeletedUserID INT
);

-- Master Sampling Locations Table
CREATE TABLE master_sampling_locations (
    MasterSamplingLocationID INT PRIMARY KEY AUTO_INCREMENT,
    MasterClientID 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
);

-- Measurement Requests Table
CREATE TABLE measurement_requests (
    MeasurementRequestID INT PRIMARY KEY AUTO_INCREMENT,
    MeasurementRequestNumber VARCHAR(50) NOT NULL,
    MasterClientID INT,
    MeasurementRequestDate DATE,
    MeasurementRequestPlannedDate DATE,
    MeasurementRequestContactPerson VARCHAR(100),
    MeasurementRequestStatus VARCHAR(50),
    MeasurementRequestNotes TEXT,
    MeasurementRequestCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    MeasurementRequestCreatedUserID INT,
    MeasurementRequestUpdatedAt DATETIME,
    MeasurementRequestUpdatedUserID INT,
    MeasurementRequestDeletedAt DATETIME,
    MeasurementRequestDeletedUserID INT,
    MeasurementRequestApprovedByID INT
);

-- Measurement Request Details Table
CREATE TABLE measurement_request_details (
    MeasurementRequestDetailID INT PRIMARY KEY AUTO_INCREMENT,
    MeasurementRequestID INT,
    MasterClimateParameterID 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
);

-- Measurement Schedules Table
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
);

-- Test Reports Table
CREATE TABLE test_reports (
    TestReportID INT PRIMARY KEY AUTO_INCREMENT,
    TestReportNumber VARCHAR(50) NOT NULL,
    MeasurementRequestID INT,
    MeasurementScheduleID INT,
    TestReportMeasurementDate DATE,
    TestReportIssueDate DATE,
    MasterClientID INT,
    MasterPersonnelID INT,
    TestReportVerifiedByID INT,
    TestReportApprovedByID 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
);

-- Temperature Measurement Results Table
CREATE TABLE temperature_measurement_results (
    TemperatureMeasurementResultID INT PRIMARY KEY AUTO_INCREMENT,
    TestReportID INT,
    MasterSamplingLocationID INT,
    TemperatureMeasurementResultSampleCode VARCHAR(50) NOT NULL,
    TemperatureMeasurementResultStartTime TIME,
    TemperatureMeasurementResultEndTime TIME,
    MasterClimateParameterID INT,
    TemperatureMeasurementResultValue DECIMAL(8,2),
    TemperatureMeasurementResultUnit VARCHAR(20),
    TemperatureMeasurementResultMethod VARCHAR(100),
    MasterThresholdValueID INT,
    TemperatureMeasurementResultNotes TEXT,
    MasterEquipmentID INT,
    TemperatureMeasurementResultCreatedAt DATETIME DEFAULT CURRENT_TIMESTAMP,
    TemperatureMeasurementResultCreatedUserID INT,
    TemperatureMeasurementResultUpdatedAt DATETIME,
    TemperatureMeasurementResultUpdatedUserID INT,
    TemperatureMeasurementResultDeletedAt DATETIME,
    TemperatureMeasurementResultDeletedUserID INT
);

-- ================================
-- SAMPLE DATA INSERTION
-- ================================

-- Insert Regulations
INSERT INTO master_regulations (
    MasterRegulationID, 
    MasterRegulationCode, 
    MasterRegulationName,
    MasterRegulationIssuingAuthority,
    MasterRegulationType,
    MasterRegulationIssueDate,
    MasterRegulationEffectiveDate,
    MasterRegulationDescription,
    MasterRegulationFilePath,
    MasterRegulationIsActive
) VALUES (
    1, 'PMK-02-2023', 'Ministry of Health Regulation No. 2 Year 2023', 'Ministry of Health of Indonesia', 'Ministerial Regulation', '2023-01-20', '2023-02-01', 'Regulation on Environmental Health Quality Standards and Health Requirements', '/documents/regulations/PMK_02_2023.pdf', TRUE
), (
    2, 'PERMENAKER-05-2018', 'Ministry of Manpower Regulation No. 5 Year 2018', 'Ministry of Manpower of Indonesia', 'Ministerial Regulation', '2018-04-25', '2018-05-10', 'Regulation on Occupational Safety and Health for the Work Environment', '/documents/regulations/PERMENAKER_05_2018.pdf', TRUE
);

-- Insert Climate Parameters
INSERT INTO master_climate_parameters (MasterClimateParameterID, MasterClimateParameterCode, MasterClimateParameterName, MasterClimateParameterGroup, MasterClimateParameterUnit, MasterClimateParameterMethod, MasterClimateParameterDescription, MasterClimateParameterPrice, MasterClimateParameterIsActive) VALUES
(1, 'TEMP', 'Temperature', 'Physical', '°C', 'SNI 7062: 2019', 'Work climate parameter for room temperature measurement', 75000.00, TRUE),
(2, 'HUMID', 'Humidity', 'Physical', '%RH', 'SNI 7062: 2019', 'Work climate parameter for relative humidity measurement', 75000.00, TRUE),
(3, 'WBGT', 'Wet Bulb Globe Temperature', 'Physical', '°C', 'SNI 7062: 2019', 'Work climate parameter for indoor/outdoor WBGT measurement', 100000.00, TRUE),
(4, 'AIRVEL', 'Air Velocity', 'Physical', 'm/s', 'SNI 7062: 2019', 'Work climate parameter for air velocity measurement', 75000.00, TRUE);

-- Insert Threshold Values for Temperature
INSERT INTO master_threshold_values (MasterThresholdValueID, MasterRegulationID, MasterClimateParameterID, MasterThresholdValueRoomType, MasterThresholdValueMin, MasterThresholdValueMax, MasterThresholdValueUnit, MasterThresholdValueDescription, MasterThresholdValueIsActive) VALUES
(1, 1, 1, 'Administration', 20.00, 25.00, '°C', 'Administration and office work area', TRUE),
(2, 1, 1, 'Special Room', 18.00, 24.00, '°C', 'Server room, laboratory, etc.', TRUE),
(3, 1, 1, 'Production', 23.00, 26.00, '°C', 'Production area', TRUE),
(4, 1, 1, 'Common Area', 22.00, 28.00, '°C', 'Common area, canteen, lobby', TRUE),
(5, 1, 1, 'Restroom', 22.00, 28.00, '°C', 'Bathroom, toilet', TRUE),
(6, 1, 1, 'Storage', 16.00, 30.00, '°C', 'Storage room, warehouse', TRUE);

-- Insert Clients
INSERT INTO master_clients (MasterClientID, MasterClientCode, MasterClientName, MasterClientAddress, MasterClientCity, MasterClientProvince, MasterClientPostalCode, MasterClientPhoneNumber, MasterClientEmail, MasterClientContactPerson, MasterClientContactPosition, MasterClientBusinessField, MasterClientIsActive) VALUES
(1, 'CLT-001', 'PT. Indonesia Manufacturing Industry', 'Jl. Industry Raya No. 123', 'Jakarta', 'DKI Jakarta', '12950', '021-5551234', 'contact@manufacturingindonesia.com', 'Budi Santoso', 'HSE Manager', 'Manufacturing', TRUE),
(2, 'CLT-002', 'PT. Digital Technology Nusantara', 'Jl. Gatot Subroto Kav. 52-53', 'Jakarta', 'DKI Jakarta', '12710', '021-5552345', 'hrd@dtn.co.id', 'Siti Rahma', 'HR Director', 'Information Technology', TRUE),
(3, 'CLT-003', 'City Public Hospital', 'Jl. Health No. 45', 'Bandung', 'West Java', '40112', '022-7654321', 'info@cityhospital.go.id', 'Dr. Ahmad Hidayat', 'Director', 'Healthcare', TRUE);

-- Insert Personnel
INSERT INTO master_personnel (MasterPersonnelID, MasterPersonnelEmployeeID, MasterPersonnelName, MasterPersonnelPosition, MasterPersonnelDepartment, MasterPersonnelEmail, MasterPersonnelPhoneNumber, MasterPersonnelCertifications, MasterPersonnelIsActive) VALUES
(1, 'EMP-001', 'Hendra Wijaya', 'Testing Supervisor', 'Laboratory', 'hendra@lab-env.co.id', '081234567890', 'Work Environment Sampling Certification', TRUE),
(2, 'EMP-002', 'Ratna Dewi', 'Senior Analyst', 'Laboratory', 'ratna@lab-env.co.id', '081234567891', 'Laboratory Analyst Certification, General OSH Certification', TRUE),
(3, 'EMP-003', 'Deni Hermawan', 'Sampling Officer', 'Laboratory', 'deni@lab-env.co.id', '081234567892', 'Test Sample Collection Certification', TRUE),
(4, 'EMP-004', 'Farida Nurhasanah', 'Laboratory Manager', 'Laboratory', 'farida@lab-env.co.id', '081234567893', 'Quality Manager Certification, OSH Certification', TRUE);

-- Insert Measurement Equipment
INSERT INTO master_equipment (MasterEquipmentID, MasterEquipmentCode, MasterEquipmentName, MasterEquipmentBrand, MasterEquipmentModel, MasterEquipmentSerialNumber, MasterEquipmentCalibrationDate, MasterEquipmentNextCalibrationDate, MasterClimateParameterID, MasterEquipmentSpecifications, MasterEquipmentIsActive) VALUES
(1, 'EQP-TH001', 'Thermohygrometer', 'Lutron', 'PHB-318', 'LT12345678', '2023-11-10', '2024-05-10', 1, 'Temperature range: -20°C to 60°C, accuracy ±0.8°C', TRUE),
(2, 'EQP-TH002', 'Thermohygrometer', 'Extech', 'RHT50', 'EX87654321', '2023-10-15', '2024-04-15', 1, 'Temperature range: -30°C to 70°C, accuracy ±0.5°C', TRUE),
(3, 'EQP-WBGT001', 'WBGT Meter', 'Lutron', 'WBGT-2010SD', 'LW12348765', '2023-11-05', '2024-05-05', 3, 'WBGT range: 15°C to 59°C, accuracy ±0.8°C', TRUE),
(4, 'EQP-ANM001', 'Anemometer', 'Lutron', 'AM-4214SD', 'LA23456789', '2023-09-20', '2024-03-20', 4, 'Air velocity range: 0.2 m/s to 35 m/s, accuracy ±2%', TRUE);

-- Insert Sampling Locations for PT. Indonesia Manufacturing Industry
INSERT INTO master_sampling_locations (MasterSamplingLocationID, MasterClientID, MasterSamplingLocationName, MasterSamplingLocationAddress, MasterSamplingLocationRoomType, MasterSamplingLocationDescription, MasterSamplingLocationIsActive) VALUES
(1, 1, 'Main Meeting Room', 'Floor 2, Main Building', 'Administration', 'Meeting room with 20 person capacity', TRUE),
(2, 1, 'HR Work Area', 'Floor 2, Main Building', 'Administration', 'HR staff work area', TRUE),
(3, 1, 'Server Room', 'Floor 1, Main Building', 'Special Room', 'Server room with special cooling', TRUE),
(4, 1, 'Production Line A', 'Floor 1, Production Building', 'Production', 'Main production area line A', TRUE),
(5, 1, 'Canteen Area', 'Floor 1, Support Building', 'Common Area', 'Employee canteen with 100 person capacity', TRUE);

-- ================================
-- TRANSACTION DATA EXAMPLE
-- ================================

-- Measurement Request Example
INSERT INTO measurement_requests (MeasurementRequestID, MeasurementRequestNumber, MasterClientID, MeasurementRequestDate, MeasurementRequestPlannedDate, MeasurementRequestContactPerson, MeasurementRequestStatus, MeasurementRequestNotes, MeasurementRequestCreatedUserID, MeasurementRequestApprovedByID) VALUES
(1, 'REQ/2024/04/001', 1, '2024-04-05', '2024-04-25', 'Budi Santoso', 'approved', 'Quarterly routine measurement for work climate parameters', 3, 4);

-- Request Details
INSERT INTO measurement_request_details (MeasurementRequestDetailID, MeasurementRequestID, MasterClimateParameterID, MasterSamplingLocationID, MeasurementRequestDetailPointCount, MeasurementRequestDetailUnitPrice, MeasurementRequestDetailSubtotal, MeasurementRequestDetailCreatedUserID) VALUES
(1, 1, 1, 1, 1, 75000.00, 75000.00, 3),
(2, 1, 1, 2, 1, 75000.00, 75000.00, 3),
(3, 1, 1, 3, 1, 75000.00, 75000.00, 3),
(4, 1, 1, 4, 1, 75000.00, 75000.00, 3),
(5, 1, 1, 5, 1, 75000.00, 75000.00, 3);

-- Measurement Schedule
INSERT INTO measurement_schedules (MeasurementScheduleID, MeasurementScheduleNumber, MeasurementRequestID, MeasurementScheduleDate, MasterPersonnelID, MeasurementScheduleStatus, MeasurementScheduleNotes, MeasurementScheduleCreatedUserID) VALUES
(1, 'SCH/2024/04/001', 1, '2024-04-25', 3, 'completed', 'Measurement conducted during normal operating hours', 3);

-- Test Report
INSERT INTO test_reports (TestReportID, TestReportNumber, MeasurementRequestID, MeasurementScheduleID, TestReportMeasurementDate, TestReportIssueDate, MasterClientID, MasterPersonnelID, TestReportVerifiedByID, TestReportApprovedByID, TestReportStatus, TestReportPageCount, TestReportNotes, TestReportCreatedUserID) VALUES
(1, 'LHU/TEMP/04/2024/001', 1, 1, '2024-04-25', '2024-04-28', 1, 3, 2, 4, 'published', 2, 'Measurement results show all parameters within normal limits', 3);

-- Temperature Measurement Results
INSERT INTO temperature_measurement_results (TemperatureMeasurementResultID, TestReportID, MasterSamplingLocationID, TemperatureMeasurementResultSampleCode, TemperatureMeasurementResultStartTime, TemperatureMeasurementResultEndTime, MasterClimateParameterID, TemperatureMeasurementResultValue, TemperatureMeasurementResultUnit, TemperatureMeasurementResultMethod, MasterThresholdValueID, TemperatureMeasurementResultNotes, MasterEquipmentID, TemperatureMeasurementResultCreatedUserID) VALUES
(1, 1, 1, 'C2504090001', '09:00:00', '09:15:00', 1, 21.00, '°C', 'SNI 7062: 2019', 1, NULL, 1, 3),
(2, 1, 2, 'C2504090002', '09:30:00', '09:45:00', 1, 22.00, '°C', 'SNI 7062: 2019', 1, NULL, 1, 3),
(3, 1, 3, 'C2504090003', '10:00:00', '10:15:00', 1, 19.00, '°C', 'SNI 7062: 2019', 2, NULL, 1, 3),
(4, 1, 4, 'C2504090004', '10:30:00', '10:45:00', 1, 26.00, '°C', 'SNI 7062: 2019', 3, NULL, 1, 3),
(5, 1, 5, 'C2504090005', '11:00:00', '11:15:00', 1, 24.00, '°C', 'SNI 7062: 2019', 4, NULL, 1, 3); 