-- Initialize PostgreSQL databases for Svrnty AI Agent system
-- This script runs automatically when the PostgreSQL container starts for the first time

-- Create databases
CREATE DATABASE svrnty;
CREATE DATABASE langfuse;

-- Connect to svrnty database
\c svrnty;

-- Create schema for agent data
CREATE SCHEMA IF NOT EXISTS agent;

-- Conversations table for AI agent conversation history
CREATE TABLE IF NOT EXISTS agent.conversations (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    messages JSONB NOT NULL DEFAULT '[]'::jsonb,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_conversations_created ON agent.conversations(created_at DESC);
CREATE INDEX idx_conversations_updated ON agent.conversations(updated_at DESC);

-- Revenue table for business data queries
CREATE TABLE IF NOT EXISTS agent.revenue (
    id SERIAL PRIMARY KEY,
    month VARCHAR(50) NOT NULL,
    amount DECIMAL(18, 2) NOT NULL,
    year INTEGER NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE UNIQUE INDEX idx_revenue_month ON agent.revenue(month, year);

-- Customers table for business data queries
CREATE TABLE IF NOT EXISTS agent.customers (
    id SERIAL PRIMARY KEY,
    name VARCHAR(200) NOT NULL,
    email VARCHAR(200),
    state VARCHAR(100),
    tier VARCHAR(50),
    created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE INDEX idx_customers_state ON agent.customers(state);
CREATE INDEX idx_customers_tier ON agent.customers(tier);
CREATE INDEX idx_customers_state_tier ON agent.customers(state, tier);

-- Seed revenue data (2024-2025)
INSERT INTO agent.revenue (month, amount, year) VALUES
    ('January', 125000.00, 2024),
    ('February', 135000.00, 2024),
    ('March', 148000.00, 2024),
    ('April', 142000.00, 2024),
    ('May', 155000.00, 2024),
    ('June', 168000.00, 2024),
    ('July', 172000.00, 2024),
    ('August', 165000.00, 2024),
    ('September', 178000.00, 2024),
    ('October', 185000.00, 2024),
    ('November', 192000.00, 2024),
    ('December', 210000.00, 2024),
    ('January', 215000.00, 2025),
    ('February', 225000.00, 2025),
    ('March', 235000.00, 2025),
    ('April', 242000.00, 2025),
    ('May', 255000.00, 2025)
ON CONFLICT (month, year) DO NOTHING;

-- Seed customer data
INSERT INTO agent.customers (name, email, state, tier) VALUES
    ('Acme Corporation', 'contact@acme.com', 'California', 'Enterprise'),
    ('TechStart Inc', 'hello@techstart.io', 'New York', 'Professional'),
    ('Global Solutions LLC', 'info@globalsol.com', 'Texas', 'Enterprise'),
    ('Innovation Labs', 'team@innovlabs.com', 'California', 'Professional'),
    ('Digital Dynamics', 'sales@digitaldyn.com', 'Washington', 'Starter'),
    ('CloudFirst Co', 'contact@cloudfirst.io', 'New York', 'Enterprise'),
    ('Data Insights Group', 'info@datainsights.com', 'Texas', 'Professional'),
    ('AI Ventures', 'hello@aiventures.ai', 'California', 'Enterprise'),
    ('Smart Systems Inc', 'contact@smartsys.com', 'Florida', 'Starter'),
    ('Future Tech Partners', 'team@futuretech.com', 'Massachusetts', 'Professional'),
    ('Quantum Analytics', 'info@quantumdata.io', 'New York', 'Enterprise'),
    ('Rapid Scale Solutions', 'sales@rapidscale.com', 'California', 'Professional'),
    ('Enterprise Connect', 'hello@entconnect.com', 'Texas', 'Enterprise'),
    ('Startup Accelerator', 'team@startacc.io', 'Washington', 'Starter'),
    ('Cloud Native Labs', 'contact@cloudnative.dev', 'Oregon', 'Professional')
ON CONFLICT DO NOTHING;

-- Create updated_at trigger function
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ language 'plpgsql';

-- Add trigger to conversations table
CREATE TRIGGER update_conversations_updated_at
    BEFORE UPDATE ON agent.conversations
    FOR EACH ROW
    EXECUTE FUNCTION update_updated_at_column();

-- Grant permissions (for application user)
GRANT USAGE ON SCHEMA agent TO postgres;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA agent TO postgres;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA agent TO postgres;

-- Summary
DO $$
BEGIN
    RAISE NOTICE 'Database initialization complete!';
    RAISE NOTICE '- Created svrnty database with agent schema';
    RAISE NOTICE '- Created conversations table for AI agent history';
    RAISE NOTICE '- Created revenue table with % rows', (SELECT COUNT(*) FROM agent.revenue);
    RAISE NOTICE '- Created customers table with % rows', (SELECT COUNT(*) FROM agent.customers);
    RAISE NOTICE '- Created langfuse database (will be initialized by Langfuse container)';
END $$;
