-- Backfill county_id for existing oil_prices records based on state+zone mapping
-- Run this ONCE after adding the county_id column to oil_prices

-- First, add the column if it doesn't exist
ALTER TABLE oil_prices ADD COLUMN IF NOT EXISTS county_id INTEGER;

-- Connecticut
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'New London' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 1 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Windham' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 2 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'New Haven' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 3 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Middlesex' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 4 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'New Haven' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 5 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Hartford' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 6 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Litchfield' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 7 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Fairfield' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 8 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Tolland' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 9 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Litchfield' AND state = 'CT') WHERE LOWER(state) = 'connecticut' AND zone = 10 AND county_id IS NULL;

-- Massachusetts
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Suffolk' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 1 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Middlesex' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 2 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Norfolk' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 3 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Plymouth' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 4 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Middlesex' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 5 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Bristol' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 6 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Barnstable' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 7 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Essex' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 8 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Essex' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 9 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Worcester' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 10 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Worcester' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 11 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Hampshire' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 12 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Hampden' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 13 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Franklin' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 14 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Berkshire' AND state = 'MA') WHERE LOWER(state) = 'massachusetts' AND zone = 15 AND county_id IS NULL;

-- New Hampshire
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Coos' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 1 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Strafford' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 2 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Merrimack' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 3 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Grafton' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 4 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Cheshire' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 5 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Hillsborough' AND state = 'NH') WHERE LOWER(state) = 'newhampshire' AND zone = 6 AND county_id IS NULL;

-- Rhode Island
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Newport' AND state = 'RI') WHERE LOWER(state) = 'rhodeisland' AND zone = 1 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Providence' AND state = 'RI') WHERE LOWER(state) = 'rhodeisland' AND zone = 2 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Washington' AND state = 'RI') WHERE LOWER(state) = 'rhodeisland' AND zone = 3 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Kent' AND state = 'RI') WHERE LOWER(state) = 'rhodeisland' AND zone = 4 AND county_id IS NULL;

-- Maine
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Cumberland' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 1 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Kennebec' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 2 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Androscoggin' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 3 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'York' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 4 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Knox' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 5 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Penobscot' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 6 AND county_id IS NULL;
UPDATE oil_prices SET county_id = (SELECT id FROM county WHERE name = 'Washington' AND state = 'ME') WHERE LOWER(state) = 'maine' AND zone = 7 AND county_id IS NULL;

-- Verify
SELECT state, zone, county_id, COUNT(*) as record_count
FROM oil_prices
GROUP BY state, zone, county_id
ORDER BY state, zone;
