-- Migration: Add missing sync_orders and sync_customers columns to store_sync_config
-- Description: Fixes the missing columns that are required by woocommerce-scheduled-sync
-- Date: 2025-11-10
-- Related Issue: #66

-- Add sync_orders column if it doesn't exist
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'store_sync_config'
    AND column_name = 'sync_orders'
  ) THEN
    ALTER TABLE store_sync_config
    ADD COLUMN sync_orders BOOLEAN DEFAULT true;

    RAISE NOTICE 'Added sync_orders column to store_sync_config';
  ELSE
    RAISE NOTICE 'sync_orders column already exists';
  END IF;
END $$;

-- Add sync_customers column if it doesn't exist
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM information_schema.columns
    WHERE table_name = 'store_sync_config'
    AND column_name = 'sync_customers'
  ) THEN
    ALTER TABLE store_sync_config
    ADD COLUMN sync_customers BOOLEAN DEFAULT true;

    RAISE NOTICE 'Added sync_customers column to store_sync_config';
  ELSE
    RAISE NOTICE 'sync_customers column already exists';
  END IF;
END $$;

-- Update existing rows to have the default values
UPDATE store_sync_config
SET
  sync_orders = COALESCE(sync_orders, true),
  sync_customers = COALESCE(sync_customers, true)
WHERE sync_orders IS NULL OR sync_customers IS NULL;

-- Log completion
DO $$
BEGIN
  RAISE NOTICE 'Migration completed: sync_orders and sync_customers columns added to store_sync_config';
  RAISE NOTICE 'All existing store configs updated with default values (true)';
END $$;
