-- 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 (
    regulation_id INT PRIMARY KEY AUTO_INCREMENT,
    regulation_code VARCHAR(50) NOT NULL,
    regulation_name VARCHAR(200) NOT NULL,
    issuing_authority VARCHAR(100) NOT NULL,
    regulation_type VARCHAR(50),
    issue_date DATE,
    effective_date DATE,
    description TEXT,
    file_path VARCHAR(255),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Master Climate Parameters Table
CREATE TABLE master_climate_parameters (
    parameter_id INT PRIMARY KEY AUTO_INCREMENT,
    parameter_code VARCHAR(50) NOT NULL,
    parameter_name VARCHAR(100) NOT NULL,
    parameter_group VARCHAR(50), -- Physical, Chemical, Biological
    unit VARCHAR(20),
    analysis_method VARCHAR(100),
    description TEXT,
    price DECIMAL(12, 2),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Master Threshold Limit Values (TLV) Table
CREATE TABLE master_threshold_values (
    threshold_id INT PRIMARY KEY AUTO_INCREMENT,
    regulation_id INT,
    parameter_id INT,
    room_type VARCHAR(100) NOT NULL,
    min_value DECIMAL(8, 2),
    max_value DECIMAL(8, 2),
    unit VARCHAR(20),
    description TEXT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (regulation_id) REFERENCES master_regulations(regulation_id),
    FOREIGN KEY (parameter_id) REFERENCES master_climate_parameters(parameter_id)
);

-- Master Clients Table
CREATE TABLE master_clients (
    client_id INT PRIMARY KEY AUTO_INCREMENT,
    client_code VARCHAR(50) NOT NULL,
    client_name VARCHAR(200) NOT NULL,
    address TEXT,
    city VARCHAR(100),
    province VARCHAR(100),
    postal_code VARCHAR(20),
    phone_number VARCHAR(50),
    email VARCHAR(100),
    contact_person VARCHAR(100),
    contact_position VARCHAR(100),
    business_field VARCHAR(100),
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Master Personnel Table (Sampling Officers, Analysts, etc.)
CREATE TABLE master_personnel (
    personnel_id INT PRIMARY KEY AUTO_INCREMENT,
    employee_id VARCHAR(50) NOT NULL,
    name VARCHAR(100) NOT NULL,
    position VARCHAR(100),
    department VARCHAR(100),
    email VARCHAR(100),
    phone_number VARCHAR(50),
    certifications TEXT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

-- Master Measurement Equipment Table
CREATE TABLE master_equipment (
    equipment_id INT PRIMARY KEY AUTO_INCREMENT,
    equipment_code VARCHAR(50) NOT NULL,
    equipment_name VARCHAR(100) NOT NULL,
    brand VARCHAR(100),
    model VARCHAR(100),
    serial_number VARCHAR(100),
    calibration_date DATE,
    next_calibration_date DATE,
    parameter_id INT,
    specifications TEXT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (parameter_id) REFERENCES master_climate_parameters(parameter_id)
);

-- Master Sampling Locations Table
CREATE TABLE master_sampling_locations (
    location_id INT PRIMARY KEY AUTO_INCREMENT,
    client_id INT,
    location_name VARCHAR(200) NOT NULL,
    address TEXT,
    latitude DECIMAL(10, 8),
    longitude DECIMAL(11, 8),
    room_type VARCHAR(100),
    description TEXT,
    is_active BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (client_id) REFERENCES master_clients(client_id)
);

-- Measurement Requests Table (Request Form)
CREATE TABLE measurement_requests (
    request_id INT PRIMARY KEY AUTO_INCREMENT,
    request_number VARCHAR(50) NOT NULL,
    client_id INT,
    request_date DATE,
    planned_sampling_date DATE,
    contact_person VARCHAR(100),
    status VARCHAR(50), -- draft, submitted, approved, scheduled, completed, canceled
    notes TEXT,
    created_by INT,
    approved_by INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (client_id) REFERENCES master_clients(client_id),
    FOREIGN KEY (created_by) REFERENCES master_personnel(personnel_id),
    FOREIGN KEY (approved_by) REFERENCES master_personnel(personnel_id)
);

-- Measurement Request Details Table
CREATE TABLE measurement_request_details (
    detail_id INT PRIMARY KEY AUTO_INCREMENT,
    request_id INT,
    parameter_id INT,
    location_id INT,
    points_count INT DEFAULT 1,
    unit_price DECIMAL(12, 2),
    subtotal DECIMAL(12, 2),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES measurement_requests(request_id),
    FOREIGN KEY (parameter_id) REFERENCES master_climate_parameters(parameter_id),
    FOREIGN KEY (location_id) REFERENCES master_sampling_locations(location_id)
);

-- Measurement Schedules Table
CREATE TABLE measurement_schedules (
    schedule_id INT PRIMARY KEY AUTO_INCREMENT,
    schedule_number VARCHAR(50) NOT NULL,
    request_id INT,
    measurement_date DATE,
    officer_id INT,
    status VARCHAR(50), -- scheduled, completed, rescheduled, canceled
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES measurement_requests(request_id),
    FOREIGN KEY (officer_id) REFERENCES master_personnel(personnel_id)
);

-- Test Reports Table
CREATE TABLE test_reports (
    report_id INT PRIMARY KEY AUTO_INCREMENT,
    report_number VARCHAR(50) NOT NULL,
    request_id INT,
    schedule_id INT,
    measurement_date DATE,
    issue_date DATE,
    client_id INT,
    officer_id INT,
    verified_by INT,
    approved_by INT,
    status VARCHAR(50), -- draft, verification, approved, published, revised
    page_count INT DEFAULT 1,
    notes TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES measurement_requests(request_id),
    FOREIGN KEY (schedule_id) REFERENCES measurement_schedules(schedule_id),
    FOREIGN KEY (client_id) REFERENCES master_clients(client_id),
    FOREIGN KEY (officer_id) REFERENCES master_personnel(personnel_id),
    FOREIGN KEY (verified_by) REFERENCES master_personnel(personnel_id),
    FOREIGN KEY (approved_by) REFERENCES master_personnel(personnel_id)
);

-- Temperature Measurement Results Table
CREATE TABLE temperature_measurement_results (
    result_id INT PRIMARY KEY AUTO_INCREMENT,
    report_id INT,
    location_id INT,
    sample_code VARCHAR(50) NOT NULL,
    measurement_time_start TIME,
    measurement_time_end TIME,
    parameter_id INT,
    measurement_value DECIMAL(8, 2),
    unit VARCHAR(20),
    method VARCHAR(100),
    threshold_id INT,
    notes TEXT,
    equipment_id INT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (report_id) REFERENCES test_reports(report_id),
    FOREIGN KEY (location_id) REFERENCES master_sampling_locations(location_id),
    FOREIGN KEY (parameter_id) REFERENCES master_climate_parameters(parameter_id),
    FOREIGN KEY (threshold_id) REFERENCES master_threshold_values(threshold_id),
    FOREIGN KEY (equipment_id) REFERENCES master_equipment(equipment_id)
);

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

-- Insert Regulations
INSERT INTO master_regulations (regulation_id, regulation_code, regulation_name, issuing_authority, regulation_type, issue_date, effective_date, description, file_path, is_active) 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 (parameter_id, parameter_code, parameter_name, parameter_group, unit, analysis_method, description, price, is_active) 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 (threshold_id, regulation_id, parameter_id, room_type, min_value, max_value, unit, description, is_active) 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 (client_id, client_code, client_name, address, city, province, postal_code, phone_number, email, contact_person, contact_position, business_field, is_active) 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 (personnel_id, employee_id, name, position, department, email, phone_number, certifications, is_active) 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 (equipment_id, equipment_code, equipment_name, brand, model, serial_number, calibration_date, next_calibration_date, parameter_id, specifications, is_active) 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 (location_id, client_id, location_name, address, room_type, description, is_active) 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 (request_id, request_number, client_id, request_date, planned_sampling_date, contact_person, status, notes, created_by, approved_by) 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 (detail_id, request_id, parameter_id, location_id, points_count, unit_price, subtotal) 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);

-- Measurement Schedule
INSERT INTO measurement_schedules (schedule_id, schedule_number, request_id, measurement_date, officer_id, status, notes) VALUES
(1, 'SCH/2024/04/001', 1, '2024-04-25', 3, 'completed', 'Measurement conducted during normal operating hours');

-- Test Report
INSERT INTO test_reports (report_id, report_number, request_id, schedule_id, measurement_date, issue_date, client_id, officer_id, verified_by, approved_by, status, page_count, notes) 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');

-- Temperature Measurement Results
INSERT INTO temperature_measurement_results (result_id, report_id, location_id, sample_code, measurement_time_start, measurement_time_end, parameter_id, measurement_value, unit, method, threshold_id, notes, equipment_id) 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); 