-- ============================================================================
-- Fix Character Corruption in ALL Tables
-- ============================================================================
-- This script fixes mojibake across the entire database
-- Run this AFTER running scan_all_tables_for_corruption.sql to see what's affected
--
-- IMPORTANT: Create a backup first!
-- docker exec cmc-db mariadb-dump -u root -psecureRootPassword --default-character-set=utf8mb4 cmc | gzip > backup_before_complete_fix_$(date +%Y%m%d).sql.gz
--
-- To run: docker exec -i cmc-db mariadb -u root -psecureRootPassword --default-character-set=utf8mb4 cmc < scripts/fix_all_corruption.sql
-- ============================================================================

SET NAMES utf8mb4;

SELECT '============================================================' as '';
SELECT 'COMPREHENSIVE CORRUPTION FIX - ALL TABLES' as '';
SELECT '============================================================' as '';

-- ============================================================================
-- Define reusable corruption patterns
-- These are applied to every text/varchar column
-- ============================================================================

-- Smart quotes and punctuation
-- â€™ → ' (smart apostrophe)
-- â€œ → " (left double quote)
-- â€ → " (right double quote)
-- â€" → – (en dash)
-- â€" → — (em dash)
-- â€‹ → (zero-width space, remove)
-- â€ƒ → (space)
-- â€¦ → …

-- Accented characters
-- Ã© → é
-- Ã‰ → É
-- Ã³ → ó
-- Ã → í
-- Ã§ → ç
-- Ã¼ → ü
-- Ã¡ → á
-- Ã± → ñ
-- Ã¶ → ö
-- Ã´ → ô
-- ÃŸ → ß
-- Ã¤ → ä

-- Symbols
-- Â° → °
-- Â® → ®
-- â„¢ → ™

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

UPDATE addresses SET name = REPLACE(name, 'â€™', ''') WHERE name LIKE '%â€™%';
UPDATE addresses SET name = REPLACE(name, 'â€œ', '"') WHERE name LIKE '%â€œ%';
UPDATE addresses SET name = REPLACE(name, 'â€', '"') WHERE name LIKE '%â€%';
UPDATE addresses SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE addresses SET name = REPLACE(name, 'Ã¼', 'ü') WHERE name LIKE '%Ã¼%';

UPDATE addresses SET address = REPLACE(address, 'â€™', ''') WHERE address LIKE '%â€™%';
UPDATE addresses SET address = REPLACE(address, 'â€œ', '"') WHERE address LIKE '%â€œ%';
UPDATE addresses SET address = REPLACE(address, 'â€', '"') WHERE address LIKE '%â€%';
UPDATE addresses SET address = REPLACE(address, 'â€"', '–') WHERE address LIKE '%â€"%';
UPDATE addresses SET address = REPLACE(address, 'â€"', '—') WHERE address LIKE '%â€"%';
UPDATE addresses SET address = REPLACE(address, 'Ã©', 'é') WHERE address LIKE '%Ã©%';
UPDATE addresses SET address = REPLACE(address, 'Ã¼', 'ü') WHERE address LIKE '%Ã¼%';
UPDATE addresses SET address = REPLACE(address, 'Ã³', 'ó') WHERE address LIKE '%Ã³%';
UPDATE addresses SET address = REPLACE(address, 'Ã±', 'ñ') WHERE address LIKE '%Ã±%';

UPDATE addresses SET city = REPLACE(city, 'Ã©', 'é') WHERE city LIKE '%Ã©%';
UPDATE addresses SET city = REPLACE(city, 'Ã¼', 'ü') WHERE city LIKE '%Ã¼%';
UPDATE addresses SET city = REPLACE(city, 'Ã³', 'ó') WHERE city LIKE '%Ã³%';

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

UPDATE attachments SET name = REPLACE(name, 'â€™', ''') WHERE name LIKE '%â€™%';
UPDATE attachments SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE attachments SET name = REPLACE(name, 'Ã¼', 'ü') WHERE name LIKE '%Ã¼%';

UPDATE attachments SET description = REPLACE(description, 'â€™', ''') WHERE description LIKE '%â€™%';
UPDATE attachments SET description = REPLACE(description, 'â€œ', '"') WHERE description LIKE '%â€œ%';
UPDATE attachments SET description = REPLACE(description, 'â€', '"') WHERE description LIKE '%â€%';
UPDATE attachments SET description = REPLACE(description, 'Ã©', 'é') WHERE description LIKE '%Ã©%';
UPDATE attachments SET description = REPLACE(description, 'Ã¼', 'ü') WHERE description LIKE '%Ã¼%';

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

UPDATE contacts SET name = REPLACE(name, 'â€™', ''') WHERE name LIKE '%â€™%';
UPDATE contacts SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE contacts SET name = REPLACE(name, 'Ã¼', 'ü') WHERE name LIKE '%Ã¼%';
UPDATE contacts SET name = REPLACE(name, 'Ã³', 'ó') WHERE name LIKE '%Ã³%';

UPDATE contacts SET first_name = REPLACE(first_name, 'â€™', ''') WHERE first_name LIKE '%â€™%';
UPDATE contacts SET first_name = REPLACE(first_name, 'Ã©', 'é') WHERE first_name LIKE '%Ã©%';
UPDATE contacts SET first_name = REPLACE(first_name, 'Ã¼', 'ü') WHERE first_name LIKE '%Ã¼%';

UPDATE contacts SET last_name = REPLACE(last_name, 'â€™', ''') WHERE last_name LIKE '%â€™%';
UPDATE contacts SET last_name = REPLACE(last_name, 'Ã©', 'é') WHERE last_name LIKE '%Ã©%';
UPDATE contacts SET last_name = REPLACE(last_name, 'Ã¼', 'ü') WHERE last_name LIKE '%Ã¼%';

UPDATE contacts SET notes = REPLACE(notes, 'â€™', ''') WHERE notes LIKE '%â€™%';
UPDATE contacts SET notes = REPLACE(notes, 'â€œ', '"') WHERE notes LIKE '%â€œ%';
UPDATE contacts SET notes = REPLACE(notes, 'â€', '"') WHERE notes LIKE '%â€%';
UPDATE contacts SET notes = REPLACE(notes, 'Ã©', 'é') WHERE notes LIKE '%Ã©%';
UPDATE contacts SET notes = REPLACE(notes, 'Ã¼', 'ü') WHERE notes LIKE '%Ã¼%';

UPDATE contacts SET job_title = REPLACE(job_title, 'â€™', ''') WHERE job_title LIKE '%â€™%';
UPDATE contacts SET job_title = REPLACE(job_title, 'Ã©', 'é') WHERE job_title LIKE '%Ã©%';

-- ============================================================================
-- CUSTOMERS Table (re-apply in case some were missed)
-- ============================================================================
SELECT 'Fixing customers table...' as '';

UPDATE customers SET name = REPLACE(name, 'â€™', ''') WHERE name LIKE '%â€™%';
UPDATE customers SET name = REPLACE(name, 'â€œ', '"') WHERE name LIKE '%â€œ%';
UPDATE customers SET name = REPLACE(name, 'â€', '"') WHERE name LIKE '%â€%';
UPDATE customers SET name = REPLACE(name, 'â€"', '–') WHERE name LIKE '%â€"%';
UPDATE customers SET name = REPLACE(name, 'â€"', '—') WHERE name LIKE '%â€"%';
UPDATE customers SET name = REPLACE(name, 'â€‹', '') WHERE name LIKE '%â€‹%';
UPDATE customers SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE customers SET name = REPLACE(name, 'Ã¼', 'ü') WHERE name LIKE '%Ã¼%';
UPDATE customers SET name = REPLACE(name, 'Ã³', 'ó') WHERE name LIKE '%Ã³%';
UPDATE customers SET name = REPLACE(name, 'Ã§', 'ç') WHERE name LIKE '%Ã§%';

UPDATE customers SET trading_name = REPLACE(trading_name, 'â€™', ''') WHERE trading_name LIKE '%â€™%';
UPDATE customers SET trading_name = REPLACE(trading_name, 'Ã©', 'é') WHERE trading_name LIKE '%Ã©%';

UPDATE customers SET notes = REPLACE(notes, 'â€™', ''') WHERE notes LIKE '%â€™%';
UPDATE customers SET notes = REPLACE(notes, 'â€œ', '"') WHERE notes LIKE '%â€œ%';
UPDATE customers SET notes = REPLACE(notes, 'â€', '"') WHERE notes LIKE '%â€%';
UPDATE customers SET notes = REPLACE(notes, 'Ã©', 'é') WHERE notes LIKE '%Ã©%';

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

UPDATE documents SET shipping_details = REPLACE(shipping_details, 'â€™', ''') WHERE shipping_details LIKE '%â€™%';
UPDATE documents SET shipping_details = REPLACE(shipping_details, 'Ã©', 'é') WHERE shipping_details LIKE '%Ã©%';

UPDATE documents SET bill_to = REPLACE(bill_to, 'â€™', ''') WHERE bill_to LIKE '%â€™%';
UPDATE documents SET bill_to = REPLACE(bill_to, 'Ã©', 'é') WHERE bill_to LIKE '%Ã©%';

UPDATE documents SET ship_to = REPLACE(ship_to, 'â€™', ''') WHERE ship_to LIKE '%â€™%';
UPDATE documents SET ship_to = REPLACE(ship_to, 'Ã©', 'é') WHERE ship_to LIKE '%Ã©%';

UPDATE documents SET subject = REPLACE(subject, 'â€™', ''') WHERE subject LIKE '%â€™%';
UPDATE documents SET subject = REPLACE(subject, 'â€œ', '"') WHERE subject LIKE '%â€œ%';
UPDATE documents SET subject = REPLACE(subject, 'â€', '"') WHERE subject LIKE '%â€%';
UPDATE documents SET subject = REPLACE(subject, 'Ã©', 'é') WHERE subject LIKE '%Ã©%';

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

UPDATE emails SET `from` = REPLACE(`from`, 'â€™', ''') WHERE `from` LIKE '%â€™%';
UPDATE emails SET `from` = REPLACE(`from`, 'Ã©', 'é') WHERE `from` LIKE '%Ã©%';

UPDATE emails SET `to` = REPLACE(`to`, 'â€™', ''') WHERE `to` LIKE '%â€™%';
UPDATE emails SET `to` = REPLACE(`to`, 'Ã©', 'é') WHERE `to` LIKE '%Ã©%';

UPDATE emails SET subject = REPLACE(subject, 'â€™', ''') WHERE subject LIKE '%â€™%';
UPDATE emails SET subject = REPLACE(subject, 'â€œ', '"') WHERE subject LIKE '%â€œ%';
UPDATE emails SET subject = REPLACE(subject, 'â€', '"') WHERE subject LIKE '%â€%';
UPDATE emails SET subject = REPLACE(subject, 'Ã©', 'é') WHERE subject LIKE '%Ã©%';
UPDATE emails SET subject = REPLACE(subject, 'Ã¼', 'ü') WHERE subject LIKE '%Ã¼%';

UPDATE emails SET body = REPLACE(body, 'â€™', ''') WHERE body LIKE '%â€™%';
UPDATE emails SET body = REPLACE(body, 'â€œ', '"') WHERE body LIKE '%â€œ%';
UPDATE emails SET body = REPLACE(body, 'â€', '"') WHERE body LIKE '%â€%';
UPDATE emails SET body = REPLACE(body, 'â€"', '–') WHERE body LIKE '%â€"%';
UPDATE emails SET body = REPLACE(body, 'â€"', '—') WHERE body LIKE '%â€"%';
UPDATE emails SET body = REPLACE(body, 'Ã©', 'é') WHERE body LIKE '%Ã©%';
UPDATE emails SET body = REPLACE(body, 'Ã¼', 'ü') WHERE body LIKE '%Ã¼%';
UPDATE emails SET body = REPLACE(body, 'Â°', '°') WHERE body LIKE '%Â°%';
UPDATE emails SET body = REPLACE(body, 'Â®', '®') WHERE body LIKE '%Â®%';

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

UPDATE enquiries SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE enquiries SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE enquiries SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE enquiries SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';
UPDATE enquiries SET comments = REPLACE(comments, 'Ã¼', 'ü') WHERE comments LIKE '%Ã¼%';

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

UPDATE invoices SET ship_via = REPLACE(ship_via, 'â€™', ''') WHERE ship_via LIKE '%â€™%';
UPDATE invoices SET ship_via = REPLACE(ship_via, 'Ã©', 'é') WHERE ship_via LIKE '%Ã©%';

UPDATE invoices SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE invoices SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE invoices SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE invoices SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

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

UPDATE jobs SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE jobs SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE jobs SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE jobs SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

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

UPDATE line_items SET name = REPLACE(name, 'â€™', ''') WHERE name LIKE '%â€™%';
UPDATE line_items SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE line_items SET name = REPLACE(name, 'Â°', '°') WHERE name LIKE '%Â°%';
UPDATE line_items SET name = REPLACE(name, 'Â®', '®') WHERE name LIKE '%Â®%';

UPDATE line_items SET description = REPLACE(description, 'â€™', ''') WHERE description LIKE '%â€™%';
UPDATE line_items SET description = REPLACE(description, 'â€œ', '"') WHERE description LIKE '%â€œ%';
UPDATE line_items SET description = REPLACE(description, 'â€', '"') WHERE description LIKE '%â€%';
UPDATE line_items SET description = REPLACE(description, 'Ã©', 'é') WHERE description LIKE '%Ã©%';
UPDATE line_items SET description = REPLACE(description, 'Â°', '°') WHERE description LIKE '%Â°%';
UPDATE line_items SET description = REPLACE(description, 'Â®', '®') WHERE description LIKE '%Â®%';

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

UPDATE order_acknowledgements SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE order_acknowledgements SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE order_acknowledgements SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE order_acknowledgements SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

-- ============================================================================
-- PRINCIPLES Table (re-apply)
-- ============================================================================
SELECT 'Fixing principles table...' as '';

UPDATE principles SET name = REPLACE(name, 'Ã¼', 'ü') WHERE name LIKE '%Ã¼%';
UPDATE principles SET name = REPLACE(name, 'Ã©', 'é') WHERE name LIKE '%Ã©%';
UPDATE principles SET name = REPLACE(name, 'Ã³', 'ó') WHERE name LIKE '%Ã³%';

UPDATE principles SET address = REPLACE(address, 'ÃŸ', 'ß') WHERE address LIKE '%ÃŸ%';
UPDATE principles SET address = REPLACE(address, 'Ã¼', 'ü') WHERE address LIKE '%Ã¼%';
UPDATE principles SET address = REPLACE(address, 'Ã©', 'é') WHERE address LIKE '%Ã©%';
UPDATE principles SET address = REPLACE(address, 'Ã±', 'ñ') WHERE address LIKE '%Ã±%';

UPDATE principles SET city = REPLACE(city, 'Ã¼', 'ü') WHERE city LIKE '%Ã¼%';
UPDATE principles SET city = REPLACE(city, 'Ã©', 'é') WHERE city LIKE '%Ã©%';

-- ============================================================================
-- PRODUCTS Table (re-apply with all patterns)
-- ============================================================================
SELECT 'Fixing products table...' as '';

-- Mojibake
UPDATE products SET title = REPLACE(title, 'Â°', '°') WHERE title LIKE '%Â°%';
UPDATE products SET title = REPLACE(title, 'Â®', '®') WHERE title LIKE '%Â®%';
UPDATE products SET title = REPLACE(title, 'â„¢', '™') WHERE title LIKE '%â„¢%';
UPDATE products SET title = REPLACE(title, 'â€™', ''') WHERE title LIKE '%â€™%';
UPDATE products SET title = REPLACE(title, 'â€œ', '"') WHERE title LIKE '%â€œ%';
UPDATE products SET title = REPLACE(title, 'â€', '"') WHERE title LIKE '%â€%';

UPDATE products SET description = REPLACE(description, 'Â°', '°') WHERE description LIKE '%Â°%';
UPDATE products SET description = REPLACE(description, 'Â®', '®') WHERE description LIKE '%Â®%';
UPDATE products SET description = REPLACE(description, 'â„¢', '™') WHERE description LIKE '%â„¢%';
UPDATE products SET description = REPLACE(description, 'â€™', ''') WHERE description LIKE '%â€™%';

UPDATE products SET item_description = REPLACE(item_description, 'Â°', '°') WHERE item_description LIKE '%Â°%';
UPDATE products SET item_description = REPLACE(item_description, 'Â®', '®') WHERE item_description LIKE '%Â®%';

-- HTML entities
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 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;%';

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

UPDATE purchase_orders SET ship_via = REPLACE(ship_via, 'â€™', ''') WHERE ship_via LIKE '%â€™%';
UPDATE purchase_orders SET ship_via = REPLACE(ship_via, 'Ã©', 'é') WHERE ship_via LIKE '%Ã©%';

UPDATE purchase_orders SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE purchase_orders SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE purchase_orders SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE purchase_orders SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

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

UPDATE quotes SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE quotes SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE quotes SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE quotes SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

UPDATE quotes SET notes = REPLACE(notes, 'â€™', ''') WHERE notes LIKE '%â€™%';
UPDATE quotes SET notes = REPLACE(notes, 'â€œ', '"') WHERE notes LIKE '%â€œ%';
UPDATE quotes SET notes = REPLACE(notes, 'â€', '"') WHERE notes LIKE '%â€%';
UPDATE quotes SET notes = REPLACE(notes, 'Ã©', 'é') WHERE notes LIKE '%Ã©%';

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

UPDATE shipments SET ship_via = REPLACE(ship_via, 'â€™', ''') WHERE ship_via LIKE '%â€™%';
UPDATE shipments SET ship_via = REPLACE(ship_via, 'Ã©', 'é') WHERE ship_via LIKE '%Ã©%';

UPDATE shipments SET comments = REPLACE(comments, 'â€™', ''') WHERE comments LIKE '%â€™%';
UPDATE shipments SET comments = REPLACE(comments, 'â€œ', '"') WHERE comments LIKE '%â€œ%';
UPDATE shipments SET comments = REPLACE(comments, 'â€', '"') WHERE comments LIKE '%â€%';
UPDATE shipments SET comments = REPLACE(comments, 'Ã©', 'é') WHERE comments LIKE '%Ã©%';

-- ============================================================================
-- Verification
-- ============================================================================
SELECT '' as '';
SELECT '============================================================' as '';
SELECT 'FIX COMPLETE - Verification' as '';
SELECT '============================================================' as '';

SELECT 'Run scripts/scan_all_tables_for_corruption.sql to see remaining corruption' as '';
