-- +goose Up
-- SQL in this section is executed when the migration is applied

-- Create navigation_items table for managing navigation
CREATE TABLE IF NOT EXISTS navigation_items (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,
    
    -- Basic info
    label VARCHAR(255) NOT NULL,
    url TEXT,
    icon VARCHAR(100),
    
    -- Type: internal, external, dropdown, page
    type VARCHAR(50) NOT NULL DEFAULT 'internal',
    
    -- Page reference (for type=page): links to existing pages
    page_type VARCHAR(100), -- e.g., 'blog', 'about', 'calendar', 'players', etc.
    page_id INTEGER, -- optional reference to specific content ID
    
    -- Visibility and display
    visible BOOLEAN NOT NULL DEFAULT true,
    display_order INTEGER NOT NULL DEFAULT 0,
    
    -- Parent for dropdown menus
    parent_id INTEGER REFERENCES navigation_items(id) ON DELETE CASCADE,
    
    -- Target
    target VARCHAR(20) DEFAULT '_self', -- _self, _blank
    
    -- Styling
    css_class VARCHAR(255),
    
    -- Permissions
    requires_auth BOOLEAN DEFAULT false,
    requires_admin BOOLEAN DEFAULT false
);

-- Create index for ordering and parent relationships
CREATE INDEX idx_navigation_items_order ON navigation_items(display_order);
CREATE INDEX idx_navigation_items_parent ON navigation_items(parent_id);
CREATE INDEX idx_navigation_items_visible ON navigation_items(visible);

-- Create social_links table for managing social media
CREATE TABLE IF NOT EXISTS social_links (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,
    
    platform VARCHAR(50) NOT NULL, -- facebook, instagram, youtube, twitter, tiktok, etc.
    url TEXT NOT NULL,
    display_order INTEGER NOT NULL DEFAULT 0,
    visible BOOLEAN NOT NULL DEFAULT true,
    icon VARCHAR(100) -- optional custom icon
);

CREATE INDEX idx_social_links_order ON social_links(display_order);
CREATE INDEX idx_social_links_visible ON social_links(visible);

-- Insert default navigation items if table is empty
INSERT INTO navigation_items (label, type, page_type, display_order, visible)
SELECT * FROM (VALUES
    ('Domů', 'page', 'home', 0, true),
    ('O klubu', 'page', 'about', 1, true),
    ('Kalendář', 'page', 'calendar', 2, true),
    ('Zápasy', 'page', 'matches', 3, true),
    ('Aktivity', 'page', 'activities', 4, true),
    ('Hráči', 'page', 'players', 5, true),
    ('Tabulky', 'page', 'tables', 6, true),
    ('Články', 'page', 'blog', 7, true),
    ('Videa', 'page', 'videos', 8, true),
    ('Fotogalerie', 'page', 'gallery', 9, true),
    ('Sponzoři', 'page', 'sponsors', 10, true),
    ('Kontakt', 'page', 'contact', 11, true)
) AS default_nav(label, type, page_type, display_order, visible)
WHERE NOT EXISTS (SELECT 1 FROM navigation_items);

-- Migrate existing social links from settings if they exist
-- This will be handled in application code as we don't want to directly access settings table

-- Add navigation settings to settings table if not exists
DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'settings') THEN
        -- Add column for navigation customization enabled
        IF NOT EXISTS (SELECT 1 FROM information_schema.columns 
                      WHERE table_name = 'settings' AND column_name = 'custom_navigation_enabled') THEN
            ALTER TABLE settings ADD COLUMN custom_navigation_enabled BOOLEAN DEFAULT false;
        END IF;
        
        -- Add column for showing social links in nav
        IF NOT EXISTS (SELECT 1 FROM information_schema.columns 
                      WHERE table_name = 'settings' AND column_name = 'show_social_in_nav') THEN
            ALTER TABLE settings ADD COLUMN show_social_in_nav BOOLEAN DEFAULT true;
        END IF;
    END IF;
END $$;
