-- ============================================================================
-- Setup Test Hotels for STAAH & Google Integration
-- ============================================================================
-- Purpose: Limit STAAH and Google integration to specific test hotels on staging
-- Date: 2025-11-24
--
-- IMPORTANT: This should ONLY be run on STAGING environment, NEVER on production!
--
-- Test Hotels:
-- - Sun Inns (all Sun Inns hotels)
-- - Orange Hotel Kota Kemuning @ Shah Alam
--
-- What this does:
-- 1. Disables STAAH and Google integrations for all hotels
-- 2. Re-enables integrations only for the test hotels listed above
--    (only if they have the corresponding property/hotel IDs configured)
-- ============================================================================

-- Step 1: Create a backup table (recommended before making changes)
CREATE TABLE hotels_integration_backup_staging AS
SELECT
    id,
    name,
    staah_property_id,
    google_hotel_id,
    staah_integration_enabled,
    google_integration_enabled,
    NOW() as backed_up_at
FROM hotels;

-- Step 2: Disable integrations for ALL hotels
UPDATE hotels
SET
    staah_integration_enabled = 0,
    google_integration_enabled = 0,
    updated_at = NOW();

-- Step 3: Enable integrations ONLY for test hotels (where they have the IDs configured)
-- Enable STAAH integration for test hotels that have STAAH property ID
UPDATE hotels
SET
    staah_integration_enabled = 1,
    updated_at = NOW()
WHERE (name LIKE '%Sun Inns%' OR name LIKE '%Orange Hotel Kota Kemuning @ Shah Alam%')
  AND staah_property_id IS NOT NULL;

-- Enable Google integration for test hotels that have Google hotel ID
UPDATE hotels
SET
    google_integration_enabled = 1,
    updated_at = NOW()
WHERE (name LIKE '%Sun Inns%' OR name LIKE '%Orange Hotel Kota Kemuning @ Shah Alam%')
  AND google_hotel_id IS NOT NULL;

-- ============================================================================
-- Verification Queries
-- ============================================================================

-- Check which hotels are now enabled
SELECT
    id,
    name,
    staah_property_id,
    google_hotel_id,
    staah_integration_enabled,
    google_integration_enabled
FROM hotels
WHERE staah_integration_enabled = 1
   OR google_integration_enabled = 1
ORDER BY name;

-- Count summary
SELECT
    'Total Hotels' as metric,
    COUNT(*) as count
FROM hotels
UNION ALL
SELECT
    'Sun Inns Hotels (by name)',
    COUNT(*)
FROM hotels
WHERE name LIKE '%Sun Inns%'
UNION ALL
SELECT
    'Orange Hotel Kota Kemuning',
    COUNT(*)
FROM hotels
WHERE name LIKE '%Orange Hotel Kota Kemuning @ Shah Alam%'
UNION ALL
SELECT
    'STAAH Enabled',
    COUNT(*)
FROM hotels
WHERE staah_integration_enabled = 1
UNION ALL
SELECT
    'Google Enabled',
    COUNT(*)
FROM hotels
WHERE google_integration_enabled = 1;

-- ============================================================================
-- Rollback Query (if needed)
-- ============================================================================
-- To restore all hotels to their previous state, run:
--
-- UPDATE hotels h
-- JOIN hotels_integration_backup_staging b ON h.id = b.id
-- SET
--     h.staah_integration_enabled = b.staah_integration_enabled,
--     h.google_integration_enabled = b.google_integration_enabled,
--     h.updated_at = NOW();
--
-- DROP TABLE IF EXISTS hotels_integration_backup_staging;
-- ============================================================================
