-- Migration 011: Missing indexes, last_exchange_at column, residence types
-- Date: 2026-04-10
-- Purpose: Performance optimization + exchange cooldown persistence + schema coherence

-- ─── Missing indexes on FK columns ───────────────────────────
CREATE INDEX IF NOT EXISTS idx_player_inventory_player_id ON player_inventory(player_id);
CREATE INDEX IF NOT EXISTS idx_vehicles_player_id ON vehicles(player_id);
CREATE INDEX IF NOT EXISTS idx_businesses_player_id ON businesses(player_id);
CREATE INDEX IF NOT EXISTS idx_residences_player_id ON residences(player_id);
CREATE INDEX IF NOT EXISTS idx_warehouses_player_id ON warehouses(player_id);
CREATE INDEX IF NOT EXISTS idx_marketplace_listings_seller_id ON marketplace_listings(seller_id);
CREATE INDEX IF NOT EXISTS idx_deals_initiator_id ON deals(initiator_id);
CREATE INDEX IF NOT EXISTS idx_deals_owner_id ON deals(owner_id);
CREATE INDEX IF NOT EXISTS idx_notifications_player_id ON notifications(player_id);
CREATE INDEX IF NOT EXISTS idx_discussions_category_id ON discussions(category_id);
CREATE INDEX IF NOT EXISTS idx_mission_progress_player_id ON mission_progress(player_id);
CREATE INDEX IF NOT EXISTS idx_gang_stock_gang_id ON gang_stock(gang_id);
CREATE INDEX IF NOT EXISTS idx_gang_assets_gang_id ON gang_assets(gang_id);
CREATE INDEX IF NOT EXISTS idx_employments_player_id ON employments(player_id);
CREATE INDEX IF NOT EXISTS idx_replies_discussion_id ON replies(discussion_id);

-- ─── Exchange cooldown column ────────────────────────────────
ALTER TABLE player_balances ADD COLUMN IF NOT EXISTS last_exchange_at TIMESTAMP DEFAULT NULL;

-- ─── Mission axes_impact column ──────────────────────────────
ALTER TABLE missions ADD COLUMN IF NOT EXISTS axes_impact JSONB DEFAULT NULL;

-- ─── Bourse history table ────────────────────────────────────
CREATE TABLE IF NOT EXISTS bourse_history (
  id SERIAL PRIMARY KEY,
  timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  cours DECIMAL(10,2) NOT NULL,
  volume_buy DECIMAL(18,2) DEFAULT 0,
  volume_sell DECIMAL(18,2) DEFAULT 0,
  joueurs_actifs INTEGER DEFAULT 0
);

-- ─── Extend residence type enum ──────────────────────────────
-- PostgreSQL requires ALTER TYPE to add new enum values
DO $$
BEGIN
  -- Add 'room' if not exists
  IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'room' AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'residences_type_enum')) THEN
    ALTER TYPE residences_type_enum ADD VALUE 'room';
  END IF;
  -- Add 'studio' if not exists
  IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'studio' AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'residences_type_enum')) THEN
    ALTER TYPE residences_type_enum ADD VALUE 'studio';
  END IF;
  -- Add 'manor' if not exists
  IF NOT EXISTS (SELECT 1 FROM pg_enum WHERE enumlabel = 'manor' AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'residences_type_enum')) THEN
    ALTER TYPE residences_type_enum ADD VALUE 'manor';
  END IF;
EXCEPTION WHEN duplicate_object THEN
  NULL; -- ignore if already exists
END
$$;

-- ─── Spatial index helpers (for VisibilityService bounding box) ──
CREATE INDEX IF NOT EXISTS idx_players_current_lat ON players(current_lat) WHERE current_lat IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_players_current_lon ON players(current_lon) WHERE current_lon IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_businesses_location_lat ON businesses(location_lat) WHERE location_lat IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_businesses_location_lon ON businesses(location_lon) WHERE location_lon IS NOT NULL;
