-- ============================================================
-- FCQC - TNA Module Seed Data
-- ============================================================
-- Creates templates, team members, projects, and tasks
-- based on existing master_buyer_pos data
-- ============================================================

-- -----------------------------------------------------------
-- 1. Team Members (now stored in users table)
-- -----------------------------------------------------------
-- NOTE: Team members were migrated from tna_team_members to users table.
-- See migrate_team_to_users.php for the migration.
-- Department/designation/manager/factory fields are now columns in users.

-- -----------------------------------------------------------
-- 2. TNA Templates
-- -----------------------------------------------------------
INSERT IGNORE INTO `tna_templates` (`name`, `description`, `duration_days`, `task_definitions`, `company_id`, `buyer_id`) VALUES
('Woven Garment - 60 Day Standard', 'Standard 60-day TNA template for woven garment production (jackets, pants, blazers)', 60,
'[
  {"phase":"Order Confirmation","title":"Buyer PO Received","duration_days":1,"days_offset":-60,"is_key_event":true,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Order Confirmation","duration_days":1,"days_offset":-59,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Costing Approval","duration_days":1,"days_offset":-58,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"BOM Finalization","duration_days":1,"days_offset":-57,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"TNA Creation","duration_days":1,"days_offset":-56,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Specification Sheet","duration_days":2,"days_offset":-55,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Tech Pack Review","duration_days":2,"days_offset":-53,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Proto Sample Development","duration_days":4,"days_offset":-51,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"Proto Approval","duration_days":2,"days_offset":-47,"is_key_event":true,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Fit Sample","duration_days":2,"days_offset":-45,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"Size Set Sample","duration_days":3,"days_offset":-43,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"PP Sample","duration_days":2,"days_offset":-40,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"PP Meeting","duration_days":1,"days_offset":-38,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Fabric Booking","duration_days":2,"days_offset":-55,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Trim Booking","duration_days":2,"days_offset":-53,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Lab Dips","duration_days":4,"days_offset":-50,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Bulk Fabric In-house","duration_days":1,"days_offset":-40,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Bulk Trim In-house","duration_days":1,"days_offset":-39,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Fabric Inspection","duration_days":1,"days_offset":-38,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"QA"},
  {"phase":"Production","title":"Cutting Start","duration_days":2,"days_offset":-36,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Cutting Complete","duration_days":1,"days_offset":-34,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Sewing Start","duration_days":1,"days_offset":-33,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Line Setting","duration_days":1,"days_offset":-33,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Pilot Run","duration_days":1,"days_offset":-31,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Sewing Complete","duration_days":1,"days_offset":-25,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Washing","duration_days":2,"days_offset":-24,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Finishing","duration_days":2,"days_offset":-22,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Packing","duration_days":1,"days_offset":-20,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Quality / QA","title":"Initial Audit","duration_days":1,"days_offset":-22,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"Inline QC","duration_days":2,"days_offset":-19,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"Final Inspection","duration_days":1,"days_offset":-16,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"AQL Pass","duration_days":1,"days_offset":-14,"is_key_event":true,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Logistics / Shipment","title":"Ex-Factory Date","duration_days":1,"days_offset":-12,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"Carton Dispatch","duration_days":1,"days_offset":-11,"is_key_event":false,"is_milestone":false,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"Stuffing","duration_days":1,"days_offset":-10,"is_key_event":false,"is_milestone":false,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"On Vessel Date","duration_days":1,"days_offset":0,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"}
]', 1, 1),
('Knit Garment - 45 Day Express', 'Express 45-day TNA template for knit garment production (t-shirts, shorts, hoodies)', 45,
'[
  {"phase":"Order Confirmation","title":"Buyer PO Received","duration_days":1,"days_offset":-45,"is_key_event":true,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Order Confirmation","duration_days":1,"days_offset":-44,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Costing Approval","duration_days":1,"days_offset":-43,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"BOM Finalization","duration_days":1,"days_offset":-42,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"TNA Creation","duration_days":1,"days_offset":-41,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Spec Sheet & Tech Pack","duration_days":2,"days_offset":-40,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Proto Development","duration_days":3,"days_offset":-38,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"Fit & Size Set Approval","duration_days":3,"days_offset":-35,"is_key_event":true,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"PP Sample & Meeting","duration_days":2,"days_offset":-32,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Fabric & Trim Booking","duration_days":2,"days_offset":-40,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Lab Dips / Bulk Approvals","duration_days":3,"days_offset":-38,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Bulk Fabric In-house","duration_days":1,"days_offset":-28,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Fabric Inspection","duration_days":1,"days_offset":-27,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"QA"},
  {"phase":"Production","title":"Cutting & Sewing Start","duration_days":1,"days_offset":-25,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Sewing Complete","duration_days":1,"days_offset":-15,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Finishing & Packing","duration_days":2,"days_offset":-14,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Quality / QA","title":"Inline QC","duration_days":1,"days_offset":-20,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"Final Inspection & AQL","duration_days":1,"days_offset":-10,"is_key_event":true,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Logistics / Shipment","title":"Ex-Factory","duration_days":1,"days_offset":-8,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"Container Stuffing","duration_days":1,"days_offset":-7,"is_key_event":false,"is_milestone":false,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"On Vessel Date","duration_days":1,"days_offset":0,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"}
]', 1, 3),
('Tailored Garment - 75 Day Premium', 'Premium 75-day TNA template for tailored/structured garments (blazers, coats)', 75,
'[
  {"phase":"Order Confirmation","title":"Buyer PO Received","duration_days":1,"days_offset":-75,"is_key_event":true,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Order Confirmation","duration_days":1,"days_offset":-74,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"Costing & BOM Approval","duration_days":2,"days_offset":-73,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Order Confirmation","title":"TNA Creation","duration_days":1,"days_offset":-71,"is_key_event":false,"is_milestone":false,"color":"green","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Spec Sheet & Tech Pack","duration_days":3,"days_offset":-70,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Proto Sample Development","duration_days":5,"days_offset":-67,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"Proto Approval","duration_days":3,"days_offset":-62,"is_key_event":true,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Sampling","title":"Fit Sample","duration_days":3,"days_offset":-59,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"Size Set Sample","duration_days":4,"days_offset":-56,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Factory"},
  {"phase":"Sampling","title":"PP Sample & Meeting","duration_days":3,"days_offset":-52,"is_key_event":false,"is_milestone":false,"color":"blue","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Fabric Booking","duration_days":3,"days_offset":-70,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Trim Booking","duration_days":2,"days_offset":-67,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Merchandiser"},
  {"phase":"Materials","title":"Lab Dips","duration_days":5,"days_offset":-65,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Bulk Fabric In-house","duration_days":1,"days_offset":-48,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"Factory"},
  {"phase":"Materials","title":"Fabric & Trim Inspection","duration_days":2,"days_offset":-47,"is_key_event":false,"is_milestone":false,"color":"#86efac","description":"","responsible_party":"QA"},
  {"phase":"Production","title":"Cutting Start","duration_days":3,"days_offset":-45,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Cutting Complete","duration_days":1,"days_offset":-42,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Sewing Start","duration_days":1,"days_offset":-41,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Line Setting & Pilot Run","duration_days":2,"days_offset":-40,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Sewing Complete","duration_days":1,"days_offset":-28,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Washing","duration_days":2,"days_offset":-27,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Production","title":"Finishing & Packing","duration_days":3,"days_offset":-25,"is_key_event":false,"is_milestone":false,"color":"orange","description":"","responsible_party":"Factory"},
  {"phase":"Quality / QA","title":"Initial Audit","duration_days":1,"days_offset":-30,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"Inline QC","duration_days":3,"days_offset":-22,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"Final Inspection","duration_days":2,"days_offset":-17,"is_key_event":false,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Quality / QA","title":"AQL Pass","duration_days":1,"days_offset":-15,"is_key_event":true,"is_milestone":false,"color":"red","description":"","responsible_party":"QA"},
  {"phase":"Logistics / Shipment","title":"Ex-Factory Date","duration_days":1,"days_offset":-12,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"Carton Dispatch & Stuffing","duration_days":2,"days_offset":-11,"is_key_event":false,"is_milestone":false,"color":"purple","description":"","responsible_party":"Logistics"},
  {"phase":"Logistics / Shipment","title":"On Vessel Date","duration_days":1,"days_offset":0,"is_key_event":true,"is_milestone":true,"color":"purple","description":"","responsible_party":"Logistics"}
]', 1, 2);

-- -----------------------------------------------------------
-- 3. TNA Projects
-- -----------------------------------------------------------
INSERT INTO `tna_projects` (`title`, `description`, `target_date`, `po_id`, `template_id`, `status`, `company_id`, `created_by`) VALUES
('Basic Cotton T-Shirt - H&M', 'Production TNA for H&M Basic Cotton T-Shirt PO-HM-2024-001', '2026-08-30', 1, 1, 'active', 1, 1),
('Slim Fit Chino Pants - H&M', 'Production TNA for H&M Slim Fit Chino Pants PO-HM-2024-002', '2026-09-15', 2, 1, 'active', 1, 1),
('Wool Blend Blazer - Zara', 'Production TNA for Zara Wool Blend Blazer PO-ZR-2024-001', '2026-10-30', 3, 3, 'active', 1, 1),
('Dri-FIT Running Shorts - Nike', 'Production TNA for Nike Dri-FIT Running Shorts PO-NK-2024-001', '2026-08-15', 4, 2, 'active', 1, 1),
('Ultraboost Tights - Adidas', 'Production TNA for Adidas Ultraboost Running Tights PO-AD-2024-001', '2026-09-30', 5, 2, 'active', 1, 1);

-- -----------------------------------------------------------
-- 4. TNA Tasks (generated from template JSON for each project)
-- -----------------------------------------------------------

-- Project 1: Basic Cotton T-Shirt (Template ID 1 - Woven 60 Day)
INSERT INTO `tna_tasks` (`project_id`, `title`, `description`, `phase`, `responsible_party`, `status`, `color`, `planned_start_date`, `planned_end_date`, `delay_days`, `completion_percentage`, `sort_order`, `is_key_event`, `is_milestone`, `factory`, `vendor`, `merchandiser`, `qa_responsible`, `company_id`)
SELECT 1, j.title, j.description, j.phase, j.responsible_party,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval','BOM Finalization','TNA Creation','Specification Sheet','Tech Pack Review') THEN 'completed'
    WHEN j.title IN ('Fabric Booking','Trim Booking') THEN 'in_progress'
    WHEN j.title IN ('Proto Sample Development','Fit Sample') THEN 'in_progress'
    ELSE 'pending'
  END,
  j.color,
  DATE_ADD(CURDATE(), INTERVAL j.days_offset DAY),
  DATE_ADD(CURDATE(), INTERVAL (j.days_offset + j.duration_days) DAY),
  CASE WHEN j.title IN ('Proto Sample Development','Fit Sample') THEN 2 ELSE 0 END,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval','BOM Finalization','TNA Creation','Specification Sheet','Tech Pack Review') THEN 100
    WHEN j.title IN ('Fabric Booking','Trim Booking') THEN 60
    ELSE 0
  END,
  @rn := @rn + 1, j.is_key_event, j.is_milestone,
  'Dhaka Garments Ltd', '', 'Fatima Begum', 'Nasrin Akter', 1
FROM tna_templates t, JSON_TABLE(t.task_definitions, '$[*]' COLUMNS (
  phase VARCHAR(100) PATH '$.phase', title VARCHAR(255) PATH '$.title',
  description TEXT PATH '$.description', responsible_party VARCHAR(255) PATH '$.responsible_party',
  color VARCHAR(20) PATH '$.color', days_offset INT PATH '$.days_offset',
  duration_days INT PATH '$.duration_days',
  is_key_event BOOLEAN PATH '$.is_key_event', is_milestone BOOLEAN PATH '$.is_milestone'
)) j, (SELECT @rn := 0) r WHERE t.id = 1;

-- Project 2: Slim Fit Chino Pants (Template ID 1 - Woven 60 Day)
INSERT INTO `tna_tasks` (`project_id`, `title`, `description`, `phase`, `responsible_party`, `status`, `color`, `planned_start_date`, `planned_end_date`, `delay_days`, `completion_percentage`, `sort_order`, `is_key_event`, `is_milestone`, `factory`, `vendor`, `merchandiser`, `qa_responsible`, `company_id`)
SELECT 2, j.title, j.description, j.phase, j.responsible_party,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval') THEN 'completed'
    WHEN j.title IN ('BOM Finalization','TNA Creation') THEN 'in_progress'
    ELSE 'pending'
  END,
  j.color,
  DATE_ADD(CURDATE(), INTERVAL j.days_offset DAY),
  DATE_ADD(CURDATE(), INTERVAL (j.days_offset + j.duration_days) DAY),
  0,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval') THEN 100
    WHEN j.title IN ('BOM Finalization','TNA Creation') THEN 40
    ELSE 0
  END,
  @rn2 := @rn2 + 1, j.is_key_event, j.is_milestone,
  'Chattogram Export Processing Zone', '', 'Shahidul Islam', 'Nasrin Akter', 1
FROM tna_templates t, JSON_TABLE(t.task_definitions, '$[*]' COLUMNS (
  phase VARCHAR(100) PATH '$.phase', title VARCHAR(255) PATH '$.title',
  description TEXT PATH '$.description', responsible_party VARCHAR(255) PATH '$.responsible_party',
  color VARCHAR(20) PATH '$.color', days_offset INT PATH '$.days_offset',
  duration_days INT PATH '$.duration_days',
  is_key_event BOOLEAN PATH '$.is_key_event', is_milestone BOOLEAN PATH '$.is_milestone'
)) j, (SELECT @rn2 := 0) r WHERE t.id = 1;

-- Project 3: Wool Blend Blazer (Template ID 3 - Tailored 75 Day)
INSERT INTO `tna_tasks` (`project_id`, `title`, `description`, `phase`, `responsible_party`, `status`, `color`, `planned_start_date`, `planned_end_date`, `delay_days`, `completion_percentage`, `sort_order`, `is_key_event`, `is_milestone`, `factory`, `vendor`, `merchandiser`, `qa_responsible`, `company_id`)
SELECT 3, j.title, j.description, j.phase, j.responsible_party, 'pending', j.color,
  DATE_ADD(CURDATE(), INTERVAL j.days_offset DAY),
  DATE_ADD(CURDATE(), INTERVAL (j.days_offset + j.duration_days) DAY),
  0, 0,
  @rn3 := @rn3 + 1, j.is_key_event, j.is_milestone,
  'Knit Concern Ltd', '', 'Nusrat Jahan', 'Nasrin Akter', 1
FROM tna_templates t, JSON_TABLE(t.task_definitions, '$[*]' COLUMNS (
  phase VARCHAR(100) PATH '$.phase', title VARCHAR(255) PATH '$.title',
  description TEXT PATH '$.description', responsible_party VARCHAR(255) PATH '$.responsible_party',
  color VARCHAR(20) PATH '$.color', days_offset INT PATH '$.days_offset',
  duration_days INT PATH '$.duration_days',
  is_key_event BOOLEAN PATH '$.is_key_event', is_milestone BOOLEAN PATH '$.is_milestone'
)) j, (SELECT @rn3 := 0) r WHERE t.id = 3;

-- Project 4: Dri-FIT Running Shorts (Template ID 2 - Knit 45 Day)
INSERT INTO `tna_tasks` (`project_id`, `title`, `description`, `phase`, `responsible_party`, `status`, `color`, `planned_start_date`, `planned_end_date`, `delay_days`, `completion_percentage`, `sort_order`, `is_key_event`, `is_milestone`, `factory`, `vendor`, `merchandiser`, `qa_responsible`, `company_id`)
SELECT 4, j.title, j.description, j.phase, j.responsible_party,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval','BOM Finalization','TNA Creation','Spec Sheet & Tech Pack') THEN 'completed'
    WHEN j.title IN ('Proto Development','Fabric & Trim Booking') THEN 'in_progress'
    ELSE 'pending'
  END,
  j.color,
  DATE_ADD(CURDATE(), INTERVAL j.days_offset DAY),
  DATE_ADD(CURDATE(), INTERVAL (j.days_offset + j.duration_days) DAY),
  CASE WHEN j.title = 'Proto Development' THEN 1 ELSE 0 END,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation','Costing Approval','BOM Finalization','TNA Creation','Spec Sheet & Tech Pack') THEN 100
    WHEN j.title IN ('Proto Development','Fabric & Trim Booking') THEN 50
    ELSE 0
  END,
  @rn4 := @rn4 + 1, j.is_key_event, j.is_milestone,
  'Dhaka Garments Ltd', '', 'Rafiq Hasan', 'Nasrin Akter', 1
FROM tna_templates t, JSON_TABLE(t.task_definitions, '$[*]' COLUMNS (
  phase VARCHAR(100) PATH '$.phase', title VARCHAR(255) PATH '$.title',
  description TEXT PATH '$.description', responsible_party VARCHAR(255) PATH '$.responsible_party',
  color VARCHAR(20) PATH '$.color', days_offset INT PATH '$.days_offset',
  duration_days INT PATH '$.duration_days',
  is_key_event BOOLEAN PATH '$.is_key_event', is_milestone BOOLEAN PATH '$.is_milestone'
)) j, (SELECT @rn4 := 0) r WHERE t.id = 2;

-- Project 5: Ultraboost Tights (Template ID 2 - Knit 45 Day)
INSERT INTO `tna_tasks` (`project_id`, `title`, `description`, `phase`, `responsible_party`, `status`, `color`, `planned_start_date`, `planned_end_date`, `delay_days`, `completion_percentage`, `sort_order`, `is_key_event`, `is_milestone`, `factory`, `vendor`, `merchandiser`, `qa_responsible`, `company_id`)
SELECT 5, j.title, j.description, j.phase, j.responsible_party,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation') THEN 'completed'
    WHEN j.title = 'Costing Approval' THEN 'in_progress'
    ELSE 'pending'
  END,
  j.color,
  DATE_ADD(CURDATE(), INTERVAL j.days_offset DAY),
  DATE_ADD(CURDATE(), INTERVAL (j.days_offset + j.duration_days) DAY),
  0,
  CASE
    WHEN j.title IN ('Buyer PO Received','Order Confirmation') THEN 100
    WHEN j.title = 'Costing Approval' THEN 30
    ELSE 0
  END,
  @rn5 := @rn5 + 1, j.is_key_event, j.is_milestone,
  'PackRight Garments', '', 'Rafiq Hasan', 'Nasrin Akter', 1
FROM tna_templates t, JSON_TABLE(t.task_definitions, '$[*]' COLUMNS (
  phase VARCHAR(100) PATH '$.phase', title VARCHAR(255) PATH '$.title',
  description TEXT PATH '$.description', responsible_party VARCHAR(255) PATH '$.responsible_party',
  color VARCHAR(20) PATH '$.color', days_offset INT PATH '$.days_offset',
  duration_days INT PATH '$.duration_days',
  is_key_event BOOLEAN PATH '$.is_key_event', is_milestone BOOLEAN PATH '$.is_milestone'
)) j, (SELECT @rn5 := 0) r WHERE t.id = 2;

-- -----------------------------------------------------------
-- 5. Notifications
-- -----------------------------------------------------------
INSERT INTO `tna_notifications` (`user_id`, `title`, `message`, `type`, `priority`, `related_project_id`, `is_read`, `created_at`) VALUES
(1, 'TNA Created: Basic Cotton T-Shirt', 'TNA project for H&M Basic Cotton T-Shirt has been created and tasks are being scheduled.', 'project_created', 'medium', 1, 0, NOW()),
(1, 'Task Completed: Buyer PO Received', 'Buyer PO Received task has been marked completed for Basic Cotton T-Shirt project.', 'task_update', 'low', 1, 0, NOW()),
(1, 'Delay Alert: Proto Sample Development', 'Proto Sample Development for Basic Cotton T-Shirt is 2 days behind schedule.', 'delay_alert', 'high', 1, 0, NOW()),
(1, 'TNA Created: Dri-FIT Running Shorts', 'TNA project for Nike Dri-FIT Running Shorts has been created.', 'project_created', 'medium', 4, 0, NOW()),
(1, 'TNA Created: Ultraboost Tights', 'TNA project for Adidas Ultraboost Running Tights has been created.', 'project_created', 'medium', 5, 0, NOW());

-- -----------------------------------------------------------
-- 6. Project Team assignments
-- -----------------------------------------------------------
INSERT IGNORE INTO `tna_project_team` (`project_id`, `user_id`, `role`) VALUES
(1, 1, 'admin'),
(4, 1, 'admin'),
(5, 1, 'admin');
