-- Analytics Events Table
CREATE TABLE IF NOT EXISTS analytics_events (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  user_id UUID REFERENCES app_users(id),
  event VARCHAR(100) NOT NULL,
  properties JSONB DEFAULT '{}',
  platform VARCHAR(20) DEFAULT 'telegram',
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_analytics_event ON analytics_events(event);
CREATE INDEX idx_analytics_user ON analytics_events(user_id);
CREATE INDEX idx_analytics_created ON analytics_events(created_at);

-- Disputes Table
CREATE TABLE IF NOT EXISTS disputes (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  submission_id UUID REFERENCES submissions(id) NOT NULL,
  raised_by UUID REFERENCES app_users(id) NOT NULL,
  reason TEXT NOT NULL,
  evidence TEXT,
  status VARCHAR(20) DEFAULT 'open',
  resolution TEXT,
  winner VARCHAR(20),
  admin_notes TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  resolved_at TIMESTAMPTZ
);

CREATE INDEX idx_disputes_status ON disputes(status);
CREATE INDEX idx_disputes_submission ON disputes(submission_id);

-- User language preference
ALTER TABLE app_users ADD COLUMN IF NOT EXISTS language VARCHAR(5) DEFAULT 'en';

-- Daily stats cache (for dashboard)
CREATE TABLE IF NOT EXISTS daily_stats (
  date DATE PRIMARY KEY,
  signups INT DEFAULT 0,
  tasks_created INT DEFAULT 0,
  tasks_completed INT DEFAULT 0,
  total_volume BIGINT DEFAULT 0,
  active_users INT DEFAULT 0,
  updated_at TIMESTAMPTZ DEFAULT NOW()
);
