-- ============================================================================
-- Add extended fields to meters table
-- These fields store additional technical and operational data for meters
-- All fields are nullable (not required)
-- ============================================================================

-- Communication & Network Fields
ALTER TABLE meters ADD COLUMN IF NOT EXISTS protocol VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS mac VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS gateway VARCHAR(100);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS network_mode VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS phone_id VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS phone_model VARCHAR(100);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS phone_name VARCHAR(100);

-- Voltage & Power Fields
ALTER TABLE meters ADD COLUMN IF NOT EXISTS voltage DECIMAL(10, 2);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS voltage_rtu DECIMAL(10, 2);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS voltage_status VARCHAR(50);

-- Signal & Communication Quality
ALTER TABLE meters ADD COLUMN IF NOT EXISTS signal INTEGER;

-- Status Fields
ALTER TABLE meters ADD COLUMN IF NOT EXISTS storage_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS flow_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS open_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS actuator_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS counter_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS leakage_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS burst_status VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS valid_status VARCHAR(50);

-- Security & Alerts
ALTER TABLE meters ADD COLUMN IF NOT EXISTS magnetic_attack BOOLEAN;
ALTER TABLE meters ADD COLUMN IF NOT EXISTS realtime_information_flag BOOLEAN;

-- Flow Measurements
ALTER TABLE meters ADD COLUMN IF NOT EXISTS current_flow DECIMAL(15, 4);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS total_flow_reverse DECIMAL(15, 4);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS current_flow_reverse DECIMAL(15, 4);

-- Protocol Fields (M-Bus/LoRaWAN specific)
ALTER TABLE meters ADD COLUMN IF NOT EXISTS l_field VARCHAR(10);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS c_field VARCHAR(10);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS ver VARCHAR(10);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS dev VARCHAR(20);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS ci_field VARCHAR(10);

-- Company & Manufacturer Info
ALTER TABLE meters ADD COLUMN IF NOT EXISTS company_abbreviation VARCHAR(50);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS manufacturer VARCHAR(100);

-- Geolocation
ALTER TABLE meters ADD COLUMN IF NOT EXISTS latitude DECIMAL(10, 8);
ALTER TABLE meters ADD COLUMN IF NOT EXISTS longitude DECIMAL(11, 8);

-- Additional Data (JSON for flexible data storage)
ALTER TABLE meters ADD COLUMN IF NOT EXISTS data JSONB;

-- ============================================================================
-- Add indexes for commonly queried fields
-- ============================================================================

CREATE INDEX IF NOT EXISTS idx_meters_protocol ON meters(protocol);
CREATE INDEX IF NOT EXISTS idx_meters_mac ON meters(mac);
CREATE INDEX IF NOT EXISTS idx_meters_gateway ON meters(gateway);
CREATE INDEX IF NOT EXISTS idx_meters_manufacturer ON meters(manufacturer);
CREATE INDEX IF NOT EXISTS idx_meters_flow_status ON meters(flow_status);
CREATE INDEX IF NOT EXISTS idx_meters_leakage_status ON meters(leakage_status);
CREATE INDEX IF NOT EXISTS idx_meters_geolocation ON meters(latitude, longitude) WHERE latitude IS NOT NULL AND longitude IS NOT NULL;

-- ============================================================================
-- Add comments for documentation
-- ============================================================================

COMMENT ON COLUMN meters.protocol IS 'Communication protocol (LoRa, LoRaWAN, NB-IoT, etc.)';
COMMENT ON COLUMN meters.mac IS 'MAC address of the device';
COMMENT ON COLUMN meters.gateway IS 'Gateway identifier or name';
COMMENT ON COLUMN meters.network_mode IS 'Network operation mode';
COMMENT ON COLUMN meters.voltage IS 'Battery voltage (V)';
COMMENT ON COLUMN meters.voltage_rtu IS 'RTU voltage (V)';
COMMENT ON COLUMN meters.voltage_status IS 'Battery status (OK, LOW, CRITICAL)';
COMMENT ON COLUMN meters.signal IS 'Signal strength (RSSI or similar)';
COMMENT ON COLUMN meters.storage_status IS 'Internal storage status';
COMMENT ON COLUMN meters.flow_status IS 'Flow measurement status';
COMMENT ON COLUMN meters.leakage_status IS 'Leak detection status';
COMMENT ON COLUMN meters.burst_status IS 'Burst pipe detection status';
COMMENT ON COLUMN meters.magnetic_attack IS 'Magnetic tampering detected';
COMMENT ON COLUMN meters.realtime_information_flag IS 'Real-time data available';
COMMENT ON COLUMN meters.current_flow IS 'Current flow rate (m³/h or L/h)';
COMMENT ON COLUMN meters.total_flow_reverse IS 'Total reverse flow accumulated';
COMMENT ON COLUMN meters.current_flow_reverse IS 'Current reverse flow rate';
COMMENT ON COLUMN meters.l_field IS 'M-Bus L-Field (length)';
COMMENT ON COLUMN meters.c_field IS 'M-Bus C-Field (control)';
COMMENT ON COLUMN meters.ver IS 'Protocol version';
COMMENT ON COLUMN meters.dev IS 'Device type identifier';
COMMENT ON COLUMN meters.ci_field IS 'M-Bus CI-Field (control information)';
COMMENT ON COLUMN meters.latitude IS 'Latitude coordinate (WGS84)';
COMMENT ON COLUMN meters.longitude IS 'Longitude coordinate (WGS84)';
COMMENT ON COLUMN meters.data IS 'Additional flexible data storage (JSON)';

-- ============================================================================
-- Verify the changes
-- ============================================================================

SELECT 
    column_name, 
    data_type, 
    is_nullable,
    column_default
FROM information_schema.columns 
WHERE table_name = 'meters' 
  AND column_name IN (
    'protocol', 'mac', 'gateway', 'network_mode', 'phone_id', 'phone_model', 
    'phone_name', 'voltage', 'voltage_rtu', 'voltage_status', 'signal',
    'storage_status', 'flow_status', 'open_status', 'actuator_status',
    'counter_status', 'leakage_status', 'burst_status', 'valid_status',
    'magnetic_attack', 'realtime_information_flag', 'current_flow',
    'total_flow_reverse', 'current_flow_reverse', 'l_field', 'c_field',
    'ver', 'dev', 'ci_field', 'company_abbreviation', 'manufacturer',
    'latitude', 'longitude', 'data'
  )
ORDER BY ordinal_position;
