-- ================================================
-- Add onboarding tracking fields to organizations table
-- ================================================

-- Add onboarding completion tracking to organizations
ALTER TABLE organizations 
  ADD COLUMN onboarding_completed BOOLEAN DEFAULT FALSE AFTER status,
  ADD COLUMN onboarding_completed_at TIMESTAMP NULL AFTER onboarding_completed,
  ADD INDEX idx_org_onboarding (onboarding_completed);

-- Update existing organizations to have completed onboarding if they were created before SaaS structure
-- (This handles migrated data from the single-tenant system)
UPDATE organizations 
SET onboarding_completed = TRUE, 
    onboarding_completed_at = created_at 
WHERE created_at < (SELECT MIN(created_at) FROM subscription_plans);