-- Migration: Fix auto_create_store_sync_config Function
-- Description: Remove references to dropped sync_orders and sync_customers columns
-- Date: 2025-10-31
-- Related Issue: #51

-- ============================================================================
-- Fix auto_create_store_sync_config Function
-- ============================================================================

-- This function is automatically triggered when a new store is inserted
-- It was still trying to insert into sync_orders and sync_customers columns
-- which were dropped in migration 20251031_160300_drop_customer_order_cache.sql

CREATE OR REPLACE FUNCTION auto_create_store_sync_config()
RETURNS TRIGGER AS $$
BEGIN
  -- Only create config for platforms that support scheduled sync
  IF NEW.platform_name IN ('woocommerce', 'shoprenter', 'shopify') THEN
    INSERT INTO store_sync_config (
      store_id,
      enabled,
      sync_frequency,
      sync_products
    )
    VALUES (
      NEW.id,
      true,
      'hourly',
      true
    )
    ON CONFLICT (store_id) DO NOTHING;
  END IF;

  RETURN NEW;
END;
$$ LANGUAGE plpgsql SECURITY DEFINER;

-- Log migration completion
DO $$
BEGIN
  RAISE NOTICE 'Fixed auto_create_store_sync_config function - removed sync_orders and sync_customers columns';
END $$;
