-- Migration 009: resource_usage_log + TTS-støtte
--
-- 1. Oppretter resource_usage_log for sporing av ressursforbruk (AI, Whisper, TTS, CAS, etc.)
-- 2. Legger grunnlaget for TTS-jobber (voice_preference i node-metadata)
--
-- Ref: docs/features/ressursforbruk.md

CREATE TABLE resource_usage_log (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    target_node_id UUID NOT NULL REFERENCES nodes(id),
    triggered_by UUID REFERENCES nodes(id),
    collection_id UUID REFERENCES nodes(id),
    resource_type TEXT NOT NULL,
    detail JSONB NOT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

CREATE INDEX idx_resource_usage_target ON resource_usage_log(target_node_id);
CREATE INDEX idx_resource_usage_triggered ON resource_usage_log(triggered_by);
CREATE INDEX idx_resource_usage_collection ON resource_usage_log(collection_id);
CREATE INDEX idx_resource_usage_type_time ON resource_usage_log(resource_type, created_at);
