-- End-to-End Validation Queries for Production Plan Collaboration
-- Run these after testing the full flow to verify data consistency

-- 1. Verify production plan exists and has correct structure
SELECT p.plan_id, p.plan_code, p.product_name, p.target_quantity,
       p.completed_quantity, p.status,
       ROUND(p.completed_quantity * 100.0 / NULLIF(p.target_quantity, 0), 1) as progress_pct
FROM pro_production_plan p
WHERE p.tenant_id = 'TEST_TENANT'
ORDER BY p.created_at DESC LIMIT 5;

-- 2. Verify yarn ratios sum to 100%
SELECT yr.plan_id,
       COUNT(*) as ratio_count,
       SUM(yr.ratio) as total_ratio,
       CASE WHEN ABS(SUM(yr.ratio) - 1.0) < 0.01 THEN 'OK' ELSE 'MISMATCH' END as check_result
FROM pro_plan_yarn_ratio yr
GROUP BY yr.plan_id;

-- 3. Verify process steps — each plan should have exactly 5 steps
SELECT ps.plan_id,
       COUNT(*) as step_count,
       SUM(CASE WHEN ps.status = 2 THEN 1 ELSE 0 END) as completed_steps,
       CASE WHEN COUNT(*) = 5 THEN 'OK' ELSE 'MISSING_STEPS' END as check_result
FROM pro_plan_process_step ps
GROUP BY ps.plan_id;

-- 4. Verify share link lifecycle
SELECT sl.short_code, sl.status, sl.click_count,
       sl.expires_at, NOW() as current_time,
       CASE WHEN sl.expires_at > NOW() THEN 'VALID' ELSE 'EXPIRED' END as expiry_check,
       pf.factory_tenant_id
FROM pro_share_link sl
LEFT JOIN pro_plan_factory pf ON pf.plan_id = sl.plan_id
ORDER BY sl.created_at DESC LIMIT 5;

-- 5. CRITICAL: Verify data consistency — completed_quantity must equal SUM(inbound)
SELECT p.plan_id, p.plan_code,
       p.completed_quantity as plan_completed,
       COALESCE(SUM(ir.quantity), 0) as inbound_sum,
       CASE
         WHEN ABS(p.completed_quantity - COALESCE(SUM(ir.quantity), 0)) < 0.01 THEN 'CONSISTENT'
         ELSE 'DRIFT_DETECTED'
       END as consistency_check
FROM pro_production_plan p
LEFT JOIN pro_inbound_record ir ON ir.plan_id = p.plan_id
GROUP BY p.plan_id, p.plan_code, p.completed_quantity;

-- 6. Verify auto-completion: plans with completed_quantity >= target_quantity should be status=3
SELECT p.plan_id, p.plan_code,
       p.target_quantity, p.completed_quantity, p.status,
       CASE
         WHEN p.completed_quantity >= p.target_quantity AND p.status != 3 THEN 'SHOULD_BE_COMPLETED'
         WHEN p.completed_quantity < p.target_quantity AND p.status = 3 THEN 'PREMATURE_COMPLETION'
         ELSE 'OK'
       END as auto_complete_check
FROM pro_production_plan p
WHERE p.status IN (2, 3);

-- 7. Verify inventory logs match inbound records
SELECT ir.record_id, ir.quantity,
       il.change_qty, il.ref_type, il.ref_id,
       CASE
         WHEN il.log_id IS NULL THEN 'MISSING_LOG'
         WHEN ABS(ir.quantity - il.change_qty) < 0.01 THEN 'OK'
         ELSE 'QUANTITY_MISMATCH'
       END as log_check
FROM pro_inbound_record ir
LEFT JOIN inv_inventory_log il ON il.ref_id = ir.record_id AND il.ref_type = 'PRODUCTION';

-- 8. Summary report
SELECT
  (SELECT COUNT(*) FROM pro_production_plan) as total_plans,
  (SELECT COUNT(*) FROM pro_production_plan WHERE status = 3) as completed_plans,
  (SELECT COUNT(*) FROM pro_share_link) as total_links,
  (SELECT COUNT(*) FROM pro_share_link WHERE status = 2) as confirmed_links,
  (SELECT COUNT(*) FROM pro_inbound_record) as total_inbound_records,
  (SELECT COALESCE(SUM(quantity), 0) FROM pro_inbound_record) as total_inbound_meters,
  (SELECT COUNT(*) FROM inv_inventory_log WHERE ref_type = 'PRODUCTION') as production_inv_logs;
