-- Supabase Database Setup for SiteMente Mission Control
-- Run this in Supabase SQL Editor

-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

-- Trading Journal Table
CREATE TABLE IF NOT EXISTS trades (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id TEXT DEFAULT 'default',
  pair TEXT NOT NULL,
  direction TEXT NOT NULL CHECK (direction IN ('long', 'short')),
  entry_price NUMERIC,
  exit_price NUMERIC,
  status TEXT NOT NULL DEFAULT 'open' CHECK (status IN ('open', 'closed')),
  is_demo BOOLEAN DEFAULT true,
  trader_style TEXT,
  setup TEXT,
  timeframe TEXT,
  pnl NUMERIC,
  pnl_percent NUMERIC,
  opened_at TIMESTAMPTZ DEFAULT NOW(),
  closed_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Tasks Table
CREATE TABLE IF NOT EXISTS tasks (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id TEXT DEFAULT 'default',
  title TEXT NOT NULL,
  description TEXT,
  status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'in_progress', 'completed')),
  priority TEXT DEFAULT 'medium' CHECK (priority IN ('low', 'medium', 'high')),
  due_date TIMESTAMPTZ,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Leads/CRM Table
CREATE TABLE IF NOT EXISTS leads (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id TEXT DEFAULT 'default',
  name TEXT NOT NULL,
  business_name TEXT,
  phone TEXT,
  email TEXT,
  source TEXT,
  status TEXT NOT NULL DEFAULT 'new' CHECK (status IN ('new', 'contacted', 'qualified', 'won', 'lost')),
  notes TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- Enable RLS (Row Level Security) - optional for now
-- ALTER TABLE trades ENABLE ROW LEVEL SECURITY;
-- ALTER TABLE tasks ENABLE ROW LEVEL SECURITY;
-- ALTER TABLE leads ENABLE ROW LEVEL SECURITY;

-- Create indexes for better performance
CREATE INDEX IF NOT EXISTS idx_trades_user ON trades(user_id);
CREATE INDEX IF NOT EXISTS idx_trades_status ON trades(status);
CREATE INDEX IF NOT EXISTS idx_tasks_user ON tasks(user_id);
CREATE INDEX IF NOT EXISTS idx_tasks_status ON tasks(status);
CREATE INDEX IF NOT EXISTS idx_leads_user ON leads(user_id);
CREATE INDEX IF NOT EXISTS idx_leads_status ON leads(status);

-- Insert some sample data
INSERT INTO trades (pair, direction, entry_price, status, is_demo, trader_style, setup, timeframe) VALUES
('BTC/USD', 'long', 67500, 'open', true, 'thoth', 'Weekly structure break', '4H'),
('ETH/USD', 'long', 3200, 'open', true, 'dopetrades', 'Double bottom', '1H');

INSERT INTO tasks (title, description, status, priority) VALUES
('Fix Vapi integration', 'Get voice working on SiteMente', 'in_progress', 'high'),
('Contact local businesses', 'Reach out to leads in Benalmádena', 'pending', 'high'),
('Set up Supabase', 'Migrate from JSON to Supabase DB', 'in_progress', 'medium');

INSERT INTO leads (name, business_name, phone, status, source) VALUES
('Juan', 'Restaurante La Nina', '+34 952 449 193', 'new', 'cold_call'),
('Maria', 'Clínica Dental Málaga', '+34 951 123 456', 'contacted', 'website');
