-- ============================================================
-- Add Users and AI Chat tables to Norda Biznes database
-- ============================================================
-- Created: 2025-11-23
-- Description: Adds user authentication and AI chat functionality

-- ============================================================
-- USERS
-- ============================================================

CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    name VARCHAR(255),
    company_name VARCHAR(255),
    phone VARCHAR(50),

    -- Status
    is_active BOOLEAN DEFAULT TRUE,
    is_verified BOOLEAN DEFAULT FALSE,
    is_admin BOOLEAN DEFAULT FALSE,

    -- Timestamps
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_login TIMESTAMP,
    verified_at TIMESTAMP,

    -- Email verification
    verification_token VARCHAR(255),
    verification_token_expires TIMESTAMP
);

CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_users_verification_token ON users(verification_token);

-- ============================================================
-- AI CHAT CONVERSATIONS
-- ============================================================

CREATE TABLE IF NOT EXISTS ai_chat_conversations (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,

    -- Conversation details
    title VARCHAR(255),
    conversation_type VARCHAR(50) DEFAULT 'general',

    -- Timestamps
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_active BOOLEAN DEFAULT TRUE,

    -- Metrics
    message_count INTEGER DEFAULT 0,
    model_name VARCHAR(100)
);

CREATE INDEX idx_chat_conversations_user ON ai_chat_conversations(user_id);
CREATE INDEX idx_chat_conversations_updated ON ai_chat_conversations(updated_at DESC);

-- ============================================================
-- AI CHAT MESSAGES
-- ============================================================

CREATE TABLE IF NOT EXISTS ai_chat_messages (
    id SERIAL PRIMARY KEY,
    conversation_id INTEGER NOT NULL REFERENCES ai_chat_conversations(id) ON DELETE CASCADE,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- Message content
    role VARCHAR(20) NOT NULL, -- 'user' or 'assistant'
    content TEXT NOT NULL,

    -- Metrics
    tokens_input INTEGER,
    tokens_output INTEGER,
    cost_usd NUMERIC(10, 6),
    latency_ms INTEGER,

    -- Flags
    edited BOOLEAN DEFAULT FALSE,
    regenerated BOOLEAN DEFAULT FALSE
);

CREATE INDEX idx_chat_messages_conversation ON ai_chat_messages(conversation_id);
CREATE INDEX idx_chat_messages_created ON ai_chat_messages(created_at);

-- ============================================================
-- AI API COST LOG (for analytics)
-- ============================================================

CREATE TABLE IF NOT EXISTS ai_api_costs (
    id SERIAL PRIMARY KEY,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    -- API details
    api_provider VARCHAR(50), -- 'gemini'
    model_name VARCHAR(100),
    feature VARCHAR(100), -- 'ai_chat', 'general', etc.

    -- User context
    user_id INTEGER REFERENCES users(id),

    -- Token usage
    input_tokens INTEGER,
    output_tokens INTEGER,
    total_tokens INTEGER,

    -- Costs
    input_cost NUMERIC(10, 6),
    output_cost NUMERIC(10, 6),
    total_cost NUMERIC(10, 6),

    -- Status
    success BOOLEAN DEFAULT TRUE,
    error_message TEXT,
    latency_ms INTEGER,

    -- Privacy (hash only, not storing prompts)
    prompt_hash VARCHAR(64)
);

CREATE INDEX idx_api_costs_timestamp ON ai_api_costs(timestamp DESC);
CREATE INDEX idx_api_costs_user ON ai_api_costs(user_id);
CREATE INDEX idx_api_costs_feature ON ai_api_costs(feature);

-- ============================================================
-- VERIFICATION
-- ============================================================

-- Verify tables were created
DO $$
BEGIN
    RAISE NOTICE 'Tables created successfully:';
    RAISE NOTICE '- users';
    RAISE NOTICE '- ai_chat_conversations';
    RAISE NOTICE '- ai_chat_messages';
    RAISE NOTICE '- ai_api_costs';
END $$;

-- Count existing records
SELECT
    'users' as table_name,
    COUNT(*) as row_count
FROM users
UNION ALL
SELECT
    'ai_chat_conversations',
    COUNT(*)
FROM ai_chat_conversations
UNION ALL
SELECT
    'ai_chat_messages',
    COUNT(*)
FROM ai_chat_messages
UNION ALL
SELECT
    'ai_api_costs',
    COUNT(*)
FROM ai_api_costs;
