-- I18N Feature Database Initialization

-- Translations table
CREATE TABLE IF NOT EXISTS translations (
    id SERIAL PRIMARY KEY,
    key VARCHAR(255) NOT NULL,
    namespace VARCHAR(100) NOT NULL DEFAULT 'common',
    locale VARCHAR(10) NOT NULL,
    source_text TEXT NOT NULL,
    translated_text TEXT NOT NULL,
    quality_score DECIMAL(3, 2) DEFAULT 0.00,
    provider VARCHAR(50),
    from_cache BOOLEAN DEFAULT FALSE,
    from_static BOOLEAN DEFAULT FALSE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    UNIQUE(key, namespace, locale)
);

-- Glossary table
CREATE TABLE IF NOT EXISTS glossary (
    id SERIAL PRIMARY KEY,
    source VARCHAR(255) NOT NULL,
    category VARCHAR(100) NOT NULL DEFAULT 'general',
    translations JSONB NOT NULL DEFAULT '{}',
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    UNIQUE(source, category)
);

-- Locales table
CREATE TABLE IF NOT EXISTS locales (
    code VARCHAR(10) PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    native_name VARCHAR(100) NOT NULL,
    flag VARCHAR(10),
    rtl BOOLEAN DEFAULT FALSE,
    enabled BOOLEAN DEFAULT TRUE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Default locales
INSERT INTO locales (code, name, native_name, flag, rtl) VALUES
    ('en', 'English', 'English', '🇺🇸', false),
    ('es', 'Spanish', 'Español', '🇪🇸', false),
    ('fr', 'French', 'Français', '🇫🇷', false),
    ('de', 'German', 'Deutsch', '🇩🇪', false),
    ('ja', 'Japanese', '日本語', '🇯🇵', false),
    ('ko', 'Korean', '한국어', '🇰🇷', false),
    ('zh', 'Chinese', '中文', '🇨🇳', false),
    ('ar', 'Arabic', 'العربية', '🇸🇦', true),
    ('he', 'Hebrew', 'עברית', '🇮🇱', true)
ON CONFLICT (code) DO NOTHING;

-- Indexes
CREATE INDEX IF NOT EXISTS idx_translations_key_locale ON translations(key, locale);
CREATE INDEX IF NOT EXISTS idx_translations_namespace ON translations(namespace);
CREATE INDEX IF NOT EXISTS idx_glossary_category ON glossary(category);
