-- V013 Enterprise Observability
CREATE TABLE IF NOT EXISTS service_health_snapshots_v013 (id varchar(36) PRIMARY KEY,service_name varchar(100) NOT NULL,status varchar(30) NOT NULL,latency_ms integer NOT NULL DEFAULT 0,details jsonb NOT NULL DEFAULT '{}'::jsonb,checked_at timestamptz NOT NULL DEFAULT now());
CREATE INDEX IF NOT EXISTS ix_health_service_time_v013 ON service_health_snapshots_v013(service_name,checked_at DESC);
CREATE TABLE IF NOT EXISTS operational_incidents_v013 (id varchar(36) PRIMARY KEY,title varchar(250) NOT NULL,description text NOT NULL DEFAULT '',source varchar(100) NOT NULL DEFAULT 'platform',severity varchar(20) NOT NULL DEFAULT 'warning',status varchar(30) NOT NULL DEFAULT 'open',fingerprint varchar(128) NOT NULL DEFAULT '',owner_id varchar(36) REFERENCES users(id),evidence jsonb NOT NULL DEFAULT '{}'::jsonb,resolution text NOT NULL DEFAULT '',opened_at timestamptz NOT NULL DEFAULT now(),acknowledged_at timestamptz,resolved_at timestamptz);
CREATE TABLE IF NOT EXISTS observability_alert_policies_v013 (id varchar(36) PRIMARY KEY,name varchar(180) UNIQUE NOT NULL,metric_name varchar(180) NOT NULL,operator varchar(10) NOT NULL DEFAULT 'gt',threshold integer NOT NULL DEFAULT 0,window_minutes integer NOT NULL DEFAULT 5,severity varchar(20) NOT NULL DEFAULT 'warning',cooldown_minutes integer NOT NULL DEFAULT 30,enabled boolean NOT NULL DEFAULT true,channels jsonb NOT NULL DEFAULT '[]'::jsonb,last_triggered_at timestamptz,created_by varchar(36) NOT NULL REFERENCES users(id),created_at timestamptz NOT NULL DEFAULT now());
CREATE TABLE IF NOT EXISTS operational_log_events_v013 (id varchar(36) PRIMARY KEY,level varchar(20) NOT NULL,service varchar(100) NOT NULL,event_name varchar(160) NOT NULL,message text NOT NULL DEFAULT '',trace_id varchar(64) NOT NULL DEFAULT '',request_id varchar(64) NOT NULL DEFAULT '',user_id varchar(36) REFERENCES users(id),attributes jsonb NOT NULL DEFAULT '{}'::jsonb,created_at timestamptz NOT NULL DEFAULT now());
