-- =============================================
-- 006_template_conditions.sql
-- =============================================
-- Adds the `conditions` JSONB column to task_tasktemplate (if missing)
-- then populates it for every NON-universal template.
--
-- Conditions tell the app which home features a template applies to.
-- Templates that are truly universal (everyone should see them) are
-- left with the default empty '{}' and receive NO UPDATE here.
--
-- Run after: 005_regional_templates.sql
-- =============================================

-- Ensure the column exists (idempotent)
ALTER TABLE task_tasktemplate
    ADD COLUMN IF NOT EXISTS conditions JSONB NOT NULL DEFAULT '{}';

-- =============================================
-- BASE TEMPLATES (from 003_task_templates.sql)
-- =============================================

-- -----------------------------------------------
-- PLUMBING (IDs 1–16)
-- -----------------------------------------------

-- Water heater anode rod → only tank-style heaters have anode rods
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Check/Replace Water Heater Anode Rod';

-- Test Interior Water Shutoffs → universal (every home has shutoffs)
-- Test Gas Shutoffs → only homes with gas
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "boiler"]}'
WHERE title = 'Test Gas Shutoffs';

-- Test Water Meter Shutoff → universal
-- Check Water Meter for Leaks → universal
-- Run Drain Cleaner → universal
-- Test Water Heater Pressure Relief Valve → tank-style water heaters
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Test Water Heater Pressure Relief Valve';

-- Replace Water Filters → universal (any home can have water filters)

-- Flush Water Heater (ID 9) → tank-style water heaters
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 9;

-- Inspect for Leaks → universal
-- Inspect Caulking → universal

-- Septic Tank Inspection → only homes with septic systems
UPDATE task_tasktemplate SET conditions = '{"has_septic": true}'
WHERE title = 'Septic Tank Inspection';

-- Winterize Outdoor Faucets → universal (any home with outdoor faucets)
-- Replace Fridge Water Line → universal (any home with a fridge water line)
-- Replace Laundry Hoses → universal
-- Test Water Sensors → universal

-- -----------------------------------------------
-- SAFETY (IDs 17–25)
-- -----------------------------------------------

-- Test Smoke and Carbon Monoxide Detectors → universal
-- Check Fire Extinguishers → universal
-- Replace Smoke and CO Detector Batteries → universal
-- Replace Smoke and CO Detectors → universal
-- Test GFCI Outlets → universal

-- Schedule Chimney Cleaning → only homes with fireplaces/chimneys
UPDATE task_tasktemplate SET conditions = '{"has_fireplace": true}'
WHERE title = 'Schedule Chimney Cleaning';

-- Termite Inspection → universal (relevant everywhere, region-filtered separately)
-- Pest Control Treatment → universal
-- Check/Charge Security Cameras → universal

-- -----------------------------------------------
-- HVAC (IDs 26–31)
-- -----------------------------------------------

-- Change HVAC Filters → homes with ducted heating or central AC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Change HVAC Filters';

-- Flush HVAC Drain Lines → homes with AC condensate lines
UPDATE task_tasktemplate SET conditions = '{"cooling_type": ["central_ac", "mini_split"]}'
WHERE title = 'Flush HVAC Drain Lines';

-- Clean Return Vents → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Clean Return Vents';

-- Clean Floor Registers → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Clean Floor Registers';

-- Clean HVAC Compressor Coils → outdoor AC unit
UPDATE task_tasktemplate SET conditions = '{"cooling_type": ["central_ac", "mini_split"]}'
WHERE title = 'Clean HVAC Compressor Coils';

-- Schedule HVAC Inspection and Service → homes with HVAC systems
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump", "boiler"], "cooling_type": ["central_ac", "mini_split"]}'
WHERE title = 'Schedule HVAC Inspection and Service';

-- -----------------------------------------------
-- APPLIANCES (IDs 32–39) — mostly universal
-- -----------------------------------------------

-- Clean Microwave → universal
-- Clean Toaster → universal

-- Clean Garbage Disposal → universal (common enough)

-- Clean Oven → universal
-- Clean Refrigerator Coils → universal
-- Clean Dishwasher Filter → universal
-- Clean Vent Hood Filters → universal
-- Clean Dryer Vent (ID 39) → universal

-- -----------------------------------------------
-- CLEANING (IDs 40–56) — all universal
-- -----------------------------------------------

-- Wipe Kitchen Counters → universal
-- Take Out Trash → universal
-- Vacuum Floors → universal
-- Mop Hard Floors → universal
-- Clean Bathrooms → universal
-- Change Bed Linens → universal
-- Do Laundry → universal
-- Clean Kitchen Appliances → universal
-- Dust Surfaces → universal
-- Clean Out Refrigerator → universal
-- Clean Vacuum → universal
-- Clean Bathroom Exhaust Fans → universal
-- Vacuum Under Furniture → universal
-- Clean Inside Trash Cans → universal
-- Clean Window Tracks → universal
-- Deep Clean Carpets → universal
-- Clean and Reverse Ceiling Fans → universal

-- -----------------------------------------------
-- EXTERIOR (IDs 57–66)
-- -----------------------------------------------

-- Clean Gutters → universal (most houses have gutters)
-- Wash Windows → universal

-- Inspect Roof → universal (every property has a roof)

-- Service Garage Door → only homes with a garage
UPDATE task_tasktemplate SET conditions = '{"has_garage": true}'
WHERE title = 'Service Garage Door' AND id = 60;

-- Inspect Weather Stripping → universal

-- Pressure Wash Exterior → homes with washable exteriors
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["vinyl_siding", "brick", "stucco", "fiber_cement"]}'
WHERE title = 'Pressure Wash Exterior';

-- Touch Up Exterior Paint → homes with painted exteriors
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["vinyl_siding", "wood", "fiber_cement", "stucco"]}'
WHERE title = 'Touch Up Exterior Paint';

-- Service Sprinkler System → homes with sprinkler systems
UPDATE task_tasktemplate SET conditions = '{"has_sprinkler_system": true}'
WHERE title = 'Service Sprinkler System' AND id = 64;

-- Weed Garden Beds → universal (any home could have garden beds)
-- Water Indoor Plants → universal


-- =============================================
-- REGIONAL TEMPLATES (from 005_regional_templates.sql)
-- =============================================

-- -----------------------------------------------
-- ZONE 1: Hot-Humid (IDs 100–105, 190–197)
-- -----------------------------------------------

-- Hurricane Season Prep → universal within region (no feature condition)
-- Inspect for Mold & Mildew → universal within region

-- Check AC Condensate Drain → homes with AC
UPDATE task_tasktemplate SET conditions = '{"cooling_type": ["central_ac", "mini_split"]}'
WHERE title = 'Check AC Condensate Drain';

-- Termite Inspection (Tropical) → universal within region
-- Dehumidifier Maintenance → universal within region (if you're in hot-humid, you likely have one)
-- Storm Shutter Test → universal within region

-- Service Whole-House Dehumidifier → universal within region
-- Check Exterior Paint for Peeling → homes with painted exteriors
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["wood", "fiber_cement", "vinyl_siding"]}'
WHERE title = 'Check Exterior Paint for Peeling';

-- Clean Dryer Vent (ID 192, zone 1) → universal
-- Inspect Siding for Moisture Damage → homes with siding
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["vinyl_siding", "wood", "fiber_cement"]}'
WHERE title = 'Inspect Siding for Moisture Damage';

-- Flush Water Heater (ID 194, zone 1) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 194;

-- Service Pool & Deck (Algae Prevention) → homes with pool
UPDATE task_tasktemplate SET conditions = '{"has_pool": true}'
WHERE title = 'Service Pool & Deck (Algae Prevention)';

-- Inspect Attic for Moisture & Ventilation → homes with attic
UPDATE task_tasktemplate SET conditions = '{"has_attic": true}'
WHERE title = 'Inspect Attic for Moisture & Ventilation';

-- Test Sump Pump (ID 197) → homes with basement/sump pump
UPDATE task_tasktemplate SET conditions = '{"has_basement": true}'
WHERE title = 'Test Sump Pump' AND id = 197;

-- -----------------------------------------------
-- ZONE 2: Hot-Dry (IDs 110–115, 170–181)
-- -----------------------------------------------

-- Wildfire Defensible Space — Zone 0 → universal within region
-- Wildfire Defensible Space — Zone 1 → universal within region

-- Inspect Roof for UV Damage → universal within region (every home has a roof)

-- Check for Scorpions & Desert Pests → universal within region
-- Dust AC Condenser Unit → homes with AC
UPDATE task_tasktemplate SET conditions = '{"cooling_type": ["central_ac", "mini_split"]}'
WHERE title = 'Dust AC Condenser Unit';

-- Check Foundation for Heat Cracks → universal within region

-- Service Evaporative Cooler (Swamp Cooler) → homes with evaporative cooling
UPDATE task_tasktemplate SET conditions = '{"cooling_type": ["evaporative"]}'
WHERE title = 'Service Evaporative Cooler (Swamp Cooler)';

-- Flush Water Heater (Hard Water) (ID 171) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater (Hard Water)';

-- Inspect Stucco & Exterior Walls → homes with stucco or siding
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["stucco", "vinyl_siding", "wood", "fiber_cement"]}'
WHERE title = 'Inspect Stucco & Exterior Walls';

-- Clean Solar Panels → universal within region (no specific home feature enum for solar)

-- Check Pool Equipment & Chemistry → homes with pool
UPDATE task_tasktemplate SET conditions = '{"has_pool": true}'
WHERE title = 'Check Pool Equipment & Chemistry';

-- Inspect Irrigation System → homes with sprinkler/irrigation
UPDATE task_tasktemplate SET conditions = '{"has_sprinkler_system": true}'
WHERE title = 'Inspect Irrigation System';

-- Replace HVAC Air Filter (ID 176, zone 2) → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Replace HVAC Air Filter' AND id = 176;

-- Seal Windows & Doors for Heat → universal within region

-- Inspect Attic Ventilation → homes with attic
UPDATE task_tasktemplate SET conditions = '{"has_attic": true}'
WHERE title = 'Inspect Attic Ventilation';

-- Treat Wood Fence & Deck (UV Protection) → universal within region (outdoor wood structures)

-- Service Garage Door in Extreme Heat → homes with garage
UPDATE task_tasktemplate SET conditions = '{"has_garage": true}'
WHERE title = 'Service Garage Door in Extreme Heat';

-- Test Smoke & CO Detectors (ID 181) → universal

-- -----------------------------------------------
-- ZONE 3: Mixed-Humid (IDs 120–124, 200–206)
-- -----------------------------------------------

-- Blow Out Irrigation System → homes with sprinkler/irrigation
UPDATE task_tasktemplate SET conditions = '{"has_sprinkler_system": true}'
WHERE title = 'Blow Out Irrigation System';

-- Insulate Exposed Pipes → universal within region

-- Check Foundation for Moisture → homes with basement
UPDATE task_tasktemplate SET conditions = '{"has_basement": true}'
WHERE title = 'Check Foundation for Moisture';

-- Test Radon Level → homes with basement
UPDATE task_tasktemplate SET conditions = '{"has_basement": true}'
WHERE title = 'Test Radon Level';

-- Spring Flood Assessment → universal within region

-- Clean Dryer Vent (ID 200) → universal
-- Service Lawn Mower for Season → homes with lawn
UPDATE task_tasktemplate SET conditions = '{"landscaping_type": ["lawn"]}'
WHERE title = 'Service Lawn Mower for Season';

-- Check Siding & Trim for Rot → homes with wood siding/trim
UPDATE task_tasktemplate SET conditions = '{"exterior_type": ["wood", "fiber_cement", "vinyl_siding"]}'
WHERE title = 'Check Siding & Trim for Rot';

-- Replace HVAC Air Filter (ID 203, zone 3) → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Replace HVAC Air Filter' AND id = 203;

-- Flush Water Heater (ID 204, zone 3) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 204;

-- Aerate & Overseed Lawn → homes with lawn
UPDATE task_tasktemplate SET conditions = '{"landscaping_type": ["lawn"]}'
WHERE title = 'Aerate & Overseed Lawn';

-- Test Smoke & CO Detectors (ID 206) → universal

-- -----------------------------------------------
-- ZONE 4: Mixed-Dry (IDs 130–134, 210–215)
-- -----------------------------------------------

-- Winterize Exterior Faucets & Hoses → universal within region

-- Check Attic Insulation Level → homes with attic
UPDATE task_tasktemplate SET conditions = '{"has_attic": true}'
WHERE title = 'Check Attic Insulation Level';

-- Inspect Weather Stripping & Caulk → universal within region

-- Seal Foundation Cracks → universal within region

-- Test Heating System Before Winter → homes with heating
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump", "boiler"]}'
WHERE title = 'Test Heating System Before Winter';

-- Replace HVAC Air Filter (ID 210, zone 4) → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Replace HVAC Air Filter' AND id = 210;

-- Clean Dryer Vent (ID 211) → universal

-- Flush Water Heater (ID 212, zone 4) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 212;

-- Service Lawn & Irrigation → homes with sprinkler/irrigation
UPDATE task_tasktemplate SET conditions = '{"has_sprinkler_system": true}'
WHERE title = 'Service Lawn & Irrigation';

-- Test Smoke & CO Detectors (ID 214) → universal

-- Clean Gutters & Downspouts → universal

-- -----------------------------------------------
-- ZONE 5: Cold (IDs 140–145, 220–224)
-- -----------------------------------------------

-- Roof Snow Raking → universal within region
-- Ice Dam Prevention Check → universal within region

-- Test Sump Pump & Backup → homes with basement/sump pump
UPDATE task_tasktemplate SET conditions = '{"has_basement": true}'
WHERE title = 'Test Sump Pump & Backup';

-- Winter Gutter Maintenance → universal within region

-- Check Foundation for Frost Heave → universal within region

-- Winterize Entire Plumbing System → universal within region

-- Replace HVAC Air Filter (ID 220, zone 5) → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Replace HVAC Air Filter' AND id = 220;

-- Clean Dryer Vent (ID 221) → universal

-- Flush Water Heater (ID 222, zone 5) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 222;

-- Test Smoke & CO Detectors (ID 223) → universal

-- Inspect & Clean Chimney (ID 224) → homes with fireplace
UPDATE task_tasktemplate SET conditions = '{"has_fireplace": true}'
WHERE title = 'Inspect & Clean Chimney' AND id = 224;

-- -----------------------------------------------
-- ZONE 6: Very Cold (IDs 150–153, 230–233)
-- -----------------------------------------------

-- Install/Check Heated Gutter System → universal within region
-- Verify Pipe Routing (Interior Walls) → universal within region
-- Heavy Snow Roof Raking → universal within region
-- Weekly Ice Dam & Gutter Inspection → universal within region

-- Flush Water Heater (ID 230, zone 6) → tank-style
UPDATE task_tasktemplate SET conditions = '{"water_heater_type": ["tank_gas", "tank_electric"]}'
WHERE title = 'Flush Water Heater' AND id = 230;

-- Test Smoke & CO Detectors (ID 231) → universal

-- Replace HVAC Air Filter (ID 232, zone 6) → homes with ducted HVAC
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump"], "cooling_type": ["central_ac"]}'
WHERE title = 'Replace HVAC Air Filter' AND id = 232;

-- Inspect & Clean Chimney (ID 233) → homes with fireplace
UPDATE task_tasktemplate SET conditions = '{"has_fireplace": true}'
WHERE title = 'Inspect & Clean Chimney' AND id = 233;

-- -----------------------------------------------
-- ZONE 7-8: Subarctic / Arctic (IDs 160–163)
-- -----------------------------------------------

-- Test Backup Heating System → homes with heating (all in this zone)
UPDATE task_tasktemplate SET conditions = '{"heating_type": ["gas_furnace", "electric", "heat_pump", "boiler"]}'
WHERE title = 'Test Backup Heating System';

-- Maintain Basement Above 55°F → homes with basement
UPDATE task_tasktemplate SET conditions = '{"has_basement": true}'
WHERE title = 'Maintain Basement Above 55°F';

-- Inspect Heat-Traced Pipes → universal within region
-- Verify Roof Structural Integrity for Snow Load → universal within region
