-- ============================================================================
-- Fix Character Corruption in ALL Tables (Safe Version with CHAR())
-- ============================================================================
-- This version uses CHAR() with Unicode code points to avoid any encoding issues
--
-- 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_safe.sql
-- ============================================================================

SET NAMES utf8mb4;

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

-- ============================================================================
-- Pattern Reference:
-- UNHEX('E28099') = â€™ → CHAR(8217 USING utf8mb4) = '
-- UNHEX('E2809C') = â€œ → CHAR(8220 USING utf8mb4) = "
-- UNHEX('E2809D') = â€  → CHAR(8221 USING utf8mb4) = "
-- UNHEX('E28093') = â€" → CHAR(8211 USING utf8mb4) = –
-- UNHEX('E28094') = â€" → CHAR(8212 USING utf8mb4) = —
-- UNHEX('E2808B') = zero-width space → (remove)
-- UNHEX('C3A9') = Ã© → CHAR(0x00E9 USING utf8mb4) = é
-- UNHEX('C3BC') = Ã¼ → CHAR(0x00FC USING utf8mb4) = ü
-- UNHEX('C3B3') = Ã³ → CHAR(0x00F3 USING utf8mb4) = ó
-- UNHEX('C3B1') = Ã± → CHAR(0x00F1 USING utf8mb4) = ñ
-- UNHEX('C3A7') = Ã§ → CHAR(0x00E7 USING utf8mb4) = ç
-- UNHEX('C3B6') = Ã¶ → CHAR(0x00F6 USING utf8mb4) = ö
-- UNHEX('C3B8') = Ã¸ → CHAR(0x00F8 USING utf8mb4) = ø
-- UNHEX('C39F') = ÃŸ → CHAR(0x00DF USING utf8mb4) = ß
-- UNHEX('C2B0') = Â° → CHAR(0x00B0 USING utf8mb4) = °
-- UNHEX('C2AE') = Â® → CHAR(0x00AE USING utf8mb4) = ®
-- UNHEX('E284A2') = â„¢ → CHAR(8482 USING utf8mb4) = ™
-- ============================================================================

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

UPDATE addresses SET name = REPLACE(name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET name = REPLACE(name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE addresses SET address = REPLACE(address, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E28093'), CHAR(8211 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('E28094'), CHAR(8212 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3B3'), '%');
UPDATE addresses SET address = REPLACE(address, UNHEX('C3B1'), CHAR(0x00F1 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3B1'), '%');

UPDATE addresses SET city = REPLACE(city, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE addresses SET city = REPLACE(city, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE addresses SET city = REPLACE(city, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3B3'), '%');

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

UPDATE attachments SET name = REPLACE(name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE attachments SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE attachments SET name = REPLACE(name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE attachments SET description = REPLACE(description, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE attachments SET description = REPLACE(description, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C3BC'), '%');

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

UPDATE contacts SET name = REPLACE(name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE contacts SET name = REPLACE(name, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');

UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE first_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE first_name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET first_name = REPLACE(first_name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE first_name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE last_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE last_name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET last_name = REPLACE(last_name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE last_name LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET notes = REPLACE(notes, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE contacts SET notes = REPLACE(notes, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE contacts SET job_title = REPLACE(job_title, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE job_title LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE contacts SET job_title = REPLACE(job_title, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE job_title LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE customers SET name = REPLACE(name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E28093'), CHAR(8211 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E28094'), CHAR(8212 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('E2808B'), '') WHERE name LIKE CONCAT('%', UNHEX('E2808B'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3A7'), CHAR(0x00E7 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A7'), '%');
UPDATE customers SET name = REPLACE(name, UNHEX('C3B8'), CHAR(0x00F8 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3B8'), '%');

UPDATE customers SET trading_name = REPLACE(trading_name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE trading_name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET trading_name = REPLACE(trading_name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE trading_name LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE customers SET notes = REPLACE(notes, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE customers SET notes = REPLACE(notes, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE documents SET shipping_details = REPLACE(shipping_details, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE shipping_details LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET shipping_details = REPLACE(shipping_details, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE shipping_details LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET bill_to = REPLACE(bill_to, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE bill_to LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET bill_to = REPLACE(bill_to, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE bill_to LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET ship_to = REPLACE(ship_to, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE ship_to LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET ship_to = REPLACE(ship_to, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE ship_to LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE documents SET subject = REPLACE(subject, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE documents SET subject = REPLACE(subject, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE emails SET `from` = REPLACE(`from`, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE `from` LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET `from` = REPLACE(`from`, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE `from` LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE emails SET `to` = REPLACE(`to`, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE `to` LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET `to` = REPLACE(`to`, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE `to` LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE emails SET subject = REPLACE(subject, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE emails SET subject = REPLACE(subject, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE subject LIKE CONCAT('%', UNHEX('C3BC'), '%');

UPDATE emails SET body = REPLACE(body, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E28093'), CHAR(8211 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('E28093'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('E28094'), CHAR(8212 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('E28094'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE emails SET body = REPLACE(body, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE body LIKE CONCAT('%', UNHEX('C2AE'), '%');

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

UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE enquiries SET comments = REPLACE(comments, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3BC'), '%');

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

UPDATE invoices SET ship_via = REPLACE(ship_via, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE invoices SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE invoices SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE invoices SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE jobs SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE jobs SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE line_items SET name = REPLACE(name, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE line_items SET name = REPLACE(name, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C2AE'), '%');

UPDATE line_items SET description = REPLACE(description, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE line_items SET description = REPLACE(description, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C2AE'), '%');

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

UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE order_acknowledgements SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE principles SET name = REPLACE(name, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET name = REPLACE(name, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET name = REPLACE(name, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE name LIKE CONCAT('%', UNHEX('C3B3'), '%');

UPDATE principles SET address = REPLACE(address, UNHEX('C39F'), CHAR(0x00DF USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C39F'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET address = REPLACE(address, UNHEX('C3B1'), CHAR(0x00F1 USING utf8mb4)) WHERE address LIKE CONCAT('%', UNHEX('C3B1'), '%');

UPDATE principles SET city = REPLACE(city, UNHEX('C3BC'), CHAR(0x00FC USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3BC'), '%');
UPDATE principles SET city = REPLACE(city, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3A9'), '%');
UPDATE principles SET city = REPLACE(city, UNHEX('C3B3'), CHAR(0x00F3 USING utf8mb4)) WHERE city LIKE CONCAT('%', UNHEX('C3B3'), '%');

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

-- Mojibake in products
UPDATE products SET title = REPLACE(title, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('C2AE'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E284A2'), CHAR(8482 USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('E284A2'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE products SET title = REPLACE(title, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE title LIKE CONCAT('%', UNHEX('E2809D'), '%');

UPDATE products SET description = REPLACE(description, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('C2AE'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('E284A2'), CHAR(8482 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E284A2'), '%');
UPDATE products SET description = REPLACE(description, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE description LIKE CONCAT('%', UNHEX('E28099'), '%');

UPDATE products SET item_description = REPLACE(item_description, UNHEX('C2B0'), CHAR(0x00B0 USING utf8mb4)) WHERE item_description LIKE CONCAT('%', UNHEX('C2B0'), '%');
UPDATE products SET item_description = REPLACE(item_description, UNHEX('C2AE'), CHAR(0x00AE USING utf8mb4)) WHERE item_description LIKE CONCAT('%', UNHEX('C2AE'), '%');

-- HTML entities in products (these are plain ASCII so no encoding issue)
UPDATE products SET title = REPLACE(title, '&deg;', CHAR(0x00B0 USING utf8mb4)) 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;', CHAR(8211 USING utf8mb4)) WHERE title LIKE '%&ndash;%';
UPDATE products SET title = REPLACE(title, '&rdquo;', CHAR(8221 USING utf8mb4)) WHERE title LIKE '%&rdquo;%';
UPDATE products SET title = REPLACE(title, '&ldquo;', CHAR(8220 USING utf8mb4)) WHERE title LIKE '%&ldquo;%';

UPDATE products SET description = REPLACE(description, '&deg;', CHAR(0x00B0 USING utf8mb4)) 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;', CHAR(0x00BD USING utf8mb4)) WHERE description LIKE '%&frac12;%';

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

UPDATE purchase_orders SET ship_via = REPLACE(ship_via, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE purchase_orders SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE purchase_orders SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE quotes SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE quotes SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE quotes SET notes = REPLACE(notes, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE quotes SET notes = REPLACE(notes, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE notes LIKE CONCAT('%', UNHEX('C3A9'), '%');

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

UPDATE shipments SET ship_via = REPLACE(ship_via, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE shipments SET ship_via = REPLACE(ship_via, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) WHERE ship_via LIKE CONCAT('%', UNHEX('C3A9'), '%');

UPDATE shipments SET comments = REPLACE(comments, UNHEX('E28099'), CHAR(8217 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E28099'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('E2809C'), CHAR(8220 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809C'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('E2809D'), CHAR(8221 USING utf8mb4)) WHERE comments LIKE CONCAT('%', UNHEX('E2809D'), '%');
UPDATE shipments SET comments = REPLACE(comments, UNHEX('C3A9'), CHAR(0x00E9 USING utf8mb4)) 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 '';
