-- Fixes allBETs' dim_web_sites row, which was incorrectly pointing at the
-- "LarCare" business (businesses.id=2) instead of "AB2 Global" (businesses.id=5)
-- -- both under the same tenant (tenant_id=2). Confirmed live via a read-only
-- diagnostic: dim_web_sites rows for both 'larcareservices.com' and
-- 'allbetsgo.com' had business_id=2, i.e. allBETs was sharing LarCare's business
-- assignment. LarCare's own row is already correct (business_id=2 IS the
-- "LarCare" business) and is untouched here.
--
-- Resolves 'AB2 Global' by name (not a hardcoded id) so this stays correct even
-- if ids differ across environments. Idempotent — only updates if the site's
-- business_id doesn't already match, and no-ops if either row can't be resolved.
-- Deploy: mysql --default-character-set=utf8mb4 -u root -p db_abos_v0.1 < db/fix_allbets_business_assignment.sql

USE db_abos_v0.1;

SET @allbets_site_id = (SELECT id FROM dim_web_sites WHERE primary_domain = 'allbetsgo.com' LIMIT 1);
SET @ab2_global_business_id = (SELECT id FROM businesses WHERE name = 'AB2 Global' LIMIT 1);

UPDATE dim_web_sites
SET business_id = @ab2_global_business_id
WHERE id = @allbets_site_id
  AND @allbets_site_id IS NOT NULL
  AND @ab2_global_business_id IS NOT NULL
  AND (business_id IS NULL OR business_id <> @ab2_global_business_id);

-- Every existing dim_web_products / dim_web_product_variants / dim_warehouses row for
-- allBETs also carries this same wrong business_id (inherited from
-- allbets_site_and_catalog_seed.sql's original @business_id := 4, which at the time
-- that ran did not point at the actual "AB2 Global" business row it intended — see
-- that file's own comment claiming tenant_id=1/id=4, which is actually "LarCare
-- Services", not "AB2 Global"). Fix those too, scoped strictly to this site so
-- nothing outside allBETs is touched.
UPDATE dim_web_products
SET business_id = @ab2_global_business_id
WHERE site_id = @allbets_site_id
  AND @ab2_global_business_id IS NOT NULL
  AND (business_id IS NULL OR business_id <> @ab2_global_business_id);

UPDATE dim_web_product_variants
SET business_id = @ab2_global_business_id
WHERE site_id = @allbets_site_id
  AND @ab2_global_business_id IS NOT NULL
  AND (business_id IS NULL OR business_id <> @ab2_global_business_id);

UPDATE dim_warehouses
SET business_id = @ab2_global_business_id
WHERE site_id = @allbets_site_id
  AND @ab2_global_business_id IS NOT NULL
  AND (business_id IS NULL OR business_id <> @ab2_global_business_id);
