-- SQL Script to manually fix existing paid consulting orders
-- This updates consulting_slots status and can be run in Supabase SQL Editor

-- Step 1: Check how many consulting orders are affected
SELECT
  o.id as order_id,
  o.payment_status,
  COUNT(cs.id) as slot_count,
  SUM(CASE WHEN cs.status = 'pending_payment' THEN 1 ELSE 0 END) as pending_slots
FROM orders o
INNER JOIN consulting_slots cs ON cs.order_id = o.id
WHERE o.payment_status = 'paid'
GROUP BY o.id, o.payment_status
HAVING SUM(CASE WHEN cs.status = 'pending_payment' THEN 1 ELSE 0 END) > 0;

-- Step 2: Update all pending_payment slots for paid orders to 'confirmed'
UPDATE consulting_slots
SET status = 'confirmed'
WHERE order_id IN (
  SELECT o.id
  FROM orders o
  WHERE o.payment_status = 'paid'
)
AND status = 'pending_payment';

-- Step 3: Verify the update
SELECT
  o.id as order_id,
  o.payment_status,
  cs.status as slot_status,
  cs.date,
  cs.start_time,
  cs.end_time,
  cs.meet_link
FROM orders o
INNER JOIN consulting_slots cs ON cs.order_id = o.id
WHERE o.payment_status = 'paid'
ORDER BY o.created_at DESC;
