-- Database Structure and Sample Data for Environmental Noise Measurement
-- Based on PMK RI No.2 Tahun 2023

-- ------------------------------------------------------------------------------
-- Master Tables - Noise Specific
-- ------------------------------------------------------------------------------

-- Master Area Types
CREATE TABLE IF NOT EXISTS master_area_types (
    area_type_id INT PRIMARY KEY,
    area_type_name VARCHAR(100) NOT NULL,
    description TEXT,
    status BOOLEAN DEFAULT TRUE,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50)
);

-- Master Noise Quality Standards
CREATE TABLE IF NOT EXISTS master_noise_standards (
    standard_id INT PRIMARY KEY,
    regulation_id INT NOT NULL,
    area_type_id INT NOT NULL,
    standard_value DECIMAL(5, 1) NOT NULL,
    unit VARCHAR(10) DEFAULT 'dB(A)',
    measurement_time VARCHAR(50),
    description TEXT,
    status BOOLEAN DEFAULT TRUE,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50),
    FOREIGN KEY (regulation_id) REFERENCES master_regulations(regulation_id),
    FOREIGN KEY (area_type_id) REFERENCES master_area_types(area_type_id)
);

-- Master Noise Measurement Methods
CREATE TABLE IF NOT EXISTS master_noise_methods (
    method_id INT PRIMARY KEY,
    method_code VARCHAR(50) NOT NULL,
    method_name VARCHAR(255) NOT NULL,
    description TEXT,
    standard_reference VARCHAR(100),
    status BOOLEAN DEFAULT TRUE,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50)
);

-- Master Noise Measurement Equipment
CREATE TABLE IF NOT EXISTS master_noise_equipment (
    equipment_id INT PRIMARY KEY,
    equipment_code VARCHAR(20) NOT NULL,
    equipment_name VARCHAR(100) NOT NULL,
    equipment_type VARCHAR(50),
    brand VARCHAR(100),
    model VARCHAR(100),
    serial_number VARCHAR(100),
    accuracy_type VARCHAR(20), -- Type 0, 1, 2 for SLM
    specifications TEXT,
    calibration_date DATE,
    next_calibration_date DATE,
    calibration_status VARCHAR(20),
    certificate_file VARCHAR(255),
    status BOOLEAN DEFAULT TRUE,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50)
);

-- ------------------------------------------------------------------------------
-- Transaction Tables - Noise Specific
-- ------------------------------------------------------------------------------

-- Noise Sampling Plan
CREATE TABLE IF NOT EXISTS trx_noise_sampling_plan (
    sampling_plan_id INT PRIMARY KEY,
    sampling_plan_code VARCHAR(50) NOT NULL,
    project_name VARCHAR(255) NOT NULL,
    client_id INT NOT NULL,
    planned_sampling_date DATE NOT NULL,
    sampling_location TEXT NOT NULL,
    point_count INT,
    approval_status VARCHAR(20) DEFAULT 'DRAFT',
    notes TEXT,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50)
);

-- Noise Measurement Results
CREATE TABLE IF NOT EXISTS trx_noise_measurement_results (
    measurement_id INT PRIMARY KEY,
    sampling_plan_id INT NOT NULL,
    report_code VARCHAR(50) NOT NULL,
    sampling_date DATE NOT NULL,
    start_time TIME,
    end_time TIME,
    weather_condition VARCHAR(100),
    temperature DECIMAL(5, 2),
    humidity DECIMAL(5, 2),
    wind_speed DECIMAL(5, 2),
    wind_direction VARCHAR(10),
    sampling_officers VARCHAR(100),
    report_status VARCHAR(20) DEFAULT 'DRAFT',
    notes TEXT,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50),
    FOREIGN KEY (sampling_plan_id) REFERENCES trx_noise_sampling_plan(sampling_plan_id)
);

-- Noise Measurement Details
CREATE TABLE IF NOT EXISTS trx_noise_measurement_details (
    measurement_detail_id INT PRIMARY KEY,
    measurement_id INT NOT NULL,
    point_code VARCHAR(20) NOT NULL,
    location_name VARCHAR(255) NOT NULL,
    area_description TEXT,
    area_type_id INT NOT NULL,
    latitude DECIMAL(10, 6),
    longitude DECIMAL(10, 6),
    measurement_height DECIMAL(4, 2),
    distance_from_source DECIMAL(6, 2),
    noise_source TEXT,
    noise_characteristics VARCHAR(50), -- Continuous, Intermittent, Impulsive, etc.
    method_id INT NOT NULL,
    equipment_id INT NOT NULL,
    day_value DECIMAL(5, 1) NOT NULL, -- Ld (day)
    evening_value DECIMAL(5, 1) NOT NULL, -- Le (evening)
    night_value DECIMAL(5, 1) NOT NULL, -- Ln (night)
    leq24_value DECIMAL(5, 1) NOT NULL, -- Leq 24 hours
    standard_id INT NOT NULL,
    compliance_status VARCHAR(20),
    recommendations TEXT,
    notes TEXT,
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50),
    FOREIGN KEY (measurement_id) REFERENCES trx_noise_measurement_results(measurement_id),
    FOREIGN KEY (area_type_id) REFERENCES master_area_types(area_type_id),
    FOREIGN KEY (method_id) REFERENCES master_noise_methods(method_id),
    FOREIGN KEY (equipment_id) REFERENCES master_noise_equipment(equipment_id),
    FOREIGN KEY (standard_id) REFERENCES master_noise_standards(standard_id)
);

-- Noise Control Recommendations
CREATE TABLE IF NOT EXISTS trx_noise_control_recommendations (
    recommendation_id INT PRIMARY KEY,
    measurement_detail_id INT NOT NULL,
    control_type VARCHAR(50) NOT NULL, -- Engineering, Administrative, PPE
    control_description TEXT NOT NULL,
    priority INT,
    estimated_cost DECIMAL(12, 2),
    estimated_reduction DECIMAL(5, 1),
    implementation_status VARCHAR(20) DEFAULT 'PLANNED',
    input_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    input_user VARCHAR(50),
    FOREIGN KEY (measurement_detail_id) REFERENCES trx_noise_measurement_details(measurement_detail_id)
);

-- ------------------------------------------------------------------------------
-- Sample Data Insertion - Master Tables
-- ------------------------------------------------------------------------------

-- Insert Data Master Area Types
INSERT INTO master_area_types (area_type_id, area_type_name, description, status, input_user)
VALUES 
(1, 'Residential Areas', 'Residential housing and settlements', TRUE, 'admin'),
(2, 'Commercial and Trade Areas', 'Commercial and service areas, including offices', TRUE, 'admin'),
(3, 'Office and Commercial Areas', 'Office and commercial areas', TRUE, 'admin'),
(4, 'Industrial Areas', 'Manufacturing industrial areas', TRUE, 'admin'),
(5, 'Hospitals and Healthcare Facilities', 'Hospitals, health centers, and other healthcare facilities', TRUE, 'admin'),
(6, 'Schools and Educational Facilities', 'Educational areas including schools, madrasas, colleges', TRUE, 'admin'),
(7, 'Places of Worship', 'Worship areas such as mosques, churches, temples, and other religious buildings', TRUE, 'admin');

-- Insert Data Master Noise Standards
INSERT INTO master_noise_standards (standard_id, regulation_id, area_type_id, standard_value, unit, measurement_time, description, status, input_user)
VALUES 
(1, 1, 1, 55.0, 'dB(A)', 'Leq 24 hours', 'Noise quality standard for residential areas', TRUE, 'admin'),
(2, 1, 2, 70.0, 'dB(A)', 'Leq 24 hours', 'Noise quality standard for commercial and trade areas', TRUE, 'admin'),
(3, 1, 3, 65.0, 'dB(A)', 'Leq 24 hours', 'Noise quality standard for office and commercial areas', TRUE, 'admin'),
(4, 1, 4, 70.0, 'dB(A)', 'Leq 24 hours', 'Noise quality standard for industrial areas', TRUE, 'admin'),
(5, 1, 5, 55.0, 'dB(A)', 'Leq 24 hours', 'Noise quality standard for hospitals and healthcare facilities', TRUE, 'admin'),
(6, 1, 6, 55.0, 'dB(A)', 'Leq during school hours', 'Noise quality standard for schools and educational facilities', TRUE, 'admin'),
(7, 1, 7, 55.0, 'dB(A)', 'Leq during worship times', 'Noise quality standard for places of worship', TRUE, 'admin');

-- Insert Data Master Noise Measurement Methods
INSERT INTO master_noise_methods (method_id, method_code, method_name, description, standard_reference, status, input_user)
VALUES 
(1, 'SNI-7231-2009', 'SNI 7231:2009 - Environmental Noise Measurement Method', 'Method for measuring environmental noise levels using Sound Level Meter', 'SNI 7231:2009', TRUE, 'admin'),
(2, 'KEPMEN-LH-48-1996', 'Ministry of Environment Decree No. 48 of 1996 - Appendix II', 'Noise level measurement method based on the Ministry of Environment Decree', 'Ministry of Environment Decree No.48 of 1996', TRUE, 'admin');

-- Insert Data Master Noise Measurement Equipment
INSERT INTO master_noise_equipment (equipment_id, equipment_code, equipment_name, equipment_type, brand, model, serial_number, accuracy_type, specifications, calibration_date, next_calibration_date, calibration_status, certificate_file, status, input_user)
VALUES 
(1, 'SLM-01', 'Sound Level Meter', 'SLM', 'RION', 'NL-52', '12345678', 'Type 1', 'Range: 20-130 dB, Accuracy: ±1.0 dB, Frequency range: 20 Hz - 20 kHz', '2024-02-15', '2025-02-15', 'VALID', '/documents/calibration/slm_rion_2024.pdf', TRUE, 'admin'),
(2, 'CAL-01', 'Acoustic Calibrator', 'Calibrator', 'RION', 'NC-74', '87654321', 'Class 1', 'Calibration level: 94 dB, Accuracy: ±0.3 dB', '2024-02-15', '2025-02-15', 'VALID', '/documents/calibration/cal_rion_2024.pdf', TRUE, 'admin'),
(3, 'SLM-02', 'Sound Level Meter', 'SLM', 'Cirrus', 'CR171B', 'CR123456', 'Type 1', 'Range: 20-140 dB, Accuracy: ±0.5 dB, Frequency range: 10 Hz - 20 kHz', '2023-11-20', '2024-11-20', 'VALID', '/documents/calibration/slm_cirrus_2023.pdf', TRUE, 'admin'),
(4, 'WS-01', 'Weather Station', 'Weather Meter', 'Kestrel', '5500', 'KW123456', 'N/A', 'Wind speed, direction, temperature, humidity, pressure measurement', '2023-10-10', '2024-10-10', 'VALID', '/documents/calibration/weather_kestrel_2023.pdf', TRUE, 'admin');

-- ------------------------------------------------------------------------------
-- Sample Data Insertion - Transaction Tables
-- ------------------------------------------------------------------------------

-- Sample Noise Sampling Plan
INSERT INTO trx_noise_sampling_plan (sampling_plan_id, sampling_plan_code, project_name, client_id, planned_sampling_date, sampling_location, point_count, approval_status, notes, input_date, input_user)
VALUES 
(1, 'SP-NOISE-2024-001', 'Environmental Noise Measurement at PT. Integrated Manufacturing Industries', 201, '2024-06-10', 'PT. Integrated Manufacturing Industries, Industrial Zone Block C5, Karawang, West Java', 6, 'APPROVED', 'Environmental noise measurement for compliance with standards', '2024-06-01 09:00:00', 'supervisor');

-- Sample Noise Measurement Results
INSERT INTO trx_noise_measurement_results (measurement_id, sampling_plan_id, report_code, sampling_date, start_time, end_time, weather_condition, temperature, humidity, wind_speed, wind_direction, sampling_officers, report_status, notes, input_date, input_user)
VALUES 
(1, 1, 'LHU/NOISE/06/2024/003', '2024-06-10', '08:00:00', '17:00:00', 'Sunny', 30.0, 65.0, 1.5, 'East', 'Agus Setiawan, Dina Wijaya', 'FINAL', 'Measurement proceeded smoothly as planned', '2024-06-10 17:30:00', 'analyst');

-- Sample Noise Measurement Details
INSERT INTO trx_noise_measurement_details (measurement_detail_id, measurement_id, point_code, location_name, area_description, area_type_id, latitude, longitude, measurement_height, distance_from_source, noise_source, noise_characteristics, method_id, equipment_id, day_value, evening_value, night_value, leq24_value, standard_id, compliance_status, recommendations, notes, input_date, input_user)
VALUES 
-- Main Factory Entrance
(1, 1, 'NL-1', 'Main Factory Entrance', 'Main factory gate area, moderate traffic level', 4, -6.372500, 107.524167, 1.5, 3.0, 'Vehicle traffic, loading/unloading activities', 'Intermittent', 1, 1, 67.8, 65.2, 62.1, 66.3, 4, 'COMPLY', '', 'Noise is quite high during morning and evening peak hours', '2024-06-10 17:35:00', 'analyst'),

-- Production Building
(2, 1, 'NL-2', 'Production Building', 'Main production building area, high activity level', 4, -6.372778, 107.524444, 1.5, 1.0, 'Production machinery, stamping, air compressors', 'Continuous, Impulsive', 1, 1, 73.4, 72.1, 68.5, 72.2, 4, 'NOT_COMPLY', 'Install sound absorbers, check machine maintenance', 'High noise from stamping machines and compressors', '2024-06-10 17:40:00', 'analyst'),

-- Office Building
(3, 1, 'NL-3', 'Office Building', 'Administrative office area, active HVAC system', 3, -6.372944, 107.525000, 1.5, 5.0, 'HVAC system, general office activities', 'Continuous, Low Frequency', 1, 1, 64.5, 62.3, 58.7, 63.2, 3, 'COMPLY', '', 'Noise primarily from air conditioning system', '2024-06-10 17:45:00', 'analyst'),

-- Facility Boundary (East)
(4, 1, 'NL-4', 'Facility Boundary (East)', 'Eastern factory boundary, close to public road', 4, -6.373111, 107.526111, 1.5, 10.0, 'Factory operations, public road traffic', 'Intermittent', 1, 1, 68.9, 67.2, 65.6, 67.8, 4, 'COMPLY', '', 'Highest noise from road traffic', '2024-06-10 17:50:00', 'analyst'),

-- Power Generator Area
(5, 1, 'NL-5', 'Power Generator Area', 'Generator area, very high noise level', 4, -6.373278, 107.524722, 1.5, 1.0, 'Power generators, cooling systems', 'Continuous, Tonal', 1, 1, 78.6, 77.5, 76.3, 77.9, 4, 'NOT_COMPLY', 'Install enclosure, add acoustic insulation', 'Noise very high even outside generator area', '2024-06-10 17:55:00', 'analyst'),

-- Nearest Residential Area
(6, 1, 'NL-6', 'Nearest Residential Area', 'Nearest residential area to factory', 1, -6.374167, 107.527222, 1.5, 300.0, 'Factory noise, general traffic', 'Intermittent, Background', 1, 1, 57.3, 55.6, 52.1, 55.9, 1, 'NOT_COMPLY', 'Install sound barriers, limit night operations', 'Factory noise still audible in residential area', '2024-06-10 18:00:00', 'analyst');

-- Sample Noise Control Recommendations
INSERT INTO trx_noise_control_recommendations (recommendation_id, measurement_detail_id, control_type, control_description, priority, estimated_cost, estimated_reduction, implementation_status, input_date, input_user)
VALUES 
-- Recommendations for Production Building
(1, 2, 'Engineering Control', 'Installation of sound absorbers on walls and ceiling of production area', 1, 75000000.00, 3.5, 'PLANNED', '2024-06-15 09:00:00', 'analyst'),
(2, 2, 'Engineering Control', 'Installation of enclosure for stamping machines', 1, 120000000.00, 8.0, 'PLANNED', '2024-06-15 09:05:00', 'analyst'),
(3, 2, 'Administrative Control', 'Preventive maintenance program for production machinery', 2, 0.00, 2.0, 'PLANNED', '2024-06-15 09:10:00', 'analyst'),
(4, 2, 'PPE', 'Provision of ear muffs for workers in production area', 1, 15000000.00, 0.0, 'PLANNED', '2024-06-15 09:15:00', 'analyst'),

-- Recommendations for Power Generator Area
(5, 5, 'Engineering Control', 'Installation of acoustic enclosure for generators', 1, 200000000.00, 15.0, 'PLANNED', '2024-06-15 09:20:00', 'analyst'),
(6, 5, 'Engineering Control', 'Installation of silencers on air intake and exhaust', 1, 85000000.00, 6.0, 'PLANNED', '2024-06-15 09:25:00', 'analyst'),
(7, 5, 'Administrative Control', 'Relocate generators further from office areas and property boundaries', 2, 350000000.00, 10.0, 'PLANNED', '2024-06-15 09:30:00', 'analyst'),

-- Recommendations for Nearest Residential Area
(8, 6, 'Engineering Control', 'Installation of noise barrier along factory boundary facing residential area', 1, 180000000.00, 5.0, 'PLANNED', '2024-06-15 09:35:00', 'analyst'),
(9, 6, 'Administrative Control', 'Restriction of noisy equipment operation during night time', 1, 0.00, 3.0, 'PLANNED', '2024-06-15 09:40:00', 'analyst'),
(10, 6, 'Administrative Control', 'Regular noise monitoring program at residential boundary', 2, 30000000.00, 0.0, 'PLANNED', '2024-06-15 09:45:00', 'analyst'); 