ALTER TABLE observations
    ADD COLUMN firmware_version VARCHAR(255) NULL;

CREATE TABLE IF NOT EXISTS timeline_events (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    subject_type VARCHAR(32) NOT NULL,
    subject_id BIGINT NOT NULL,
    event_type VARCHAR(64) NOT NULL,
    event_time TIMESTAMP NOT NULL,
    asset_id BIGINT NULL,
    component_id BIGINT NULL,
    firmware_version VARCHAR(255) NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_timeline_events_asset
        FOREIGN KEY (asset_id) REFERENCES assets(id)
        ON DELETE SET NULL ON UPDATE CASCADE,
    CONSTRAINT fk_timeline_events_component
        FOREIGN KEY (component_id) REFERENCES components(id)
        ON DELETE SET NULL ON UPDATE CASCADE
);

CREATE INDEX idx_timeline_events_subject_time ON timeline_events(subject_type, subject_id, event_time, id);
CREATE INDEX idx_timeline_events_asset_time ON timeline_events(asset_id, event_time, id);
CREATE INDEX idx_timeline_events_component_time ON timeline_events(component_id, event_time, id);
CREATE INDEX idx_observations_component_time ON observations(component_id, observed_at, id);
