-- Create languages table
CREATE TABLE IF NOT EXISTS languages (
    id VARCHAR(5) PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    native_name VARCHAR(100) NOT NULL,
    code VARCHAR(10) NOT NULL UNIQUE,
    is_default BOOLEAN DEFAULT FALSE,
    is_active BOOLEAN DEFAULT TRUE,
    sort_order INTEGER DEFAULT 0,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Create translations table
CREATE TABLE IF NOT EXISTS translations (
    id SERIAL PRIMARY KEY,
    key VARCHAR(200) NOT NULL,
    language_code VARCHAR(10) NOT NULL,
    value TEXT NOT NULL,
    context VARCHAR(100),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (language_code) REFERENCES languages(code) ON DELETE CASCADE
);

-- Create content_translations table
CREATE TABLE IF NOT EXISTS content_translations (
    id SERIAL PRIMARY KEY,
    content_type VARCHAR(50) NOT NULL,
    content_id INTEGER NOT NULL,
    language_code VARCHAR(10) NOT NULL,
    title VARCHAR(500),
    content TEXT,
    excerpt TEXT,
    meta_title VARCHAR(200),
    meta_description VARCHAR(500),
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (language_code) REFERENCES languages(code) ON DELETE CASCADE
);

-- Create user_language_preferences table
CREATE TABLE IF NOT EXISTS user_language_preferences (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL UNIQUE,
    language_code VARCHAR(10) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    FOREIGN KEY (language_code) REFERENCES languages(code) ON DELETE CASCADE
);

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_translations_key ON translations(key);
CREATE INDEX IF NOT EXISTS idx_translations_language_key ON translations(language_code, key);
CREATE INDEX IF NOT EXISTS idx_translations_context ON translations(context);
CREATE INDEX IF NOT EXISTS idx_content_translations_content ON content_translations(content_type, content_id);
CREATE INDEX IF NOT EXISTS idx_content_translations_language ON content_translations(language_code);
CREATE INDEX IF NOT EXISTS idx_languages_active ON languages(is_active) WHERE is_active = TRUE;
CREATE INDEX IF NOT EXISTS idx_languages_default ON languages(is_default) WHERE is_default = TRUE;

-- Insert default languages
INSERT INTO languages (id, name, native_name, code, is_default, is_active, sort_order) VALUES
('cs', 'Czech', 'Čeština', 'cs', true, true, 1),
('en', 'English', 'English', 'en', false, true, 2)
ON CONFLICT (code) DO UPDATE SET
    name = EXCLUDED.name,
    native_name = EXCLUDED.native_name,
    is_default = EXCLUDED.is_default,
    is_active = EXCLUDED.is_active,
    sort_order = EXCLUDED.sort_order;

-- Insert basic Czech translations
INSERT INTO translations (key, language_code, value, context) VALUES
-- Common
('common.welcome_message', 'cs', 'Vítejte v našem fotbalovém klubu', 'common'),
('common.welcome_subtitle', 'cs', 'Oficiální stránky fotbalového klubu', 'common'),

-- Navigation
('nav.home', 'cs', 'Domů', 'navbar'),
('nav.news', 'cs', 'Aktuality', 'navbar'),
('nav.matches', 'cs', 'Zápasy', 'navbar'),
('nav.players', 'cs', 'Hráči', 'navbar'),
('nav.gallery', 'cs', 'Galerie', 'navbar'),
('nav.videos', 'cs', 'Videa', 'navbar'),
('nav.contact', 'cs', 'Kontakt', 'navbar'),
('nav.about', 'cs', 'O klubu', 'navbar'),
('nav.activities', 'cs', 'Aktivity', 'navbar'),
('nav.sponsors', 'cs', 'Sponzoři', 'navbar'),
('nav.articles', 'cs', 'Články', 'navbar'),
('nav.tables', 'cs', 'Tabulky', 'navbar'),
('nav.calendar', 'cs', 'Kalendář', 'navbar'),
('nav.shop', 'cs', 'Obchod', 'navbar'),

-- Common actions
('action.save', 'cs', 'Uložit', 'common'),
('action.cancel', 'cs', 'Zrušit', 'common'),
('action.edit', 'cs', 'Upravit', 'common'),
('action.delete', 'cs', 'Smazat', 'common'),
('action.create', 'cs', 'Vytvořit', 'common'),
('action.update', 'cs', 'Aktualizovat', 'common'),
('action.publish', 'cs', 'Publikovat', 'common'),
('action.unpublish', 'cs', 'Skrýt', 'common'),
('action.search', 'cs', 'Hledat', 'common'),
('action.more', 'cs', 'Více', 'common'),

-- Forms
('form.required', 'cs', 'Toto pole je povinné', 'form'),
('form.email', 'cs', 'E-mail', 'form'),
('form.password', 'cs', 'Heslo', 'form'),
('form.name', 'cs', 'Jméno', 'form'),
('form.message', 'cs', 'Zpráva', 'form'),
('form.submit', 'cs', 'Odeslat', 'form'),

-- Messages
('msg.success', 'cs', 'Operace proběhla úspěšně', 'message'),
('msg.error', 'cs', 'Došlo k chybě', 'message'),
('msg.loading', 'cs', 'Načítám...', 'message'),
('msg.no_data', 'cs', 'Žádná data', 'message'),

-- Admin
('admin.dashboard', 'cs', 'Nástěnka', 'admin'),
('admin.articles', 'cs', 'Články', 'admin'),
('admin.matches', 'cs', 'Zápasy', 'admin'),
('admin.players', 'cs', 'Hráči', 'admin'),
('admin.settings', 'cs', 'Nastavení', 'admin'),
('admin.users', 'cs', 'Uživatelé', 'admin'),

-- Content
('content.read_more', 'cs', 'Číst více', 'content'),
('content.published_at', 'cs', 'Publikováno', 'content'),
('content.updated_at', 'cs', 'Aktualizováno', 'content'),
('content.author', 'cs', 'Autor', 'content'),

-- Matches
('match.date', 'cs', 'Datum', 'match'),
('match.time', 'cs', 'Čas', 'match'),
('match.place', 'cs', 'Místo', 'match'),
('match.result', 'cs', 'Výsledek', 'match'),
('match.score', 'cs', 'Skóre', 'match'),
('match.team_home', 'cs', 'Domácí', 'match'),
('match.team_away', 'cs', 'Hosté', 'match'),

-- Teams
('team.name', 'cs', 'Název týmu', 'team'),
('team.players', 'cs', 'Hráči', 'team'),
('team.coach', 'cs', 'Trenér', 'team'),
('team.category', 'cs', 'Kategorie', 'team'),

-- Gallery
('homepage.gallery', 'cs', 'Galerie', 'gallery'),
('gallery.albums', 'cs', 'Alba', 'gallery'),
('gallery.photos', 'cs', 'Fotky', 'gallery'),
('gallery.view_all', 'cs', 'Zobrazit vše', 'gallery'),

-- Search
('search.placeholder', 'cs', 'Hledat...', 'search'),
('search.results', 'cs', 'Výsledky hledání', 'search'),
('search.no_results', 'cs', 'Nebyly nalezeny žádné výsledky', 'search'),

-- Pagination
('pagination.previous', 'cs', 'Předchozí', 'pagination'),
('pagination.next', 'cs', 'Další', 'pagination'),
('pagination.page', 'cs', 'Strana', 'pagination'),
('pagination.of', 'cs', 'z', 'pagination'),

-- Date/Time
('date.today', 'cs', 'Dnes', 'date'),
('date.yesterday', 'cs', 'Včera', 'date'),
('date.tomorrow', 'cs', 'Zítra', 'date'),
('date.format', 'cs', 'DD.MM.YYYY', 'date'),
('time.format', 'cs', 'HH:mm', 'date')
ON CONFLICT (key, language_code) DO NOTHING;

-- Insert basic English translations
INSERT INTO translations (key, language_code, value, context) VALUES
-- Common
('common.welcome_message', 'en', 'Welcome to our football club', 'common'),
('common.welcome_subtitle', 'en', 'Official website of the football club', 'common'),

-- Navigation
('nav.home', 'en', 'Home', 'navbar'),
('nav.news', 'en', 'News', 'navbar'),
('nav.matches', 'en', 'Matches', 'navbar'),
('nav.players', 'en', 'Players', 'navbar'),
('nav.gallery', 'en', 'Gallery', 'navbar'),
('nav.videos', 'en', 'Videos', 'navbar'),
('nav.contact', 'en', 'Contact', 'navbar'),
('nav.about', 'en', 'About', 'navbar'),
('nav.activities', 'en', 'Activities', 'navbar'),
('nav.sponsors', 'en', 'Sponsors', 'navbar'),
('nav.articles', 'en', 'Articles', 'navbar'),
('nav.tables', 'en', 'Tables', 'navbar'),
('nav.calendar', 'en', 'Calendar', 'navbar'),
('nav.shop', 'en', 'Shop', 'navbar'),

-- Common actions
('action.save', 'en', 'Save', 'common'),
('action.cancel', 'en', 'Cancel', 'common'),
('action.edit', 'en', 'Edit', 'common'),
('action.delete', 'en', 'Delete', 'common'),
('action.create', 'en', 'Create', 'common'),
('action.update', 'en', 'Update', 'common'),
('action.publish', 'en', 'Publish', 'common'),
('action.unpublish', 'en', 'Unpublish', 'common'),
('action.search', 'en', 'Search', 'common'),
('action.more', 'en', 'More', 'common'),

-- Forms
('form.required', 'en', 'This field is required', 'form'),
('form.email', 'en', 'Email', 'form'),
('form.password', 'en', 'Password', 'form'),
('form.name', 'en', 'Name', 'form'),
('form.message', 'en', 'Message', 'form'),
('form.submit', 'en', 'Submit', 'form'),

-- Messages
('msg.success', 'en', 'Operation successful', 'message'),
('msg.error', 'en', 'An error occurred', 'message'),
('msg.loading', 'en', 'Loading...', 'message'),
('msg.no_data', 'en', 'No data', 'message'),

-- Admin
('admin.dashboard', 'en', 'Dashboard', 'admin'),
('admin.articles', 'en', 'Articles', 'admin'),
('admin.matches', 'en', 'Matches', 'admin'),
('admin.players', 'en', 'Players', 'admin'),
('admin.settings', 'en', 'Settings', 'admin'),
('admin.users', 'en', 'Users', 'admin'),

-- Content
('content.read_more', 'en', 'Read more', 'content'),
('content.published_at', 'en', 'Published', 'content'),
('content.updated_at', 'en', 'Updated', 'content'),
('content.author', 'en', 'Author', 'content'),

-- Matches
('match.date', 'en', 'Date', 'match'),
('match.time', 'en', 'Time', 'match'),
('match.place', 'en', 'Place', 'match'),
('match.result', 'en', 'Result', 'match'),
('match.score', 'en', 'Score', 'match'),
('match.team_home', 'en', 'Home', 'match'),
('match.team_away', 'en', 'Away', 'match'),

-- Teams
('team.name', 'en', 'Team name', 'team'),
('team.players', 'en', 'Players', 'team'),
('team.coach', 'en', 'Coach', 'team'),
('team.category', 'en', 'Category', 'team'),

-- Gallery
('homepage.gallery', 'en', 'Gallery', 'gallery'),
('gallery.albums', 'en', 'Albums', 'gallery'),
('gallery.photos', 'en', 'Photos', 'gallery'),
('gallery.view_all', 'en', 'View all', 'gallery'),

-- Search
('search.placeholder', 'en', 'Search...', 'search'),
('search.results', 'en', 'Search results', 'search'),
('search.no_results', 'en', 'No results found', 'search'),

-- Pagination
('pagination.previous', 'en', 'Previous', 'pagination'),
('pagination.next', 'en', 'Next', 'pagination'),
('pagination.page', 'en', 'Page', 'pagination'),
('pagination.of', 'en', 'of', 'pagination'),

-- Date/Time
('date.today', 'en', 'Today', 'date'),
('date.yesterday', 'en', 'Yesterday', 'date'),
('date.tomorrow', 'en', 'Tomorrow', 'date'),
('date.format', 'en', 'MM/DD/YYYY', 'date'),
('time.format', 'en', 'HH:mm', 'date')
ON CONFLICT (key, language_code) DO NOTHING;
