-- Manually activates the Pro plan for user id 2, matching exactly what the
-- real PayPal capture flow writes (see api.albaratai.com.mx/src/services/subscriptions.js).

SET @seller_id  = 2;
SET @plan_name  = 'Pro'; -- must match plans.name exactly, e.g. 'Gratis', 'Básico', 'Pro'

-- 1. Cancel any currently active subscription for this seller
UPDATE seller_subscriptions
SET status = 'cancelled'
WHERE seller_id = @seller_id
  AND status = 'active';

-- 2. Activate the chosen plan
INSERT INTO seller_subscriptions (seller_id, plan_id, starts_at, expires_at, status)
SELECT
  @seller_id,
  p.id,
  NOW(),
  DATE_ADD(NOW(), INTERVAL IF(p.duration_days = 0, 3650, p.duration_days) DAY),
  'active'
FROM plans p
WHERE p.name = @plan_name;

-- 3. (Matches what activate() also does) reflect featured_profile on the seller's profile
UPDATE seller_profiles sp
JOIN plans p ON p.name = @plan_name
SET sp.is_featured = p.featured_profile
WHERE sp.user_id = @seller_id;
