-- ============================================================================
-- Fix Character Corruption in ALL Tables (Using HEX encoding)
-- ============================================================================
-- This version uses HEX-encoded patterns to avoid encoding issues in the SQL file itself
--
-- IMPORTANT: Create a backup first!
-- To run: docker exec -i cmc-db mariadb -u root -psecureRootPassword --default-character-set=utf8mb4 cmc < scripts/fix_all_corruption_hex.sql
-- ============================================================================

SET NAMES utf8mb4;

SELECT '============================================================' as '';
SELECT 'COMPREHENSIVE CORRUPTION FIX - ALL TABLES (HEX VERSION)' as '';
SELECT '============================================================' as '';

-- ============================================================================
-- HEX Pattern Reference:
-- E28099 = â€™ → U+2019 = ' (smart apostrophe)
-- E2809C = â€œ → U+201C = " (left double quote)
-- E2809D = â€  → U+201D = " (right double quote)
-- E28093 = â€" → U+2013 = – (en dash)
-- E28094 = â€" → U+2014 = — (em dash)
-- E2809B = â€› (removed - zero-width space)
-- C3A9 = Ã© → U+00E9 = é
-- C3BC = Ã¼ → U+00FC = ü
-- C3B3 = Ã³ → U+00F3 = ó
-- C3AD = Ã­ → U+00ED = í
-- C3A7 = Ã§ → U+00E7 = ç
-- C3B1 = Ã± → U+00F1 = ñ
-- C3B6 = Ã¶ → U+00F6 = ö
-- C39F = ÃŸ → U+00DF = ß
-- C2B0 = Â° → U+00B0 = °
-- C2AE = Â® → U+00AE = ®
-- E284A2 = â„¢ → U+2122 = ™
-- ============================================================================

-- ============================================================================
-- ADDRESSES Table
-- ============================================================================
SELECT 'Fixing addresses table...' as '';

UPDATE addresses SET name = REPLACE(name, UNHEX('E28099'), ''') WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('E2809C'), '"') WHERE name LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('E2809D'), '"') WHERE name LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('C3BC'), 'ü') WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE addresses SET address = REPLACE(address, UNHEX('E28099'), ''') WHERE address LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E2809C'), '"') WHERE address LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E2809D'), '"') WHERE address LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E28093'), '–') WHERE address LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E28094'), '—') WHERE address LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3A9'), 'é') WHERE address LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3BC'), 'ü') WHERE address LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3B3'), 'ó') WHERE address LIKE CONCAT('%', UNHEX('C3B3'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3B1'), 'ñ') WHERE address LIKE CONCAT('%', UNHEX('C3B1'), '%');

UPDATE addresses SET city = REPLACE(city, UNHEX('C3A9'), 'é') WHERE city LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET city = REPLACE(city, UNHEX('C3BC'), 'ü') WHERE city LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE addresses SET city = REPLACE(city, UNHEX('C3B3'), 'ó') WHERE city LIKE CONCAT('%', UNHEX('C3B3'), '%');

-- ============================================================================
-- ATTACHMENTS Table
-- ============================================================================
SELECT 'Fixing attachments table...' as '';

UPDATE attachments SET name = REPLACE(name, UNHEX('E28099'), ''') WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE attachments SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE attachments SET name = REPLACE(name, UNHEX('C3BC'), 'ü') WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE attachments SET description = REPLACE(description, UNHEX('E28099'), ''') WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('E2809C'), '"') WHERE description LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('E2809D'), '"') WHERE description LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('C3A9'), 'é') WHERE description LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('C3BC'), 'ü') WHERE description LIKE CONCAT('%', UNHEX('C3BC'), '%');

-- ============================================================================
-- CONTACTS Table
-- ============================================================================
SELECT 'Fixing contacts table...' as '';

UPDATE contacts SET name = REPLACE(name, UNHEX('E28099'), ''') WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3BC'), 'ü') WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3B3'), 'ó') WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');

UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('E28099'), ''') WHERE first_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('C3A9'), 'é') WHERE first_name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('C3BC'), 'ü') WHERE first_name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('E28099'), ''') WHERE last_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('C3A9'), 'é') WHERE last_name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('C3BC'), 'ü') WHERE last_name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET notes = REPLACE(notes, UNHEX('E28099'), ''') WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('E2809C'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('E2809D'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('C3A9'), 'é') WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('C3BC'), 'ü') WHERE notes LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET job_title = REPLACE(job_title, UNHEX('E28099'), ''') WHERE job_title LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET job_title = REPLACE(job_title, UNHEX('C3A9'), 'é') WHERE job_title LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- CUSTOMERS Table
-- ============================================================================
SELECT 'Fixing customers table...' as '';

UPDATE customers SET name = REPLACE(name, UNHEX('E28099'), ''') WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2809C'), '"') WHERE name LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2809D'), '"') WHERE name LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E28093'), '–') WHERE name LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E28094'), '—') WHERE name LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2808B'), '') WHERE name LIKE CONCAT('%', UNHEX('E2808B'), '%'); -- zero-width space
UPDATE customers SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3BC'), 'ü') WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3B3'), 'ó') WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3A7'), 'ç') WHERE name LIKE CONCAT('%', UNHEX('C3A7'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3B8'), 'ø') WHERE name LIKE CONCAT('%', UNHEX('C3B8'), '%');

UPDATE customers SET trading_name = REPLACE(trading_name, UNHEX('E28099'), ''') WHERE trading_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET trading_name = REPLACE(trading_name, UNHEX('C3A9'), 'é') WHERE trading_name LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE customers SET notes = REPLACE(notes, UNHEX('E28099'), ''') WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('E2809C'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('E2809D'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('C3A9'), 'é') WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- DOCUMENTS Table
-- ============================================================================
SELECT 'Fixing documents table...' as '';

UPDATE documents SET shipping_details = REPLACE(shipping_details, UNHEX('E28099'), ''') WHERE shipping_details LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET shipping_details = REPLACE(shipping_details, UNHEX('C3A9'), 'é') WHERE shipping_details LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET bill_to = REPLACE(bill_to, UNHEX('E28099'), ''') WHERE bill_to LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET bill_to = REPLACE(bill_to, UNHEX('C3A9'), 'é') WHERE bill_to LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET ship_to = REPLACE(ship_to, UNHEX('E28099'), ''') WHERE ship_to LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET ship_to = REPLACE(ship_to, UNHEX('C3A9'), 'é') WHERE ship_to LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET subject = REPLACE(subject, UNHEX('E28099'), ''') WHERE subject LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('E2809C'), '"') WHERE subject LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('E2809D'), '"') WHERE subject LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('C3A9'), 'é') WHERE subject LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- EMAILS Table
-- ============================================================================
SELECT 'Fixing emails table...' as '';

UPDATE emails SET `from` = REPLACE(`from`, UNHEX('E28099'), ''') WHERE `from` LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET `from` = REPLACE(`from`, UNHEX('C3A9'), 'é') WHERE `from` LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE emails SET `to` = REPLACE(`to`, UNHEX('E28099'), ''') WHERE `to` LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET `to` = REPLACE(`to`, UNHEX('C3A9'), 'é') WHERE `to` LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE emails SET subject = REPLACE(subject, UNHEX('E28099'), ''') WHERE subject LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('E2809C'), '"') WHERE subject LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('E2809D'), '"') WHERE subject LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('C3A9'), 'é') WHERE subject LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('C3BC'), 'ü') WHERE subject LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE emails SET body = REPLACE(body, UNHEX('E28099'), ''') WHERE body LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E2809C'), '"') WHERE body LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E2809D'), '"') WHERE body LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E28093'), '–') WHERE body LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E28094'), '—') WHERE body LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C3A9'), 'é') WHERE body LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C3BC'), 'ü') WHERE body LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C2B0'), '°') WHERE body LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C2AE'), '®') WHERE body LIKE CONCAT('%', UNHEX('C2AE'), '%');

-- ============================================================================
-- ENQUIRIES Table
-- ============================================================================
SELECT 'Fixing enquiries table...' as '';

UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('C3BC'), 'ü') WHERE comments LIKE CONCAT('%', UNHEX('C3BC'), '%');

-- ============================================================================
-- INVOICES Table
-- ============================================================================
SELECT 'Fixing invoices table...' as '';

UPDATE invoices SET ship_via = REPLACE(ship_via, UNHEX('E28099'), ''') WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE invoices SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), 'é') WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE invoices SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- JOBS Table
-- ============================================================================
SELECT 'Fixing jobs table...' as '';

UPDATE jobs SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- LINE_ITEMS Table
-- ============================================================================
SELECT 'Fixing line_items table...' as '';

UPDATE line_items SET name = REPLACE(name, UNHEX('E28099'), ''') WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C2B0'), '°') WHERE name LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C2AE'), '®') WHERE name LIKE CONCAT('%', UNHEX('C2AE'), '%');

UPDATE line_items SET description = REPLACE(description, UNHEX('E28099'), ''') WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('E2809C'), '"') WHERE description LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('E2809D'), '"') WHERE description LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C3A9'), 'é') WHERE description LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C2B0'), '°') WHERE description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C2AE'), '®') WHERE description LIKE CONCAT('%', UNHEX('C2AE'), '%');

-- ============================================================================
-- ORDER_ACKNOWLEDGEMENTS Table
-- ============================================================================
SELECT 'Fixing order_acknowledgements table...' as '';

UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- PRINCIPLES Table
-- ============================================================================
SELECT 'Fixing principles table...' as '';

UPDATE principles SET name = REPLACE(name, UNHEX('C3BC'), 'ü') WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET name = REPLACE(name, UNHEX('C3A9'), 'é') WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET name = REPLACE(name, UNHEX('C3B3'), 'ó') WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');

UPDATE principles SET address = REPLACE(address, UNHEX('C39F'), 'ß') WHERE address LIKE CONCAT('%', UNHEX('C39F'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3BC'), 'ü') WHERE address LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3A9'), 'é') WHERE address LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3B1'), 'ñ') WHERE address LIKE CONCAT('%', UNHEX('C3B1'), '%');

UPDATE principles SET city = REPLACE(city, UNHEX('C3BC'), 'ü') WHERE city LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET city = REPLACE(city, UNHEX('C3A9'), 'é') WHERE city LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET city = REPLACE(city, UNHEX('C3B3'), 'ó') WHERE city LIKE CONCAT('%', UNHEX('C3B3'), '%');

-- ============================================================================
-- PRODUCTS Table
-- ============================================================================
SELECT 'Fixing products table...' as '';

-- Mojibake in products
UPDATE products SET title = REPLACE(title, UNHEX('C2B0'), '°') WHERE title LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('C2AE'), '®') WHERE title LIKE CONCAT('%', UNHEX('C2AE'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E284A2'), '™') WHERE title LIKE CONCAT('%', UNHEX('E284A2'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E28099'), ''') WHERE title LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E2809C'), '"') WHERE title LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E2809D'), '"') WHERE title LIKE CONCAT('%', UNHEX('E2809D'), '%');

UPDATE products SET description = REPLACE(description, UNHEX('C2B0'), '°') WHERE description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('C2AE'), '®') WHERE description LIKE CONCAT('%', UNHEX('C2AE'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('E284A2'), '™') WHERE description LIKE CONCAT('%', UNHEX('E284A2'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('E28099'), ''') WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');

UPDATE products SET item_description = REPLACE(item_description, UNHEX('C2B0'), '°') WHERE item_description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET item_description = REPLACE(item_description, UNHEX('C2AE'), '®') WHERE item_description LIKE CONCAT('%', UNHEX('C2AE'), '%');

-- HTML entities in products
UPDATE products SET title = REPLACE(title, '&deg;', '°') WHERE title LIKE '%&deg;%';
UPDATE products SET title = REPLACE(title, '&nbsp;', ' ') WHERE title LIKE '%&nbsp;%';
UPDATE products SET title = REPLACE(title, '&amp;', '&') WHERE title LIKE '%&amp;%';
UPDATE products SET title = REPLACE(title, '&ndash;', '–') WHERE title LIKE '%&ndash;%';
UPDATE products SET title = REPLACE(title, '&rdquo;', '"') WHERE title LIKE '%&rdquo;%';
UPDATE products SET title = REPLACE(title, '&ldquo;', '"') WHERE title LIKE '%&ldquo;%';

UPDATE products SET description = REPLACE(description, '&deg;', '°') WHERE description LIKE '%&deg;%';
UPDATE products SET description = REPLACE(description, '&nbsp;', ' ') WHERE description LIKE '%&nbsp;%';
UPDATE products SET description = REPLACE(description, '&amp;', '&') WHERE description LIKE '%&amp;%';
UPDATE products SET description = REPLACE(description, '&frac12;', '½') WHERE description LIKE '%&frac12;%';

-- ============================================================================
-- PURCHASE_ORDERS Table
-- ============================================================================
SELECT 'Fixing purchase_orders table...' as '';

UPDATE purchase_orders SET ship_via = REPLACE(ship_via, UNHEX('E28099'), ''') WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE purchase_orders SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), 'é') WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- QUOTES Table
-- ============================================================================
SELECT 'Fixing quotes table...' as '';

UPDATE quotes SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE quotes SET notes = REPLACE(notes, UNHEX('E28099'), ''') WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('E2809C'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('E2809D'), '"') WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('C3A9'), 'é') WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- SHIPMENTS Table
-- ============================================================================
SELECT 'Fixing shipments table...' as '';

UPDATE shipments SET ship_via = REPLACE(ship_via, UNHEX('E28099'), ''') WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE shipments SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), 'é') WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE shipments SET comments = REPLACE(comments, UNHEX('E28099'), ''') WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('E2809C'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('E2809D'), '"') WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('C3A9'), 'é') WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

-- ============================================================================
-- Verification
-- ============================================================================
SELECT '' as '';
SELECT '============================================================' as '';
SELECT 'FIX COMPLETE' as '';
SELECT '============================================================' as '';
SELECT 'Run scan_all_tables_for_corruption.sql to verify' as '';
