-- Video Automation Pipeline - Settings Table
-- Version: 002
-- Description: Add settings table for storing configuration in database

-- Settings table for key-value configuration storage
CREATE TABLE IF NOT EXISTS settings (
    key VARCHAR(255) PRIMARY KEY,
    value TEXT NOT NULL DEFAULT '',
    description TEXT,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Apply trigger to auto-update updated_at
DROP TRIGGER IF EXISTS update_settings_updated_at ON settings;
CREATE TRIGGER update_settings_updated_at
    BEFORE UPDATE ON settings
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- Index for faster lookups
CREATE INDEX IF NOT EXISTS idx_settings_key ON settings(key);

-- Insert default settings (won't overwrite existing)
INSERT INTO settings (key, value, description) VALUES
    ('video_provider', 'grok', 'Active video provider: grok, leonardo, runway'),
    ('leonardo_api_key', '', 'Leonardo AI API key'),
    ('leonardo_resolution', '720', 'Leonardo video resolution: 480 or 720'),
    ('tts_provider', 'chatterbox', 'Active TTS provider: chatterbox, elevenlabs'),
    ('chatterbox_language', 'pt', 'ChatterBox language code'),
    ('image_provider', 'grok', 'Active image provider: dalle, grok, googleflow')
ON CONFLICT (key) DO NOTHING;
