-- Emergency Drill & Response DDL
CREATE TABLE IF NOT EXISTS disp_pipe_burst_simulation (
    id BIGSERIAL PRIMARY KEY,
    simulation_no VARCHAR(50) UNIQUE,
    location VARCHAR(200),
    lng DOUBLE PRECISION,
    lat DOUBLE PRECISION,
    pipe_diameter DOUBLE PRECISION,
    pipe_material VARCHAR(50),
    affected_radius INT,
    affected_users INT,
    estimated_water_loss DOUBLE PRECISION,
    estimated_repair_hours INT,
    pressure_drop DOUBLE PRECISION,
    affected_valves TEXT,
    status VARCHAR(20) DEFAULT 'COMPLETED',
    created_time TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS disp_water_quality_incident (
    id BIGSERIAL PRIMARY KEY,
    incident_no VARCHAR(50) UNIQUE,
    location VARCHAR(200),
    pollutant_type VARCHAR(50),
    detected_value DOUBLE PRECISION,
    standard_value DOUBLE PRECISION,
    severity VARCHAR(20),
    status VARCHAR(20) DEFAULT 'DETECTED',
    matched_plan VARCHAR(200),
    detected_time TIMESTAMP,
    response_start_time TIMESTAMP,
    resolved_time TIMESTAMP,
    response_log TEXT,
    resolution TEXT,
    created_time TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS disp_emergency_drill (
    id BIGSERIAL PRIMARY KEY,
    drill_no VARCHAR(50) UNIQUE,
    name VARCHAR(200),
    drill_type VARCHAR(30),
    description TEXT,
    status VARCHAR(20) DEFAULT 'PLANNED',
    planned_time TIMESTAMP,
    started_time TIMESTAMP,
    completed_time TIMESTAMP,
    created_time TIMESTAMP DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS disp_drill_evaluation (
    id BIGSERIAL PRIMARY KEY,
    drill_id BIGINT REFERENCES disp_emergency_drill(id),
    score INT,
    response_time_score INT,
    coordination_score INT,
    overall_rating VARCHAR(20),
    findings TEXT,
    recommendations TEXT,
    evaluator VARCHAR(50),
    created_time TIMESTAMP DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_pbs_status ON disp_pipe_burst_simulation(status);
CREATE INDEX IF NOT EXISTS idx_wqi_status ON disp_water_quality_incident(status);
CREATE INDEX IF NOT EXISTS idx_wqi_type ON disp_water_quality_incident(pollutant_type);
CREATE INDEX IF NOT EXISTS idx_ed_status ON disp_emergency_drill(status);
