-- SQL Migration for Alarm Recording Support
-- Add columns to track alarm recording status in sanasight_alarm_events table

-- Add encoder_event_id to store the encoder's event identifier
ALTER TABLE sanasight_alarm_events 
ADD COLUMN IF NOT EXISTS encoder_event_id VARCHAR(255) NULL 
COMMENT 'Event ID from encoder API for alarm recording';

-- Add recording_started flag
ALTER TABLE sanasight_alarm_events 
ADD COLUMN IF NOT EXISTS recording_started TINYINT(1) DEFAULT 0 
COMMENT 'Flag indicating if recording was initiated for this alarm';

-- Add recording_started_at timestamp
ALTER TABLE sanasight_alarm_events 
ADD COLUMN IF NOT EXISTS recording_started_at DATETIME NULL 
COMMENT 'Timestamp when recording was started';

-- Add recording_stopped flag
ALTER TABLE sanasight_alarm_events 
ADD COLUMN IF NOT EXISTS recording_stopped TINYINT(1) DEFAULT 0 
COMMENT 'Flag indicating if recording was stopped';

-- Add recording_stopped_at timestamp
ALTER TABLE sanasight_alarm_events 
ADD COLUMN IF NOT EXISTS recording_stopped_at DATETIME NULL 
COMMENT 'Timestamp when recording was stopped';

-- Create index on encoder_event_id for faster lookups
CREATE INDEX IF NOT EXISTS idx_encoder_event_id 
ON sanasight_alarm_events(encoder_event_id);

-- Create index on recording_started for filtering
CREATE INDEX IF NOT EXISTS idx_recording_started 
ON sanasight_alarm_events(recording_started);
