-- ============================================================
-- FCQC - Seed Templates for All Garment Inspection Stages
-- Covers 19 templates: Supplier Audit through Post-Shipment CAPA
-- ============================================================

SET FOREIGN_KEY_CHECKS = 0;

-- ========================================================================
-- 1. SUPPLIER AUDIT TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(100, 'supplier_audit', 'Supplier Audit Checklist', 1, 'active',
  '{"auto_save_interval": 30, "gps_required": true, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(100, 100, 'overview', 'Audit Overview', 1, 'supplier_audit', '{"columns": 2}'),
(101, 100, 'facility_info', 'Facility Information', 2, 'supplier_audit', '{"columns": 2}'),
(102, 100, 'quality_system', 'Quality Management System', 3, 'supplier_audit', '{"columns": 2}'),
(103, 100, 'production_capacity', 'Production Capacity', 4, 'supplier_audit', '{"columns": 2}'),
(104, 100, 'social_compliance', 'Social Compliance', 5, 'supplier_audit', '{"columns": 2}'),
(105, 100, 'scoring', 'Audit Scoring', 6, 'supplier_audit', '{"columns": 1}'),
(106, 100, 'photos', 'Photo Evidence', 7, 'supplier_audit', '{"columns": 1}'),
(107, 100, 'decision', 'Final Decision', 8, 'supplier_audit', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1000, 100, 'supplier_name', 'text', 'Supplier Name', 1, 1, '{"required": true}', NULL, '{"placeholder": "Full legal name"}'),
(1001, 100, 'supplier_code', 'text', 'Supplier Code', 1, 2, '{"required": true}', NULL, '{"placeholder": "Internal supplier code"}'),
(1002, 100, 'audit_date', 'date', 'Audit Date', 1, 3, '{"required": true}', NULL, '{"default_value": "today"}'),
(1003, 100, 'audit_type', 'select', 'Audit Type', 1, 4, '{"required": true}',
  '[{"label":"Initial","value":"initial"},{"label":"Periodic","value":"periodic"},{"label":"Follow-up","value":"followup"},{"label":"For Cause","value":"for_cause"}]', NULL),
(1004, 100, 'auditor_name', 'text', 'Auditor Name', 1, 5, '{"required": true}', NULL, '{"placeholder": "Full name"}'),
(1005, 100, 'address', 'text', 'Facility Address', 1, 6, '{"required": true}', NULL, '{"placeholder": "Full address"}'),
(1006, 101, 'facility_size', 'number', 'Facility Size (sq ft)', 1, 1, '{"required": true, "min": 0}', NULL, '{"unit": "sq ft"}'),
(1007, 101, 'total_employees', 'number', 'Total Employees', 1, 2, '{"required": true, "min": 0}', NULL, NULL),
(1008, 101, 'num_sewing_lines', 'number', 'Number of Sewing Lines', 1, 3, '{"required": true, "min": 0}', NULL, NULL),
(1009, 101, 'num_machines', 'number', 'Number of Sewing Machines', 1, 4, '{"required": true, "min": 0}', NULL, NULL),
(1010, 101, 'certifications', 'multi_select', 'Certifications Held', 0, 5, NULL,
  '[{"label":"ISO 9001","value":"iso9001"},{"label":"ISO 14001","value":"iso14001"},{"label":"OHSAS 18001","value":"ohsas18001"},{"label":"SA 8000","value":"sa8000"},{"label":"WRAP","value":"wrap"},{"label":"OEKO-TEX","value":"oekotex"},{"label":"GOTS","value":"gots"},{"label":"BCI","value":"bci"}]', NULL),
(1011, 102, 'has_qms', 'select', 'QMS Documented?', 1, 1, '{"required": true}',
  '[{"label":"Yes - fully documented","value":"yes"},{"label":"Partially","value":"partial"},{"label":"No","value":"no"}]', NULL),
(1012, 102, 'qms_score', 'number', 'QMS Score (0-100)', 1, 2, '{"required": true, "min": 0, "max": 100}', NULL, NULL),
(1013, 102, 'has_aql_procedure', 'checkbox', 'AQL Inspection Procedure in Place', 1, 3, '{"required": true}', NULL, NULL),
(1014, 102, 'has_capa_system', 'checkbox', 'CAPA System in Place', 1, 4, '{"required": true}', NULL, NULL),
(1015, 102, 'has_calibration', 'checkbox', 'Equipment Calibration Program', 1, 5, '{"required": true}', NULL, NULL),
(1016, 102, 'has_training', 'checkbox', 'Quality Training Program', 1, 6, '{"required": true}', NULL, NULL),
(1017, 102, 'qms_notes', 'textarea', 'QMS Notes', 0, 7, NULL, NULL, '{"rows": 3}'),
(1018, 103, 'monthly_capacity', 'number', 'Monthly Capacity (pcs)', 1, 1, '{"required": true, "min": 0}', NULL, NULL),
(1019, 103, 'lead_time_days', 'number', 'Average Lead Time (days)', 1, 2, '{"required": true, "min": 0}', NULL, NULL),
(1020, 103, 'capacity_utilization', 'number', 'Current Capacity Utilization %', 1, 3, '{"required": true, "min": 0, "max": 100}', NULL, NULL),
(1021, 103, 'has_overflow', 'checkbox', 'Uses Subcontractors for Overflow?', 0, 4, NULL, NULL, NULL),
(1022, 103, 'capacity_notes', 'textarea', 'Capacity Notes', 0, 5, NULL, NULL, '{"rows": 2}'),
(1023, 104, 'min_age_verified', 'checkbox', 'Minimum Age Policy Verified', 1, 1, '{"required": true}', NULL, NULL),
(1024, 104, 'working_hours_compliant', 'select', 'Working Hours Compliant?', 1, 2, '{"required": true}',
  '[{"label":"Compliant","value":"compliant"},{"label":"Minor issues","value":"minor"},{"label":"Non-compliant","value":"noncompliant"}]', NULL),
(1025, 104, 'health_safety_score', 'number', 'Health & Safety Score (0-100)', 1, 3, '{"required": true, "min": 0, "max": 100}', NULL, NULL),
(1026, 104, 'fire_safety_ok', 'checkbox', 'Fire Safety Equipment OK?', 1, 4, '{"required": true}', NULL, NULL),
(1027, 104, 'has_medical', 'checkbox', 'Medical Facility On-site?', 0, 5, NULL, NULL, NULL),
(1028, 105, 'overall_score', 'number', 'Overall Audit Score (0-100)', 1, 1, '{"required": true, "min": 0, "max": 100}', NULL, NULL),
(1029, 105, 'supplier_grade', 'select', 'Supplier Grade', 1, 2, '{"required": true}',
  '[{"label":"A - Approved","value":"A"},{"label":"B - Conditional","value":"B"},{"label":"C - Probation","value":"C"},{"label":"D - Rejected","value":"D"}]', NULL),
(1030, 105, 'scorecard_detail', 'supplier_scorecard', 'Scorecard Breakdown', 0, 3, NULL, NULL, NULL),
(1031, 106, 'facility_photo', 'photo', 'Facility Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 5}'),
(1032, 107, 'audit_decision', 'select', 'Audit Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Conditionally Approved","value":"conditional"},{"label":"Not Approved","value":"rejected"},{"label":"Re-audit Required","value":"reaudit"}]', NULL),
(1033, 107, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 4}'),
(1034, 107, 'next_audit_date', 'date', 'Next Scheduled Audit Date', 0, 3, NULL, NULL, NULL);

-- ========================================================================
-- 2. FABRIC 4-POINT INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(101, 'fabric_4point', 'Fabric 4-Point Inspection', 1, 'active',
  '{"auto_save_interval": 15, "gps_required": false, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(110, 101, 'overview', 'Inspection Overview', 1, 'fabric_4point', '{"columns": 2}'),
(111, 101, 'rolls', 'Roll-by-Roll Inspection', 2, 'fabric_4point', '{"columns": 1}'),
(112, 101, 'summary', 'Inspection Summary', 3, 'fabric_4point', '{"columns": 2}'),
(113, 101, 'photos', 'Photo Evidence', 4, 'fabric_4point', '{"columns": 1}'),
(114, 101, 'decision', 'Final Decision', 5, 'fabric_4point', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1100, 110, 'supplier_name', 'text', 'Fabric Supplier', 1, 1, '{"required": true}', NULL, '{"placeholder": "Supplier name"}'),
(1101, 110, 'invoice_number', 'text', 'Invoice/Delivery Note #', 1, 2, '{"required": true}', NULL, NULL),
(1102, 110, 'fabric_type', 'select', 'Fabric Type', 1, 3, '{"required": true}',
  '[{"label":"Single Jersey","value":"single_jersey"},{"label":"Double Jersey","value":"double_jersey"},{"label":"Interlock","value":"interlock"},{"label":"Rib","value":"rib"},{"label":"Twill","value":"twill"},{"label":"Poplin","value":"poplin"},{"label":"Denim","value":"denim"},{"label":"Satin","value":"satin"},{"label":"Fleece","value":"fleece"},{"label":"Woven","value":"woven"},{"label":"Knitted","value":"knitted"},{"label":"Non-woven","value":"nonwoven"},{"label":"Other","value":"other"}]', NULL),
(1103, 110, 'fabric_content', 'text', 'Fabric Content/Composition', 1, 4, '{"required": true}', NULL, '{"placeholder": "e.g. 100% Cotton, 65/35 Poly/Cotton"}'),
(1104, 110, 'color_ref', 'text', 'Color / Reference', 1, 5, '{"required": true}', NULL, NULL),
(1105, 110, 'total_rolls', 'number', 'Total Rolls Received', 1, 6, '{"required": true, "min": 1}', NULL, NULL),
(1106, 110, 'total_yards', 'number', 'Total Yards/Meters Received', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(1107, 110, 'inspected_rolls', 'number', 'Rolls Inspected', 1, 8, '{"required": true, "min": 1}', NULL, NULL),
(1108, 110, 'inspector_name', 'text', 'Inspector Name', 1, 9, '{"required": true}', NULL, NULL),
(1109, 110, 'inspection_date', 'date', 'Inspection Date', 1, 10, '{"required": true}', NULL, '{"default_value": "today"}'),
(1110, 111, 'roll_table', 'roll_table', 'Roll Data Entry', 1, 1, '{"required": true}', NULL,
  '{"columns": ["roll_number","supplier","width","length","total_points","points_per_100","result"], "allow_add": true}'),
(1111, 112, 'total_points_all', 'number', 'Total Points (All Rolls)', 1, 1, '{"required": true, "min": 0}', NULL, '{"readonly": true, "computed": "roll_summary"}'),
(1112, 112, 'avg_points_per_100', 'number', 'Average Points/100 sq yds', 1, 2, '{"required": true, "min": 0}', NULL, '{"readonly": true, "computed": "roll_summary"}'),
(1113, 112, 'acceptance_criteria', 'select', 'Acceptance Criteria Used', 1, 3, '{"required": true}',
  '[{"label":"40 points/100 sq yds (Standard)","value":"40"},{"label":"20 points/100 sq yds (Strict)","value":"20"},{"label":"60 points/100 sq yds (Relaxed)","value":"60"}]', '{"default_value": "40"}'),
(1114, 112, 'passed_rolls', 'number', 'Number of Rolls PASSED', 1, 4, '{"required": true, "min": 0}', NULL, '{"readonly": true, "computed": "roll_summary"}'),
(1115, 112, 'failed_rolls', 'number', 'Number of Rolls FAILED', 1, 5, '{"required": true, "min": 0}', NULL, '{"readonly": true, "computed": "roll_summary"}'),
(1116, 112, 'fabric_grade', 'select', 'Overall Fabric Grade', 1, 6, '{"required": true}',
  '[{"label":"A - First Quality","value":"A"},{"label":"B - Seconds","value":"B"},{"label":"C - Reject","value":"C"}]', '{"readonly": true, "computed": "roll_summary"}'),
(1117, 113, 'fabric_photos', 'photo', 'Fabric Defect Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 10, "require_annotation": true}'),
(1118, 114, 'fabric_decision', 'select', 'Fabric Decision', 1, 1, '{"required": true}',
  '[{"label":"Accept - All Rolls Passed","value":"accept"},{"label":"Conditional - Minor Issues","value":"conditional"},{"label":"Reject - Return to Supplier","value":"reject"},{"label":"Re-inspect Required","value":"reinspect"}]', NULL),
(1119, 114, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 3. TRIMS / ACCESSORIES INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(102, 'trims', 'Trims & Accessories Inspection', 1, 'active',
  '{"auto_save_interval": 30, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(120, 102, 'overview', 'Overview', 1, 'trims', '{"columns": 2}'),
(121, 102, 'trims_check', 'Trims Verification', 2, 'trims', '{"columns": 2}'),
(122, 102, 'measurements', 'Trim Measurements', 3, 'trims', '{"columns": 1}'),
(123, 102, 'photos', 'Photos', 4, 'trims', '{"columns": 1}'),
(124, 102, 'decision', 'Decision', 5, 'trims', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1200, 120, 'po_number', 'text', 'PO Number', 1, 1, '{"required": true}', NULL, NULL),
(1201, 120, 'style_number', 'text', 'Style Number', 1, 2, '{"required": true}', NULL, NULL),
(1202, 120, 'supplier', 'text', 'Trim Supplier', 1, 3, '{"required": true}', NULL, NULL),
(1203, 120, 'inspection_date', 'date', 'Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(1204, 120, 'trims_received', 'number', 'Total Trim Items Received', 1, 5, '{"required": true, "min": 1}', NULL, NULL),
(1205, 121, 'trim_items', 'multi_select', 'Trim Types Verified', 1, 1, '{"required": true}',
  '[{"label":"Buttons","value":"buttons"},{"label":"Zippers","value":"zippers"},{"label":"Thread","value":"thread"},{"label":"Elastic","value":"elastic"},{"label":"Drawstrings","value":"drawstrings"},{"label":"Rivets","value":"rivets"},{"label":"Snaps/Poppers","value":"snaps"},{"label":"Hook & Eye","value":"hook_eye"},{"label":"Velcro","value":"velcro"},{"label":"Lace/Trim","value":"lace"},{"label":"Rib/Knit Trim","value":"rib"},{"label":"Shoulder Pads","value":"shoulder_pads"},{"label":"Labels (Woven)","value":"labels"},{"label":"Hang Tags","value":"hangtags"},{"label":"Care Labels","value":"care_labels"},{"label":"Other","value":"other"}]', NULL),
(1206, 121, 'quantity_accuracy', 'select', 'Quantity Match PO?', 1, 2, '{"required": true}',
  '[{"label":"Yes - all match","value":"yes"},{"label":"Partial match","value":"partial"},{"label":"No - discrepancies","value":"no"}]', NULL),
(1207, 121, 'color_match_trim', 'select', 'Color Match Approved?', 1, 3, '{"required": true}',
  '[{"label":"Match","value":"match"},{"label":"Slight variation","value":"slight"},{"label":"Does not match","value":"nomatch"}]', NULL),
(1208, 121, 'quality_check', 'select', 'Trim Quality', 1, 4, '{"required": true}',
  '[{"label":"Good","value":"good"},{"label":"Minor defects","value":"minor"},{"label":"Major defects","value":"major"}]', NULL),
(1209, 122, 'trim_defect_grid', 'defect_grid', 'Trim Defects', 0, 1, NULL, NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1210, 123, 'trim_photos', 'photo', 'Trim Photos', 0, 1, '{"max_files": 5}', NULL, '{"require_annotation": true}'),
(1211, 124, 'trim_decision', 'select', 'Trim Decision', 1, 1, '{"required": true}',
  '[{"label":"Accept","value":"accept"},{"label":"Conditional Accept","value":"conditional"},{"label":"Reject","value":"reject"}]', NULL),
(1212, 124, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 4. LAB DIP APPROVAL TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(103, 'lab_dip', 'Lab Dip Approval', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(130, 103, 'overview', 'Lab Dip Overview', 1, 'lab_dip', '{"columns": 2}'),
(131, 103, 'color_assessment', 'Color Assessment', 2, 'lab_dip', '{"columns": 2}'),
(132, 103, 'photos', 'Photo Evidence', 3, 'lab_dip', '{"columns": 1}'),
(133, 103, 'decision', 'Approval Decision', 4, 'lab_dip', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1300, 130, 'buyer_name', 'text', 'Buyer Name', 1, 1, '{"required": true}', NULL, NULL),
(1301, 130, 'style_number', 'text', 'Style Number', 1, 2, '{"required": true}', NULL, NULL),
(1302, 130, 'color_name', 'text', 'Color Name', 1, 3, '{"required": true}', NULL, NULL),
(1303, 130, 'color_code', 'text', 'Color Code (PMS/Hex)', 1, 4, '{"required": true}', NULL, NULL),
(1304, 130, 'supplier', 'text', 'Supplier/Mill', 1, 5, '{"required": true}', NULL, NULL),
(1305, 130, 'dip_revision', 'number', 'Dip Revision #', 1, 6, '{"required": true, "min": 1}', NULL, '{"default_value": 1}'),
(1306, 130, 'received_date', 'date', 'Date Received', 1, 7, '{"required": true}', NULL, NULL),
(1307, 131, 'color_standard_ref', 'text', 'Color Standard Reference', 1, 1, '{"required": true}', NULL, NULL),
(1308, 131, 'visual_match', 'select', 'Visual Match Result', 1, 2, '{"required": true}',
  '[{"label":"Excellent Match","value":"excellent"},{"label":"Good Match","value":"good"},{"label":"Fair - Minor Difference","value":"fair"},{"label":"Poor - Significant Difference","value":"poor"}]', NULL),
(1309, 131, 'dE_value', 'number', 'Delta E (dE) Reading', 0, 3, '{"min": 0, "max": 10}', NULL, '{"unit": "dE", "help_text": "Spectrophotometer reading if available"}'),
(1310, 131, 'light_source', 'select', 'Light Source Used', 0, 4, NULL,
  '[{"label":"D65 (Daylight)","value":"D65"},{"label":"TL84 (Fluorescent)","value":"TL84"},{"label":"A (Incandescent)","value":"A"},{"label":"UV","value":"UV"}]', NULL),
(1311, 131, 'metamerism', 'select', 'Metamerism Observed?', 0, 5, NULL,
  '[{"label":"None","value":"none"},{"label":"Slight","value":"slight"},{"label":"Significant","value":"significant"}]', NULL),
(1312, 131, 'wash_fastness', 'select', 'Wash Fastness Grade', 0, 6, NULL,
  '[{"label":"Grade 5 - Excellent","value":"5"},{"label":"Grade 4 - Good","value":"4"},{"label":"Grade 3 - Fair","value":"3"},{"label":"Grade 2 - Poor","value":"2"},{"label":"Grade 1 - Very Poor","value":"1"}]', NULL),
(1313, 131, 'crocking_dry', 'select', 'Crocking (Dry) Grade', 0, 7, NULL,
  '[{"label":"5 - Excellent","value":"5"},{"label":"4 - Good","value":"4"},{"label":"3 - Fair","value":"3"},{"label":"2 - Poor","value":"2"},{"label":"1 - Very Poor","value":"1"}]', NULL),
(1314, 131, 'crocking_wet', 'select', 'Crocking (Wet) Grade', 0, 8, NULL,
  '[{"label":"5 - Excellent","value":"5"},{"label":"4 - Good","value":"4"},{"label":"3 - Fair","value":"3"},{"label":"2 - Poor","value":"2"},{"label":"1 - Very Poor","value":"1"}]', NULL),
(1315, 132, 'standard_photo', 'photo', 'Color Standard Photo', 1, 1, '{"required": true}', NULL, NULL),
(1316, 132, 'lab_dip_photo', 'photo', 'Lab Dip Photo', 1, 2, '{"required": true}', NULL, NULL),
(1317, 132, 'comparison_photo', 'photo', 'Side-by-Side Comparison', 1, 3, '{"required": true}', NULL, NULL),
(1318, 133, 'dip_decision', 'select', 'Lab Dip Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Approved with Comments","value":"approved_comments"},{"label":"Revise & Resubmit","value":"revise"},{"label":"Rejected","value":"rejected"}]', NULL),
(1319, 133, 'approval_notes', 'textarea', 'Approval Notes', 1, 2, '{"required": true}', NULL, '{"rows": 4}');

-- ========================================================================
-- 5. PPM (PIECES PER MINUTE) INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(104, 'ppm', 'PPM (Production Pilot Monitoring)', 1, 'active',
  '{"auto_save_interval": 15, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(140, 104, 'overview', 'PPM Overview', 1, 'ppm', '{"columns": 2}'),
(141, 104, 'production_check', 'Production Check', 2, 'ppm', '{"columns": 2}'),
(142, 104, 'defects', 'Defect Recording', 3, 'ppm', '{"columns": 1}'),
(143, 104, 'decision', 'PPM Decision', 4, 'ppm', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1400, 140, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1401, 140, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1402, 140, 'line_number', 'text', 'Production Line #', 1, 3, '{"required": true}', NULL, NULL),
(1403, 140, 'production_date', 'date', 'Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(1404, 140, 'target_output', 'number', 'Target Output (pcs/hour)', 1, 5, '{"required": true, "min": 0}', NULL, NULL),
(1405, 140, 'actual_output', 'number', 'Actual Output (pcs/hour)', 1, 6, '{"required": true, "min": 0}', NULL, NULL),
(1406, 140, 'efficiency', 'number', 'Efficiency %', 1, 7, '{"required": true, "min": 0, "max": 100}', NULL, '{"readonly": true}'),
(1407, 141, 'mb_check', 'select', 'Material Bundling OK?', 1, 1, '{"required": true}',
  '[{"label":"OK","value":"ok"},{"label":"Issues","value":"issues"}]', NULL),
(1408, 141, 'operator_skill', 'select', 'Operator Skill Level Assessment', 1, 2, '{"required": true}',
  '[{"label":"Adequate","value":"adequate"},{"label":"Needs Training","value":"needs_training"},{"label":"Unsatisfactory","value":"unsatisfactory"}]', NULL),
(1409, 141, 'machine_pacing', 'select', 'Machine Pacing', 1, 3, '{"required": true}',
  '[{"label":"Optimal","value":"optimal"},{"label":"Too Fast","value":"too_fast"},{"label":"Too Slow","value":"too_slow"}]', NULL),
(1410, 141, 'bottleneck', 'textarea', 'Bottleneck / Issues Observed', 0, 4, NULL, NULL, '{"rows": 3}'),
(1411, 142, 'defect_grid', 'defect_grid', 'Defects Found', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1412, 143, 'ppm_decision', 'select', 'PPM Decision', 1, 1, '{"required": true}',
  '[{"label":"Line Ready for Bulk Production","value":"ready"},{"label":"Conditional - Minor Adjustments","value":"conditional"},{"label":"Not Ready - Corrective Action Required","value":"not_ready"}]', NULL),
(1413, 143, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 6. PILOT RUN INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(105, 'pilot_run', 'Pilot Run Inspection', 1, 'active',
  '{"auto_save_interval": 30, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(150, 105, 'overview', 'Pilot Overview', 1, 'pilot_run', '{"columns": 2}'),
(151, 105, 'sample_review', 'Sample Review', 2, 'pilot_run', '{"columns": 2}'),
(152, 105, 'measurements', 'Measurements', 3, 'pilot_run', '{"columns": 1}'),
(153, 105, 'construction', 'Construction Check', 4, 'pilot_run', '{"columns": 2}'),
(154, 105, 'photos', 'Photos', 5, 'pilot_run', '{"columns": 1}'),
(155, 105, 'decision', 'Pilot Decision', 6, 'pilot_run', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1500, 150, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1501, 150, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1502, 150, 'buyer_name', 'text', 'Buyer Name', 1, 3, '{"required": true}', NULL, NULL),
(1503, 150, 'pilot_date', 'date', 'Pilot Run Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(1504, 150, 'sample_quantity', 'number', 'Sample Quantity Produced', 1, 5, '{"required": true, "min": 0}', NULL, NULL),
(1505, 150, 'sample_size_inspected', 'number', 'Sample Size Inspected', 1, 6, '{"required": true, "min": 1}', NULL, NULL),
(1506, 151, 'appearance_match', 'select', 'Appearance vs Approved Sample?', 1, 1, '{"required": true}',
  '[{"label":"Excellent match","value":"excellent"},{"label":"Good match","value":"good"},{"label":"Minor differences","value":"minor"},{"label":"Significant differences","value":"major"}]', NULL),
(1507, 151, 'hand_feel', 'select', 'Hand Feel / Fabric Handle', 1, 2, '{"required": true}',
  '[{"label":"Matches standard","value":"match"},{"label":"Slightly different","value":"slight"},{"label":"Different - unacceptable","value":"different"}]', NULL),
(1508, 151, 'color_match_pilot', 'select', 'Color Match', 1, 3, '{"required": true}',
  '[{"label":"Match","value":"match"},{"label":"Slight variation","value":"slight"},{"label":"Does not match","value":"nomatch"}]', NULL),
(1509, 152, 'measurement_grid', 'measurement_grid', 'Pilot Measurements', 1, 1, '{"required": true}', NULL,
  '{"measurement_template_id": 1, "show_tolerances": true}'),
(1510, 153, 'stitch_quality', 'select', 'Stitch Quality', 1, 1, '{"required": true}',
  '[{"label":"Excellent","value":"excellent"},{"label":"Good","value":"good"},{"label":"Fair","value":"fair"},{"label":"Poor","value":"poor"}]', NULL),
(1511, 153, 'seam_finish', 'select', 'Seam Finish', 1, 2, '{"required": true}',
  '[{"label":"Clean - no issues","value":"clean"},{"label":"Minor issues","value":"minor"},{"label":"Major issues","value":"major"}]', NULL),
(1512, 153, 'trim_finish', 'select', 'Trim Finish (Threads Clean?)', 1, 3, '{"required": true}',
  '[{"label":"Clean","value":"clean"},{"label":"Some loose threads","value":"loose"},{"label":"Poor","value":"poor"}]', NULL),
(1513, 153, 'defect_grid', 'defect_grid', 'Pilot Defects', 0, 4, NULL, NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1514, 154, 'pilot_photos', 'photo', 'Pilot Run Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 8}'),
(1515, 155, 'pilot_decision', 'select', 'Pilot Run Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved for Production","value":"approved"},{"label":"Approved with Conditions","value":"conditional"},{"label":"Changes Required - Re-run Pilot","value":"rerun"},{"label":"Rejected","value":"rejected"}]', NULL),
(1516, 155, 'decision_notes', 'textarea', 'Decision Notes & Next Steps', 1, 2, '{"required": true}', NULL, '{"rows": 4}');

-- ========================================================================
-- 7. CUTTING INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(106, 'cutting', 'Cutting Inspection', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(160, 106, 'overview', 'Cutting Overview', 1, 'cutting', '{"columns": 2}'),
(161, 106, 'marker_check', 'Marker / Pattern Check', 2, 'cutting', '{"columns": 2}'),
(162, 106, 'lay_check', 'Lay Inspection', 3, 'cutting', '{"columns": 2}'),
(163, 106, 'bundle_check', 'Bundle Inspection', 4, 'cutting', '{"columns": 1}'),
(164, 106, 'defects', 'Cutting Defects', 5, 'cutting', '{"columns": 1}'),
(165, 106, 'photos', 'Photos', 6, 'cutting', '{"columns": 1}'),
(166, 106, 'decision', 'Decision', 7, 'cutting', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1600, 160, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1601, 160, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1602, 160, 'cutting_date', 'date', 'Cutting Date', 1, 3, '{"required": true}', NULL, '{"default_value": "today"}'),
(1603, 160, 'cutting_table_no', 'text', 'Cutting Table #', 0, 4, NULL, NULL, NULL),
(1604, 160, 'fabric_rolls_used', 'number', 'Fabric Rolls Used', 1, 5, '{"required": true, "min": 0}', NULL, NULL),
(1605, 160, 'total_plys_cut', 'number', 'Total Plys Cut', 1, 6, '{"required": true, "min": 0}', NULL, NULL),
(1606, 160, 'quantity_cut', 'number', 'Total Pieces Cut', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(1607, 161, 'marker_ref', 'text', 'Marker Reference #', 1, 1, '{"required": true}', NULL, NULL),
(1608, 161, 'marker_width_ok', 'checkbox', 'Marker Width Utilized Correctly?', 1, 2, '{"required": true}', NULL, NULL),
(1609, 161, 'marker_length_ok', 'checkbox', 'Marker Length Correct?', 1, 3, '{"required": true}', NULL, NULL),
(1610, 161, 'pattern_direction_ok', 'checkbox', 'Pattern Direction (Grain) Correct?', 1, 4, '{"required": true}', NULL, NULL),
(1611, 161, 'marker_efficiency', 'number', 'Marker Efficiency %', 0, 5, '{"min": 0, "max": 100}', NULL, NULL),
(1612, 162, 'lay_flatness', 'select', 'Lay Flatness', 1, 1, '{"required": true}',
  '[{"label":"Flat - no wrinkles","value":"flat"},{"label":"Minor wrinkles","value":"minor"},{"label":"Wrinkled - needs correction","value":"wrinkled"}]', NULL),
(1613, 162, 'lay_alignment', 'select', 'Lay Alignment (Edges Aligned?)', 1, 2, '{"required": true}',
  '[{"label":"Aligned","value":"aligned"},{"label":"Minor misalignment","value":"minor"},{"label":"Misaligned","value":"misaligned"}]', NULL),
(1614, 162, 'stripes_match', 'select', 'Stripe/Pattern Matching?', 0, 3, NULL,
  '[{"label":"Matched","value":"matched"},{"label":"N/A - solid fabric","value":"na"},{"label":"Not matched","value":"notmatched"}]', NULL),
(1615, 162, 'tension_ok', 'checkbox', 'Fabric Tension/Stretch Uniform?', 1, 4, '{"required": true}', NULL, NULL),
(1616, 163, 'bundle_table', 'bundle_table', 'Bundle Inspection Data', 0, 1, NULL, NULL,
  '{"allow_add": true, "columns": ["bundle_number","lay_number","total_plys","inspected_plys","defect_count","result"]}'),
(1617, 164, 'cutting_defects', 'defect_grid', 'Cutting Defects', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1618, 165, 'cutting_photos', 'photo', 'Cutting Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 8}'),
(1619, 166, 'cutting_decision', 'select', 'Cutting Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved - OK to Sew","value":"approved"},{"label":"Conditional - Minor Rework","value":"conditional"},{"label":"On Hold - Needs Correction","value":"hold"},{"label":"Reject","value":"reject"}]', NULL),
(1620, 166, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 8. PRINT INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(107, 'printing', 'Print Inspection', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(170, 107, 'overview', 'Print Overview', 1, 'printing', '{"columns": 2}'),
(171, 107, 'print_quality', 'Print Quality Check', 2, 'printing', '{"columns": 2}'),
(172, 107, 'defects', 'Print Defects', 3, 'printing', '{"columns": 1}'),
(173, 107, 'photos', 'Photos', 4, 'printing', '{"columns": 1}'),
(174, 107, 'decision', 'Decision', 5, 'printing', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1700, 170, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1701, 170, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1702, 170, 'print_type', 'select', 'Print Type', 1, 3, '{"required": true}',
  '[{"label":"Screen Print","value":"screen"},{"label":"Digital Print","value":"digital"},{"label":"Sublimation","value":"sublimation"},{"label":"Heat Transfer","value":"heat_transfer"},{"label":"PVC/Plastisol","value":"plastisol"},{"label":"Discharge","value":"discharge"},{"label":"Foam/ puff","value":"foam"},{"label":"Other","value":"other"}]', NULL),
(1703, 170, 'print_location', 'text', 'Print Location', 0, 4, NULL, NULL, '{"placeholder": "e.g. Center chest, Back"}'),
(1704, 170, 'print_date', 'date', 'Inspection Date', 1, 5, '{"required": true}', NULL, '{"default_value": "today"}'),
(1705, 170, 'quantity_inspected', 'number', 'Quantity Inspected', 1, 6, '{"required": true, "min": 0}', NULL, NULL),
(1706, 171, 'placement_accuracy', 'select', 'Placement Accuracy', 1, 1, '{"required": true}',
  '[{"label":"Correct","value":"correct"},{"label":"Slightly off (+-5mm)","value":"slight"},{"label":"Incorrect (>5mm off)","value":"incorrect"}]', NULL),
(1707, 171, 'color_accuracy', 'select', 'Color Accuracy vs Approved', 1, 2, '{"required": true}',
  '[{"label":"Match","value":"match"},{"label":"Slight variation","value":"slight"},{"label":"Does not match","value":"nomatch"}]', NULL),
(1708, 171, 'registration', 'select', 'Print Registration', 1, 3, '{"required": true}',
  '[{"label":"Perfect","value":"perfect"},{"label":"Minor misregistration","value":"minor"},{"label":"Significant misregistration","value":"major"}]', NULL),
(1709, 171, 'coverage', 'select', 'Ink Coverage / Opacity', 1, 4, '{"required": true}',
  '[{"label":"Good coverage","value":"good"},{"label":"Uneven / thin","value":"uneven"},{"label":"Too heavy / bleeding","value":"heavy"}]', NULL),
(1710, 171, 'hand_feel_print', 'select', 'Hand Feel (After Print)', 0, 5, NULL,
  '[{"label":"Acceptable","value":"acceptable"},{"label":"Too stiff","value":"stiff"},{"label":"Tacky / sticky","value":"tacky"}]', NULL),
(1711, 172, 'print_defects', 'defect_grid', 'Print Defects', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1712, 173, 'print_photos', 'photo', 'Print Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 6}'),
(1713, 174, 'print_decision', 'select', 'Print Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Conditional","value":"conditional"},{"label":"Rework Required","value":"rework"},{"label":"Reject","value":"reject"}]', NULL),
(1714, 174, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 9. EMBROIDERY INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(108, 'embroidery', 'Embroidery Inspection', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(180, 108, 'overview', 'Embroidery Overview', 1, 'embroidery', '{"columns": 2}'),
(181, 108, 'quality_check', 'Quality Check', 2, 'embroidery', '{"columns": 2}'),
(182, 108, 'defects', 'Defects', 3, 'embroidery', '{"columns": 1}'),
(183, 108, 'photos', 'Photos', 4, 'embroidery', '{"columns": 1}'),
(184, 108, 'decision', 'Decision', 5, 'embroidery', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1800, 180, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1801, 180, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1802, 180, 'embroidery_type', 'select', 'Embroidery Type', 1, 3, '{"required": true}',
  '[{"label":"Flat Embroidery","value":"flat"},{"label":"3D Puff Embroidery","value":"3d_puff"},{"label":"Chenille","value":"chenille"},{"label":"Patch","value":"patch"},{"label":"Bead/Sequins","value":"bead"},{"label":"Applique","value":"applique"}]', NULL),
(1803, 180, 'design_ref', 'text', 'Design Reference #', 1, 4, '{"required": true}', NULL, NULL),
(1804, 180, 'stitch_count', 'number', 'Total Stitch Count', 0, 5, '{"min": 0}', NULL, NULL),
(1805, 180, 'embroidery_date', 'date', 'Inspection Date', 1, 6, '{"required": true}', NULL, '{"default_value": "today"}'),
(1806, 180, 'quantity_inspected', 'number', 'Quantity Inspected', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(1807, 181, 'design_accuracy', 'select', 'Design Accuracy vs Spec', 1, 1, '{"required": true}',
  '[{"label":"Accurate","value":"accurate"},{"label":"Minor deviations","value":"minor"},{"label":"Major deviations","value":"major"}]', NULL),
(1808, 181, 'thread_tension', 'select', 'Thread Tension', 1, 2, '{"required": true}',
  '[{"label":"Correct","value":"correct"},{"label":"Too tight (puckering)","value":"tight"},{"label":"Too loose (loops)","value":"loose"}]', NULL),
(1809, 181, 'backing_quality', 'select', 'Backing Quality', 1, 3, '{"required": true}',
  '[{"label":"Clean","value":"clean"},{"label":"Needs trimming","value":"trim"},{"label":"Poor","value":"poor"}]', NULL),
(1810, 181, 'color_thread_match', 'select', 'Thread Color Match', 1, 4, '{"required": true}',
  '[{"label":"Match","value":"match"},{"label":"Slight variation","value":"slight"},{"label":"Wrong color","value":"wrong"}]', NULL),
(1811, 182, 'embroidery_defects', 'defect_grid', 'Embroidery Defects', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1812, 183, 'embroidery_photos', 'photo', 'Embroidery Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 6}'),
(1813, 184, 'embroidery_decision', 'select', 'Embroidery Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Conditional","value":"conditional"},{"label":"Rework Required","value":"rework"},{"label":"Reject","value":"reject"}]', NULL),
(1814, 184, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 10. SEWING INLINE INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(109, 'sewing_inline', 'Sewing Inline Inspection (Traffic Light)', 1, 'active',
  '{"auto_save_interval": 15, "photo_required": true, "traffic_light": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(190, 109, 'overview', 'Line Overview', 1, 'sewing_inline', '{"columns": 2}'),
(191, 109, 'hourly_checks', 'Hourly Traffic Light Checks', 2, 'sewing_inline', '{"columns": 1}'),
(192, 109, 'defect_analysis', 'Defect Analysis', 3, 'sewing_inline', '{"columns": 1}'),
(193, 109, 'photos', 'Photos', 4, 'sewing_inline', '{"columns": 1}'),
(194, 109, 'summary', 'Shift Summary', 5, 'sewing_inline', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(1900, 190, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(1901, 190, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(1902, 190, 'line_number', 'text', 'Production Line #', 1, 3, '{"required": true}', NULL, NULL),
(1903, 190, 'shift', 'select', 'Shift', 1, 4, '{"required": true}',
  '[{"label":"Morning","value":"morning"},{"label":"Afternoon","value":"afternoon"},{"label":"Night","value":"night"}]', NULL),
(1904, 190, 'inspection_date', 'date', 'Date', 1, 5, '{"required": true}', NULL, '{"default_value": "today"}'),
(1905, 190, 'total_operators', 'number', 'Total Operators on Line', 1, 6, '{"required": true, "min": 0}', NULL, NULL),
(1906, 190, 'target_per_hour', 'number', 'Target Output per Hour', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(1907, 191, 'hourly_check', 'hourly_check', 'Hourly Traffic Light Data', 1, 1, '{"required": true}', NULL,
  '{"hours": 8, "allow_photo_per_check": true}'),
(1908, 192, 'defect_grid', 'defect_grid', 'Inline Defects Found', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(1909, 192, 'top_defect_types', 'multi_select', 'Top Defect Types Identified', 0, 2, NULL,
  '[{"label":"Open Seam","value":"open_seam"},{"label":"Skipped Stitch","value":"skipped"},{"label":"Broken Stitch","value":"broken"},{"label":"Puckering","value":"puckering"},{"label":"Uneven Stitch","value":"uneven"},{"label":"Pleating","value":"pleating"},{"label":"Wrong Thread","value":"wrong_thread"},{"label":"Edge Not Finished","value":"edge"},{"label":"Tension Issue","value":"tension"},{"label":"Other","value":"other"}]', NULL),
(1910, 193, 'inline_photos', 'photo', 'Line Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 8}'),
(1911, 194, 'green_count', 'number', 'Green (Good) Hours', 1, 1, '{"required": true, "min": 0}', NULL, NULL),
(1912, 194, 'yellow_count', 'number', 'Yellow (Warning) Hours', 1, 2, '{"required": true, "min": 0}', NULL, NULL),
(1913, 194, 'red_count', 'number', 'Red (Critical) Hours', 1, 3, '{"required": true, "min": 0}', NULL, NULL),
(1914, 194, 'overall_line_status', 'select', 'Overall Line Status', 1, 4, '{"required": true}',
  '[{"label":"GREEN - Running Well","value":"GREEN"},{"label":"YELLOW - Needs Attention","value":"YELLOW"},{"label":"RED - Intervention Required","value":"RED"}]', NULL),
(1915, 194, 'shift_notes', 'textarea', 'Shift Summary Notes', 1, 5, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 11. SEWING END-LINE INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(110, 'sewing_endline', 'Sewing End-Line Inspection (AQL)', 1, 'active',
  '{"auto_save_interval": 30, "photo_required": true, "aql_enabled": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(200, 110, 'overview', 'End-Line Overview', 1, 'sewing_endline', '{"columns": 2}'),
(201, 110, 'measurements', 'Measurement Audit', 2, 'sewing_endline', '{"columns": 1}'),
(202, 110, 'defects', 'Defect Recording (AQL)', 3, 'sewing_endline', '{"columns": 1}'),
(203, 110, 'aql', 'AQL Evaluation', 4, 'sewing_endline', '{"columns": 1}'),
(204, 110, 'photos', 'Photos', 5, 'sewing_endline', '{"columns": 1}'),
(205, 110, 'decision', 'Decision', 6, 'sewing_endline', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2000, 200, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2001, 200, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2002, 200, 'line_number', 'text', 'Production Line #', 1, 3, '{"required": true}', NULL, NULL),
(2003, 200, 'total_production', 'number', 'Total Production (pcs)', 1, 4, '{"required": true, "min": 0}', NULL, NULL),
(2004, 200, 'lot_size', 'number', 'Lot Size (for AQL)', 1, 5, '{"required": true, "min": 1}', NULL, NULL),
(2005, 200, 'sample_size', 'number', 'Sample Size Inspected', 1, 6, '{"required": true, "min": 1}', NULL, NULL),
(2006, 200, 'inspection_date', 'date', 'Date', 1, 7, '{"required": true}', NULL, '{"default_value": "today"}'),
(2007, 201, 'measurement_grid', 'measurement_grid', 'End-Line Measurements', 1, 1, '{"required": true}', NULL,
  '{"measurement_template_id": 1, "show_tolerances": true}'),
(2008, 202, 'defect_grid', 'defect_grid', 'End-Line Defects', 1, 1, '{"required": true}', NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(2009, 203, 'aql_calculator', 'aql_calculator', 'AQL Calculation', 1, 1, '{"required": true}', NULL,
  '{"quality_plan_id": 1, "auto_calculate": true}'),
(2010, 204, 'endline_photos', 'photo', 'End-Line Photos', 0, 1, '{"max_files": 6}', NULL, NULL),
(2011, 205, 'endline_decision', 'select', 'End-Line Decision', 1, 1, '{"required": true}',
  '[{"label":"PASS - Ready for Finishing","value":"PASS"},{"label":"CONDITIONAL PASS","value":"CONDITIONAL_PASS"},{"label":"FAIL - Rework Required","value":"FAIL"},{"label":"HOLD - Needs Review","value":"HOLD"}]', NULL),
(2012, 205, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 12. GARMENT WASH INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(111, 'washing', 'Garment Wash Inspection', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(210, 111, 'overview', 'Wash Overview', 1, 'washing', '{"columns": 2}'),
(211, 111, 'wash_tests', 'Wash Test Results', 2, 'washing', '{"columns": 1}'),
(212, 111, 'appearance', 'Appearance After Wash', 3, 'washing', '{"columns": 2}'),
(213, 111, 'photos', 'Photos', 4, 'washing', '{"columns": 1}'),
(214, 111, 'decision', 'Decision', 5, 'washing', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2100, 210, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2101, 210, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2102, 210, 'wash_type', 'select', 'Wash Type', 1, 3, '{"required": true}',
  '[{"label":"Garment Wash","value":"garment_wash"},{"label":"Enzyme Wash","value":"enzyme"},{"label":"Stone Wash","value":"stone"},{"label":"Bleach Wash","value":"bleach"},{"label":"Softener Wash","value":"softener"},{"label":"Bio-Polishing","value":"bio_polish"},{"label":"Caustic Wash","value":"caustic"},{"label":"Industrial Laundry","value":"laundry"}]', NULL),
(2103, 210, 'wash_date', 'date', 'Inspection Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(2104, 210, 'batch_number', 'text', 'Batch/Lot Number', 1, 5, '{"required": true}', NULL, NULL),
(2105, 210, 'quantity_tested', 'number', 'Quantity Tested', 1, 6, '{"required": true, "min": 0}', NULL, NULL),
(2106, 211, 'wash_test', 'wash_test', 'Wash Test Data', 1, 1, '{"required": true}', NULL,
  '{"allow_add": true, "test_types": ["dimensional_stability","color_fastness","shrinkage","spirality","twist"]}'),
(2107, 212, 'appearance_after_wash', 'select', 'Appearance After Wash', 1, 1, '{"required": true}',
  '[{"label":"Excellent - no change","value":"excellent"},{"label":"Good - minimal change","value":"good"},{"label":"Fair - visible change","value":"fair"},{"label":"Poor - unacceptable","value":"poor"}]', NULL),
(2108, 212, 'color_bleeding', 'select', 'Color Bleeding / Staining', 1, 2, '{"required": true}',
  '[{"label":"None","value":"none"},{"label":"Slight","value":"slight"},{"label":"Significant","value":"significant"}]', NULL),
(2109, 212, 'shrinkage_pct', 'number', 'Overall Shrinkage %', 0, 3, '{"min": -10, "max": 10}', NULL, '{"unit": "%"}'),
(2110, 212, 'pilling_after_wash', 'select', 'Pilling After Wash', 0, 4, NULL,
  '[{"label":"None","value":"none"},{"label":"Slight","value":"slight"},{"label":"Moderate","value":"moderate"},{"label":"Severe","value":"severe"}]', NULL),
(2111, 213, 'wash_photos', 'photo', 'Wash Test Photos (Before/After)', 1, 1, '{"required": true}', NULL, '{"max_files": 8}'),
(2112, 214, 'wash_decision', 'select', 'Wash Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Conditional","value":"conditional"},{"label":"Failed - Needs Correction","value":"fail"},{"label":"Re-test Required","value":"retest"}]', NULL),
(2113, 214, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 13. FINISHING INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(112, 'finishing', 'Finishing Inspection', 1, 'active',
  '{"auto_save_interval": 30, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(220, 112, 'overview', 'Finishing Overview', 1, 'finishing', '{"columns": 2}'),
(221, 112, 'ironing', 'Ironing / Pressing Check', 2, 'finishing', '{"columns": 2}'),
(222, 112, 'spot_cleaning', 'Spot Cleaning / Stain Check', 3, 'finishing', '{"columns": 2}'),
(223, 112, 'trimming', 'Thread Trimming Check', 4, 'finishing', '{"columns": 2}'),
(224, 112, 'final_check', 'Final Visual Check', 5, 'finishing', '{"columns": 1}'),
(225, 112, 'photos', 'Photos', 6, 'finishing', '{"columns": 1}'),
(226, 112, 'decision', 'Decision', 7, 'finishing', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2200, 220, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2201, 220, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2202, 220, 'line_number', 'text', 'Line #', 0, 3, NULL, NULL, NULL),
(2203, 220, 'finishing_date', 'date', 'Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(2204, 220, 'quantity_inspected', 'number', 'Quantity Inspected', 1, 5, '{"required": true, "min": 0}', NULL, NULL),
(2205, 221, 'ironing_quality', 'select', 'Ironing / Pressing Quality', 1, 1, '{"required": true}',
  '[{"label":"Well pressed - no wrinkles","value":"good"},{"label":"Minor wrinkles","value":"minor"},{"label":"Poor - significant wrinkles","value":"poor"}]', NULL),
(2206, 221, 'crease_lines', 'select', 'Crease Lines Correct?', 1, 2, '{"required": true}',
  '[{"label":"Correct","value":"correct"},{"label":"Misaligned","value":"misaligned"},{"label":"Missing","value":"missing"}]', NULL),
(2207, 221, 'shininess', 'select', 'Shininess / Glazing Marks?', 1, 3, '{"required": true}',
  '[{"label":"None","value":"none"},{"label":"Minor","value":"minor"},{"label":"Significant","value":"significant"}]', NULL),
(2208, 222, 'stains_present', 'select', 'Stains Present?', 1, 1, '{"required": true}',
  '[{"label":"Clean - no stains","value":"clean"},{"label":"Minor stains (removable)","value":"minor"},{"label":"Significant stains","value":"significant"}]', NULL),
(2209, 222, 'spot_cleaning_result', 'select', 'Spot Cleaning Result', 0, 2, NULL,
  '[{"label":"N/A","value":"na"},{"label":"Successful","value":"success"},{"label":"Partially successful","value":"partial"},{"label":"Unsuccessful","value":"failed"}]', NULL),
(2210, 223, 'thread_trimming', 'select', 'Thread Trimming Quality', 1, 1, '{"required": true}',
  '[{"label":"Clean - no loose threads","value":"clean"},{"label":"Minor loose threads","value":"minor"},{"label":"Poor - many loose threads","value":"poor"}]', NULL),
(2211, 224, 'overall_appearance', 'select', 'Overall Appearance', 1, 1, '{"required": true}',
  '[{"label":"Excellent","value":"excellent"},{"label":"Good","value":"good"},{"label":"Fair","value":"fair"},{"label":"Poor","value":"poor"}]', NULL),
(2212, 224, 'packaging_readiness', 'checkbox', 'Ready for Packaging?', 1, 2, '{"required": true}', NULL, NULL),
(2213, 224, 'defect_grid', 'defect_grid', 'Finishing Defects', 0, 3, NULL, NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(2214, 225, 'finishing_photos', 'photo', 'Finishing Photos', 0, 1, '{"max_files": 6}', NULL, NULL),
(2215, 226, 'finishing_decision', 'select', 'Finishing Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved - Ready for Packing","value":"approved"},{"label":"Conditional","value":"conditional"},{"label":"Rework Required","value":"rework"},{"label":"Reject","value":"reject"}]', NULL),
(2216, 226, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 14. NEEDLE & METAL DETECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(113, 'needle_detection', 'Needle & Metal Detection', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(230, 113, 'overview', 'Detection Overview', 1, 'needle_detection', '{"columns": 2}'),
(231, 113, 'scan_log', 'Scan Log', 2, 'needle_detection', '{"columns": 1}'),
(232, 113, 'alarms', 'Alarm Investigation', 3, 'needle_detection', '{"columns": 2}'),
(233, 113, 'photos', 'Photos', 4, 'needle_detection', '{"columns": 1}'),
(234, 113, 'decision', 'Decision', 5, 'needle_detection', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2300, 230, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2301, 230, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2302, 230, 'machine_id', 'text', 'Needle Detector Machine ID', 1, 3, '{"required": true}', NULL, NULL),
(2303, 230, 'machine_model', 'text', 'Machine Model', 0, 4, NULL, NULL, NULL),
(2304, 230, 'last_calibration_date', 'date', 'Last Calibration Date', 1, 5, '{"required": true}', NULL, NULL),
(2305, 230, 'detection_date', 'date', 'Inspection Date', 1, 6, '{"required": true}', NULL, '{"default_value": "today"}'),
(2306, 230, 'operator_name', 'text', 'Operator Name', 1, 7, '{"required": true}', NULL, NULL),
(2307, 231, 'needle_log', 'needle_log', 'Needle Scan Log', 1, 1, '{"required": true}', NULL,
  '{"allow_add": true, "columns": ["scan_time","total_pieces","alarms","found_needles","result"]}'),
(2308, 232, 'total_alarms', 'number', 'Total Alarms Triggered', 1, 1, '{"required": true, "min": 0}', NULL, NULL),
(2309, 232, 'confirmed_needles', 'number', 'Confirmed Broken Needles Found', 1, 2, '{"required": true, "min": 0}', NULL, NULL),
(2310, 232, 'false_alarms', 'number', 'False Alarms', 1, 3, '{"required": true, "min": 0}', NULL, NULL),
(2311, 232, 'root_cause_needle', 'textarea', 'Root Cause (if needles found)', 0, 4, NULL, NULL, '{"rows": 3, "placeholder": "Investigate and document root cause"}'),
(2312, 233, 'needle_photos', 'photo', 'Detection Photos', 0, 1, '{"max_files": 5}', NULL, NULL),
(2313, 234, 'needle_decision', 'select', 'Needle Detection Decision', 1, 1, '{"required": true}',
  '[{"label":"PASS - Clear","value":"PASS"},{"label":"CONDITIONAL","value":"CONDITIONAL"},{"label":"FAIL - Needs Re-scan","value":"FAIL"},{"label":"HOLD - Investigation Needed","value":"HOLD"}]', NULL),
(2314, 234, 'decision_notes', 'textarea', 'Decision Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 15. PACKING INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(114, 'packing', 'Packing Inspection', 1, 'active',
  '{"auto_save_interval": 30, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(240, 114, 'overview', 'Packing Overview', 1, 'packing', '{"columns": 2}'),
(241, 114, 'packing_check', 'Packing Verification', 2, 'packing', '{"columns": 2}'),
(242, 114, 'carton_marking', 'Carton Marking', 3, 'packing', '{"columns": 2}'),
(243, 114, 'photos', 'Photos', 4, 'packing', '{"columns": 1}'),
(244, 114, 'decision', 'Decision', 5, 'packing', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2400, 240, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2401, 240, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2402, 240, 'packing_date', 'date', 'Date', 1, 3, '{"required": true}', NULL, '{"default_value": "today"}'),
(2403, 240, 'total_cartons', 'number', 'Total Cartons', 1, 4, '{"required": true, "min": 0}', NULL, NULL),
(2404, 240, 'cartons_inspected', 'number', 'Cartons Inspected', 1, 5, '{"required": true, "min": 0}', NULL, NULL),
(2405, 241, 'packing_method', 'select', 'Packing Method', 1, 1, '{"required": true}',
  '[{"label":"Polybag only","value":"polybag"},{"label":"Polybag + Carton","value":"polybag_carton"},{"label":"Hanger Pack","value":"hanger_pack"},{"label":"Flat Pack","value":"flat_pack"},{"label":"Bulk Pack","value":"bulk"}]', NULL),
(2406, 241, 'polybag_quality', 'select', 'Polybag Condition', 1, 2, '{"required": true}',
  '[{"label":"Good","value":"good"},{"label":"Fair","value":"fair"},{"label":"Poor","value":"poor"}]', NULL),
(2407, 241, 'polybag_thickness_ok', 'checkbox', 'Polybag Thickness Meets Spec?', 1, 3, '{"required": true}', NULL, NULL),
(2408, 241, 'carton_strength', 'select', 'Carton Strength/Quality', 1, 4, '{"required": true}',
  '[{"label":"Good","value":"good"},{"label":"Fair","value":"fair"},{"label":"Poor","value":"poor"}]', NULL),
(2409, 242, 'carton_marking_ok', 'checkbox', 'Carton Markings Correct?', 1, 1, '{"required": true}', NULL, NULL),
(2410, 242, 'carton_marking_photo', 'photo', 'Carton Marking Photo', 1, 2, '{"required": true}', NULL, NULL),
(2411, 242, 'qty_per_carton', 'number', 'Quantity per Carton', 1, 3, '{"required": true, "min": 0}', NULL, NULL),
(2412, 242, 'carton_weight_ok', 'checkbox', 'Carton Weight Within Spec?', 1, 4, '{"required": true}', NULL, NULL),
(2413, 242, 'mixed_sizes_ok', 'checkbox', 'Mixed Sizes/Cartons Correct?', 0, 5, NULL, NULL, NULL),
(2414, 243, 'packing_photos', 'photo', 'Packing Photos', 1, 1, '{"required": true}', NULL, '{"max_files": 6}'),
(2415, 244, 'packing_decision', 'select', 'Packing Decision', 1, 1, '{"required": true}',
  '[{"label":"Approved","value":"approved"},{"label":"Conditional","value":"conditional"},{"label":"Rework Required","value":"rework"},{"label":"Reject","value":"reject"}]', NULL),
(2416, 244, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 16. CARTON DROP TEST TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(115, 'carton_drop', 'Carton Drop Test', 1, 'active',
  '{"photo_required": true, "gps_required": false}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(250, 115, 'overview', 'Test Overview', 1, 'carton_drop', '{"columns": 2}'),
(251, 115, 'drop_tests', 'Drop Test Results', 2, 'carton_drop', '{"columns": 1}'),
(252, 115, 'photos', 'Photos', 3, 'carton_drop', '{"columns": 1}'),
(253, 115, 'decision', 'Decision', 4, 'carton_drop', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2500, 250, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2501, 250, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2502, 250, 'test_date', 'date', 'Test Date', 1, 3, '{"required": true}', NULL, '{"default_value": "today"}'),
(2503, 250, 'drop_height_cm', 'number', 'Drop Height (cm)', 1, 4, '{"required": true, "min": 0}', NULL, NULL),
(2504, 250, 'test_standard', 'select', 'Test Standard', 1, 5, '{"required": true}',
  '[{"label":"ISTA 1A","value":"ista_1a"},{"label":"ISTA 2A","value":"ista_2a"},{"label":"ASTM D4169","value":"astm_d4169"},{"label":"Internal Standard","value":"internal"},{"label":"Buyer Spec","value":"buyer"}]', NULL),
(2505, 250, 'sample_sizes', 'text', 'Sample Carton Sizes', 0, 6, NULL, NULL, '{"placeholder": "L x W x H in cm"}'),
(2506, 250, 'gross_weight_kg', 'number', 'Gross Weight (kg)', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(2507, 251, 'drop_test', 'drop_test', 'Drop Test Data', 1, 1, '{"required": true}', NULL,
  '{"allow_add": true, "orientations": ["face","edge","corner","flat"]}'),
(2508, 251, 'overall_drop_result', 'select', 'Overall Drop Test Result', 1, 2, '{"required": true}',
  '[{"label":"PASS - No damage","value":"PASS"},{"label":"MINOR DAMAGE - Acceptable","value":"MINOR"},{"label":"FAIL - Carton damaged","value":"FAIL"}]', NULL),
(2509, 252, 'drop_photos_before', 'photo', 'Photos Before Test', 1, 1, '{"required": true}', NULL, '{"max_files": 4}'),
(2510, 252, 'drop_photos_after', 'photo', 'Photos After Test', 1, 2, '{"required": true}', NULL, '{"max_files": 4}'),
(2511, 253, 'drop_decision', 'select', 'Drop Test Decision', 1, 1, '{"required": true}',
  '[{"label":"PASS - Packaging Approved","value":"PASS"},{"label":"CONDITIONAL - Minor Improvements","value":"CONDITIONAL"},{"label":"FAIL - Redesign Required","value":"FAIL"}]', NULL),
(2512, 253, 'decision_notes', 'textarea', 'Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 17. CONTAINER LOADING INSPECTION TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(116, 'container_loading', 'Container Loading Inspection', 1, 'active',
  '{"auto_save_interval": 30, "gps_required": true, "photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(260, 116, 'overview', 'Loading Overview', 1, 'container_loading', '{"columns": 2}'),
(261, 116, 'container_info', 'Container Information', 2, 'container_loading', '{"columns": 2}'),
(262, 116, 'loading_check', 'Loading Verification', 3, 'container_loading', '{"columns": 2}'),
(263, 116, 'photos', 'Photos', 4, 'container_loading', '{"columns": 1}'),
(264, 116, 'decision', 'Loading Decision', 5, 'container_loading', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2600, 260, 'style_number', 'text', 'Style Number', 1, 1, '{"required": true}', NULL, NULL),
(2601, 260, 'po_number', 'text', 'PO Number', 1, 2, '{"required": true}', NULL, NULL),
(2602, 260, 'loading_date', 'date', 'Loading Date', 1, 3, '{"required": true}', NULL, '{"default_value": "today"}'),
(2603, 260, 'booking_ref', 'text', 'Booking Reference', 1, 4, '{"required": true}', NULL, NULL),
(2604, 260, 'loading_type', 'select', 'Loading Type', 1, 5, '{"required": true}',
  '[{"label":"FCL - Full Container Load","value":"FCL"},{"label":"LCL - Less Container Load","value":"LCL"}]', NULL),
(2605, 261, 'container_number', 'text', 'Container Number', 1, 1, '{"required": true}', NULL, NULL),
(2606, 261, 'seal_number', 'text', 'Seal Number', 1, 2, '{"required": true}', NULL, NULL),
(2607, 261, 'container_size', 'select', 'Container Size', 1, 3, '{"required": true}',
  '[{"label":"20 GP","value":"20GP"},{"label":"40 GP","value":"40GP"},{"label":"40 HQ","value":"40HQ"},{"label":"45 HC","value":"45HC"},{"label":"Other","value":"other"}]', NULL),
(2608, 261, 'truck_plate', 'text', 'Truck Plate Number', 6, 4, '{"required": true}', NULL, NULL),
(2609, 261, 'driver_name', 'text', 'Driver Name', 1, 5, '{"required": true}', NULL, NULL),
(2610, 262, 'container_condition', 'select', 'Container Interior Condition', 1, 1, '{"required": true}',
  '[{"label":"Good - clean & dry","value":"good"},{"label":"Minor damage / dirt","value":"minor"},{"label":"Major damage - rejected","value":"major"}]', NULL),
(2611, 262, 'floor_condition_ok', 'checkbox', 'Floor Condition OK?', 1, 2, '{"required": true}', NULL, NULL),
(2612, 262, 'clean_dry_ok', 'checkbox', 'Clean & Dry?', 1, 3, '{"required": true}', NULL, NULL),
(2613, 262, 'no_odors', 'checkbox', 'No Unusual Odors?', 1, 4, '{"required": true}', NULL, NULL),
(2614, 262, 'stuffing_pattern_ok', 'checkbox', 'Stuffing Pattern Followed?', 1, 5, '{"required": true}', NULL, NULL),
(2615, 262, 'carton_count_match', 'select', 'Carton Count Matches Document?', 1, 6, '{"required": true}',
  '[{"label":"Match","value":"match"},{"label":"Minor discrepancy (<1%)","value":"minor"},{"label":"Major discrepancy","value":"major"}]', NULL),
(2616, 262, 'quantity_loaded', 'number', 'Total Quantity Loaded', 1, 7, '{"required": true, "min": 0}', NULL, NULL),
(2617, 262, 'temperature_c', 'number', 'Container Temperature (°C)', 0, 8, NULL, NULL, '{"unit": "°C"}'),
(2618, 262, 'humidity_pct', 'number', 'Container Humidity (%)', 0, 9, '{"min": 0, "max": 100}', NULL, '{"unit": "%"}'),
(2619, 263, 'container_interior_photo', 'photo', 'Interior Photo (Empty)', 1, 1, '{"required": true}', NULL, NULL),
(2620, 263, 'container_seal_photo', 'photo', 'Seal Number Photo', 1, 2, '{"required": true}', NULL, NULL),
(2621, 263, 'container_loaded_photo', 'photo', 'Loaded Container Photo', 1, 3, '{"required": true}', NULL, NULL),
(2622, 263, 'container_gps_photo', 'photo', 'Container with GPS Location', 0, 4, NULL, NULL, '{"require_gps": true}'),
(2623, 264, 'loading_decision', 'select', 'Loading Decision', 1, 1, '{"required": true}',
  '[{"label":"PASS - Seal & Release","value":"PASS"},{"label":"CONDITIONAL PASS","value":"CONDITIONAL"},{"label":"FAIL - Do Not Load","value":"FAIL"}]', NULL),
(2624, 264, 'loading_notes', 'textarea', 'Loading Notes', 1, 2, '{"required": true}', NULL, '{"rows": 3}');

-- ========================================================================
-- 18. POST-SHIPMENT CAPA TEMPLATE
-- ========================================================================
INSERT IGNORE INTO `inspection_templates` (`id`, `template_type`, `name`, `version`, `status`, `config`) VALUES
(117, 'capa_post_shipment', 'Post-Shipment CAPA Investigation', 1, 'active',
  '{"photo_required": true}');

INSERT IGNORE INTO `inspection_pages` (`id`, `template_id`, `page_key`, `label`, `sort_order`, `category`, `config`) VALUES
(270, 117, 'overview', 'CAPA Overview', 1, 'capa_post_shipment', '{"columns": 2}'),
(271, 117, 'problem_analysis', 'Problem Analysis', 2, 'capa_post_shipment', '{"columns": 2}'),
(272, 117, 'root_cause', 'Root Cause Analysis', 3, 'capa_post_shipment', '{"columns": 2}'),
(273, 117, 'action_plan', 'Corrective & Preventive Action', 4, 'capa_post_shipment', '{"columns": 2}'),
(274, 117, 'verification', 'Verification Plan', 5, 'capa_post_shipment', '{"columns": 2}'),
(275, 117, 'photos', 'Evidence Photos', 5, 'capa_post_shipment', '{"columns": 1}'),
(276, 117, 'closure', 'CAPA Closure', 6, 'capa_post_shipment', '{"columns": 1}');

INSERT IGNORE INTO `inspection_fields` (`id`, `page_id`, `field_key`, `field_type`, `label`, `is_required`, `sort_order`, `validation_rules`, `options`, `config`) VALUES
(2700, 270, 'capa_title', 'text', 'CAPA Title', 1, 1, '{"required": true}', NULL, '{"placeholder": "Brief title describing the issue"}'),
(2701, 270, 'capa_number', 'text', 'CAPA Reference Number', 1, 2, '{"required": true}', NULL, NULL),
(2702, 270, 'initiated_by', 'text', 'Initiated By', 1, 3, '{"required": true}', NULL, NULL),
(2703, 270, 'initiated_date', 'date', 'Initiation Date', 1, 4, '{"required": true}', NULL, '{"default_value": "today"}'),
(2704, 270, 'source', 'select', 'CAPA Source', 1, 5, '{"required": true}',
  '[{"label":"Customer Complaint","value":"complaint"},{"label":"Internal Audit","value":"internal_audit"},{"label":"Supplier Audit","value":"supplier_audit"},{"label":"Inspection Failure","value":"inspection_failure"},{"label":"Non-conformance","value":"nonconformance"},{"label":"Customer Rejection","value":"customer_rejection"},{"label":"Other","value":"other"}]', NULL),
(2705, 270, 'severity', 'select', 'Severity', 1, 6, '{"required": true}',
  '[{"label":"Critical","value":"critical"},{"label":"Major","value":"major"},{"label":"Minor","value":"minor"}]', NULL),
(2706, 270, 'related_po', 'text', 'Related PO Number', 0, 7, NULL, NULL, NULL),
(2707, 270, 'related_style', 'text', 'Related Style Number', 0, 8, NULL, NULL, NULL),
(2708, 271, 'problem_description', 'textarea', 'Problem Description', 1, 1, '{"required": true}', NULL, '{"rows": 5, "placeholder": "Detailed description of the problem"}'),
(2709, 271, 'problem_scope', 'select', 'Problem Scope', 1, 2, '{"required": true}',
  '[{"label":"One-time incident","value":"onetime"},{"label":"Recurring issue","value":"recurring"},{"label":"Systemic / process issue","value":"systemic"}]', NULL),
(2710, 271, 'affected_quantity', 'number', 'Affected Quantity (pcs)', 0, 3, '{"min": 0}', NULL, NULL),
(2711, 271, 'defect_grid', 'defect_grid', 'Related Defects', 0, 4, NULL, NULL,
  '{"show_photo_button": true, "defect_sources": ["master_defect_library"]}'),
(2712, 272, 'root_cause_method', 'select', 'Root Cause Method Used', 1, 1, '{"required": true}',
  '[{"label":"5 Whys","value":"5whys"},{"label":"Fishbone / Ishikawa","value":"fishbone"},{"label":"FMEA","value":"fmea"},{"label":"Cause & Effect","value":"cause_effect"},{"label":"Other","value":"other"}]', NULL),
(2713, 272, 'root_cause_category', 'select', 'Root Cause Category', 1, 2, '{"required": true}',
  '[{"label":"Material","value":"material"},{"label":"Method / Process","value":"method"},{"label":"Machine / Equipment","value":"machine"},{"label":"Manpower / Training","value":"manpower"},{"label":"Measurement","value":"measurement"},{"label":"Environment","value":"environment"}]', NULL),
(2714, 272, 'root_cause_description', 'textarea', 'Root Cause Description', 1, 3, '{"required": true}', NULL, '{"rows": 4}'),
(2715, 273, 'corrective_action', 'textarea', 'Immediate Corrective Action Taken', 1, 1, '{"required": true}', NULL, '{"rows": 4, "placeholder": "What was done to fix the immediate issue?"}'),
(2716, 273, 'preventive_action', 'textarea', 'Preventive Action Plan', 1, 2, '{"required": true}', NULL, '{"rows": 4, "placeholder": "What will prevent recurrence?"}'),
(2717, 273, 'action_owner', 'text', 'Action Owner (Responsible)', 1, 3, '{"required": true}', NULL, NULL),
(2718, 273, 'action_deadline', 'date', 'Action Deadline', 1, 4, '{"required": true}', NULL, NULL),
(2719, 274, 'verification_method', 'select', 'Verification Method', 1, 1, '{"required": true}',
  '[{"label":"Re-inspection","value":"reinspection"},{"label":"Process Audit","value":"process_audit"},{"label":"Documentation Review","value":"doc_review"},{"label":"Customer Feedback","value":"customer_feedback"},{"label":"Testing","value":"testing"},{"label":"Other","value":"other"}]', NULL),
(2720, 274, 'verification_criteria', 'textarea', 'Verification Criteria', 1, 2, '{"required": true}', NULL, '{"rows": 3, "placeholder": "How will we verify effectiveness?"}'),
(2721, 274, 'verifier_name', 'text', 'Verifier Name', 1, 3, '{"required": true}', NULL, NULL),
(2722, 274, 'verification_deadline', 'date', 'Verification Deadline', 1, 4, '{"required": true}', NULL, NULL),
(2723, 275, 'capa_photos', 'photo', 'CAPA Evidence Photos', 0, 1, '{"max_files": 8}', NULL, NULL),
(2724, 276, 'effectiveness_result', 'select', 'Effectiveness Check Result', 1, 1, '{"required": true}',
  '[{"label":"Effective - Can Close","value":"effective"},{"label":"Partially Effective - Extended","value":"partial"},{"label":"Not Effective - New CAPA Needed","value":"ineffective"}]', NULL),
(2725, 276, 'closure_notes', 'textarea', 'Closure Notes', 1, 2, '{"required": true}', NULL, '{"rows": 4}'),
(2726, 276, 'closure_date', 'date', 'Closure Date', 0, 3, NULL, NULL, NULL);

-- ========================================================================
-- ADDITIONAL MASTER DEFECTS FOR NEW STAGES
-- ========================================================================
INSERT IGNORE INTO `master_defect_library` (`id`, `category`, `code`, `description`, `defect_type`, `inspection_type`, `photo_required`, `auto_capa`) VALUES

-- Fabric defects (4-point system)
(100, 'major', 'FAB-01', 'Hole > 1cm', 'fabric', 'fabric_4point', 1, 0),
(101, 'major', 'FAB-02', 'Slub / yarn irregularity > 3cm', 'fabric', 'fabric_4point', 1, 0),
(102, 'minor', 'FAB-03', 'Nep / knot', 'fabric', 'fabric_4point', 0, 0),
(103, 'major', 'FAB-04', 'Color shading / variation across width', 'fabric', 'fabric_4point', 1, 0),
(104, 'major', 'FAB-05', 'Bowing / skewing > 3%', 'fabric', 'fabric_4point', 0, 0),
(105, 'major', 'FAB-06', 'Selvedge defect', 'fabric', 'fabric_4point', 0, 0),
(106, 'major', 'FAB-07', 'Stain / contamination', 'fabric', 'fabric_4point', 1, 0),
(107, 'critical', 'FAB-08', 'Wrong fabric delivered', 'fabric', 'fabric_4point', 1, 1),

-- Cutting defects
(108, 'major', 'CUT-01', 'Notch cut too deep', 'cutting', 'cutting', 0, 0),
(109, 'major', 'CUT-02', 'Fused edge / heat damage', 'cutting', 'cutting', 1, 0),
(110, 'major', 'CUT-03', 'Panel misalignment', 'cutting', 'cutting', 1, 0),
(111, 'minor', 'CUT-04', 'Rough edge', 'cutting', 'cutting', 0, 0),
(112, 'major', 'CUT-05', 'Wrong size cut', 'cutting', 'cutting', 1, 1),

-- Print defects
(113, 'major', 'PRN-01', 'Misregistration / off-register', 'printing', 'printing', 1, 0),
(114, 'major', 'PRN-02', 'Color wrong / off-shade', 'printing', 'printing', 1, 0),
(115, 'major', 'PRN-03', 'Ink bleed / fuzzy edges', 'printing', 'printing', 1, 0),
(116, 'minor', 'PRN-04', 'Pin holes in print', 'printing', 'printing', 0, 0),
(117, 'major', 'PRN-05', 'Print placement incorrect', 'printing', 'printing', 1, 0),
(118, 'minor', 'PRN-06', 'Ink stain outside print area', 'printing', 'printing', 1, 0),

-- Embroidery defects
(119, 'major', 'EMB-01', 'Broken / missing stitches', 'embroidery', 'embroidery', 1, 0),
(120, 'major', 'EMB-02', 'Design distortion', 'embroidery', 'embroidery', 1, 0),
(121, 'minor', 'EMB-03', 'Loose thread / jump stitch', 'embroidery', 'embroidery', 0, 0),
(122, 'major', 'EMB-04', 'Wrong thread color', 'embroidery', 'embroidery', 1, 0),
(123, 'minor', 'EMB-05', 'Backing not removed', 'embroidery', 'embroidery', 0, 0),
(124, 'major', 'EMB-06', 'Puckering around embroidery', 'embroidery', 'embroidery', 1, 0),

-- Washing defects
(125, 'major', 'WSH-01', 'Excessive shrinkage > spec', 'washing', 'washing', 0, 0),
(126, 'major', 'WSH-02', 'Color fading > spec', 'washing', 'washing', 1, 0),
(127, 'major', 'WSH-03', 'Color staining / bleeding', 'washing', 'washing', 1, 1),
(128, 'minor', 'WSH-04', 'Slight twisting / spirality', 'washing', 'washing', 0, 0),
(129, 'major', 'WSH-05', 'Hand feel too harsh / stiff', 'washing', 'washing', 0, 0),

-- Finishing defects
(130, 'minor', 'FIN-01', 'Loose thread ends > 2cm', 'finishing', 'finishing', 0, 0),
(131, 'major', 'FIN-02', 'Stain not removed', 'finishing', 'finishing', 1, 0),
(132, 'major', 'FIN-03', 'Crease / wrinkle not removable', 'finishing', 'finishing', 1, 0),
(133, 'major', 'FIN-04', 'Shininess / glazing from iron', 'finishing', 'finishing', 1, 0),
(134, 'minor', 'FIN-05', 'Uneven folding / packing', 'finishing', 'finishing', 0, 0),

-- Inline sewing defects
(135, 'major', 'INL-01', 'Open seam', 'sewing', 'sewing_inline', 1, 0),
(136, 'major', 'INL-02', 'Skipped stitches > 2cm', 'sewing', 'sewing_inline', 1, 0),
(137, 'minor', 'INL-03', 'Uneven stitch length', 'sewing', 'sewing_inline', 0, 0),
(138, 'minor', 'INL-04', 'Pleating / tucking', 'sewing', 'sewing_inline', 0, 0),
(139, 'major', 'INL-05', 'Broken needle in garment', 'sewing', 'sewing_inline', 1, 1),
(140, 'major', 'INL-06', 'Wrong thread used', 'sewing', 'sewing_inline', 1, 0),

-- Needle detection
(141, 'critical', 'NDL-01', 'Broken needle found in garment', 'needle', 'needle_detection', 1, 1),
(142, 'major', 'NDL-02', 'Metal fragment detected', 'needle', 'needle_detection', 1, 1),
(143, 'major', 'NDL-03', 'Machine alarm not investigated', 'needle', 'needle_detection', 0, 0),

-- Packing defects
(144, 'minor', 'PKG-01', 'Polybag torn / damaged', 'packaging', 'packing', 1, 0),
(145, 'major', 'PKG-02', 'Wrong polybag size', 'packaging', 'packing', 1, 0),
(146, 'major', 'PKG-03', 'Carton damaged', 'packaging', 'packing', 1, 0),
(147, 'major', 'PKG-04', 'Wrong quantity in carton', 'packaging', 'packing', 1, 1),
(148, 'major', 'PKG-05', 'Mixed sizes in wrong carton', 'packaging', 'packing', 1, 0),

-- Container loading
(149, 'critical', 'CLI-01', 'Container dirty / contaminated', 'loading', 'container_loading', 1, 1),
(150, 'major', 'CLI-02', 'Wrong seal number', 'loading', 'container_loading', 1, 1),
(151, 'major', 'CLI-03', 'Carton count mismatch', 'loading', 'container_loading', 1, 0),
(152, 'major', 'CLI-04', 'Container damaged', 'loading', 'container_loading', 1, 1);

-- ========================================================================
-- MEASUREMENT TEMPLATES FOR CUTTING
-- ========================================================================
INSERT IGNORE INTO `measurement_templates` (`id`, `inspection_type`, `point_name`, `tolerance_min`, `tolerance_max`, `unit`, `sort_order`, `category`) VALUES
(20, 'cutting', 'Cut Panel Length', -0.50, 0.50, 'cm', 1, 'cutting'),
(21, 'cutting', 'Cut Panel Width', -0.50, 0.50, 'cm', 2, 'cutting'),
(22, 'cutting', 'Notch Position', -0.30, 0.30, 'cm', 3, 'cutting');

-- ========================================================================
-- SAMPLE INSPECTION RULES FOR NEW TEMPLATES
-- ========================================================================
INSERT IGNORE INTO `inspection_rules` (`id`, `template_id`, `priority`, `condition_expression`, `actions`, `label`, `is_active`) VALUES
(100, 100, 1, 'audit_scoring.overall_score < 60', '["set_status:FAIL", "set_decision:rejected", "trigger_CAPA"]', 'Low audit score auto-reject', 1),
(101, 100, 2, 'audit_scoring.overall_score >= 80', '["set_status:PASS", "set_decision:approved"]', 'High audit score auto-approve', 1),
(102, 101, 1, 'fabric_summary.failed_rolls > 0', '["set_status:FAIL", "set_decision:reject", "trigger_CAPA"]', 'Any failed roll triggers review', 1),
(103, 109, 1, 'sewing_inline_summary.red_count > 2', '["set_status:FAIL", "trigger_CAPA", "set_decision:FAIL"]', 'Multiple red hours require CAPA', 1),
(104, 113, 1, 'needle_detection_summary.confirmed_needles > 0', '["set_status:FAIL", "trigger_CAPA", "set_decision:FAIL"]', 'Broken needle found', 1),
(105, 116, 1, 'container_loading_summary.container_condition = "major"', '["set_status:FAIL", "set_decision:FAIL"]', 'Container damaged', 1);

SET FOREIGN_KEY_CHECKS = 1;
