-- =========================================================
-- Migration 016: Training Media (Audio/Video)
-- =========================================================

CREATE TABLE IF NOT EXISTS training_media (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    module_id UUID NOT NULL REFERENCES training_modules(id) ON DELETE CASCADE,
    content_id UUID REFERENCES training_module_content(id),
    media_type VARCHAR(10) NOT NULL CHECK (media_type IN ('audio', 'video')),
    status VARCHAR(20) NOT NULL DEFAULT 'processing',
    bucket VARCHAR(100) NOT NULL,
    object_key VARCHAR(500) NOT NULL,
    file_size_bytes BIGINT,
    duration_seconds FLOAT,
    mime_type VARCHAR(50),
    voice_model VARCHAR(100),
    language VARCHAR(10) DEFAULT 'de',
    metadata JSONB DEFAULT '{}'::jsonb,
    error_message TEXT,
    generated_by VARCHAR(50),
    is_published BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_training_media_module ON training_media(module_id);
CREATE INDEX IF NOT EXISTS idx_training_media_type ON training_media(module_id, media_type);
CREATE INDEX IF NOT EXISTS idx_training_media_published ON training_media(module_id, media_type, is_published) WHERE is_published = true;
