-- Test script to verify trigger is working
-- Run this after creating the trigger

-- Enable notice messages so we can see RAISE NOTICE output

-- First, check current trigger status
\echo '=== Checking trigger status ==='
SELECT 
    tgname AS trigger_name,
    tgrelid::regclass AS table_name,
    CASE tgenabled 
        WHEN 'O' THEN 'enabled'
        WHEN 'D' THEN 'disabled'
        ELSE 'unknown'
    END AS status
FROM pg_trigger
WHERE tgname = 'trg_task_fixed_update_visibility';

-- Check if function exists
\echo '=== Checking function exists ==='
SELECT proname, pronargs 
FROM pg_proc 
WHERE proname = 'fn_task_fixed_update_visibility';

-- Find a task to test with (one that's not already 'Fixed')
\echo '=== Finding test task ==='
SELECT id, status, store_code, customer_id, week_start_date
FROM task
WHERE status IS NOT NULL 
  AND lower(status) != 'fixed'
LIMIT 5;

-- Test update (replace <task_id> with actual ID from above)
-- Uncomment and modify:
/*
\echo '=== Testing trigger ==='
UPDATE task
SET status = 'Fixed'
WHERE id = <task_id>
RETURNING id, status;

-- Check if store_user_link was updated
\echo '=== Checking store_user_link update ==='
SELECT store_code, availability, soh_no_sales, oos, updated_at
FROM store_user_link
WHERE store_code = '<store_code>' 
  AND customer_id = <customer_id>
LIMIT 1;

-- Check if store_visibility was updated
\echo '=== Checking store_visibility update ==='
SELECT * FROM store_visibility
WHERE store_code = '<store_code>' 
  AND customer_id = <customer_id>
ORDER BY updated_at DESC
LIMIT 1;
*/

