-- Cloud + local LLM provider configuration
CREATE TABLE IF NOT EXISTS llm_providers (
    id SERIAL PRIMARY KEY,
    label VARCHAR(128) NOT NULL,
    provider_type VARCHAR(48) NOT NULL DEFAULT 'custom_openai',
    api_base_url TEXT,
    api_key TEXT,
    model VARCHAR(128) NOT NULL DEFAULT 'deepseek-chat',
    is_active BOOLEAN NOT NULL DEFAULT FALSE,
    is_default BOOLEAN NOT NULL DEFAULT FALSE,
    extra_config JSONB NOT NULL DEFAULT '{}',
    last_test_status VARCHAR(64),
    last_test_message TEXT,
    last_test_at TIMESTAMPTZ,
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_llm_providers_active ON llm_providers (is_active);
CREATE INDEX IF NOT EXISTS idx_llm_providers_default ON llm_providers (is_default) WHERE is_default = TRUE;

-- Seed local Ollama as default if empty
INSERT INTO llm_providers (label, provider_type, api_base_url, model, is_active, is_default)
SELECT 'Ollama lokaal', 'ollama', NULL, 'qwen3:8b', TRUE, TRUE
WHERE NOT EXISTS (SELECT 1 FROM llm_providers);
