-- OPC UA scan results: stores the output of node discovery scans
-- triggered from the admin panel to help users find available variables.
CREATE TABLE IF NOT EXISTS opcua_scan_results (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    device_id UUID NOT NULL,
    project_id UUID NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'pending',  -- pending | completed | failed
    result JSONB,
    error_message TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    expires_at TIMESTAMPTZ NOT NULL DEFAULT NOW() + INTERVAL '15 minutes'
);

CREATE INDEX IF NOT EXISTS idx_opcua_scan_device ON opcua_scan_results(device_id);
CREATE INDEX IF NOT EXISTS idx_opcua_scan_project ON opcua_scan_results(project_id);
CREATE INDEX IF NOT EXISTS idx_opcua_scan_expires ON opcua_scan_results(expires_at);
