-- ============================================================
-- FashCognitive Apparel MES - Demo Seed Data
-- Master Production Orders + Planning Board (Level 1: Planning/APS)
--
-- One-time seed for company_id = 1. Uses explicit IDs so it is
-- safe to inspect; re-running will collide on primary keys.
-- Live DB only (seed data is not part of production_schema.sql).
-- ============================================================

-- ── Master Production Orders ──────────────────────────────
INSERT INTO master_production_orders
  (id, company_id, mpo_no, buyer_po_id, style_id, color, size, total_qty, uom, priority,
   planned_start_date, planned_end_date, status, notes, created_by, approved_by, approved_at, released_at)
VALUES
  (1, 1, 'MPO-2026-000001', 1, 1, 'Black', '32', 4000, 'PCS', 'high',
   '2026-07-20', '2026-08-12', 'released', 'Split across Sewing Line A for PO-HM-2024-001', 1, 1, NOW(), NOW()),
  (2, 1, 'MPO-2026-000002', 1, 1, 'Black', '34', 4000, 'PCS', 'medium',
   '2026-08-03', '2026-08-06', 'released', 'Split across Sewing Line B for PO-HM-2024-001', 1, 1, NOW(), NOW()),
  (3, 1, 'MPO-2026-000003', 2, 2, 'Navy', '34', 6000, 'PCS', 'medium',
   '2026-08-10', '2026-09-05', 'planned', 'Planned MPO for Slim Fit Chino Pants awaiting release', 1, NULL, NULL, NULL);

-- ── MPO split lines ──────────────────────────────────────
INSERT INTO master_production_order_lines
  (id, company_id, mpo_id, unit_id, line_id, allocated_qty, split_pct,
   planned_start_date, planned_end_date, status, production_order_id)
VALUES
  (1, 1, 1, 1, 1, 4000, 100.00, '2026-07-20', '2026-08-12', 'released', 2),
  (2, 1, 2, 1, 2, 4000, 100.00, '2026-08-03', '2026-08-06', 'released', 3),
  (3, 1, 3, 1, 1, 3000,  50.00, '2026-08-10', '2026-09-05', 'planned', NULL),
  (4, 1, 3, 1, 2, 3000,  50.00, '2026-08-10', '2026-09-05', 'planned', NULL);

-- Back-link the already-running production orders to their MPOs
UPDATE production_orders SET mpo_id = 1 WHERE id = 2;
UPDATE production_orders SET mpo_id = 2 WHERE id = 3;

-- ── Production shifts ────────────────────────────────────
INSERT INTO production_shifts
  (id, company_id, unit_id, line_id, shift_name, start_time, end_time, planned_operators, status)
VALUES
  (1, 1, 1, 1, 'Day Shift A', '08:00:00', '17:00:00', 25, 'active'),
  (2, 1, 1, 1, 'Night Shift A', '20:00:00', '05:00:00', 15, 'active'),
  (3, 1, 1, 2, 'Day Shift B', '08:00:00', '17:00:00', 20, 'active'),
  (4, 1, 1, 3, 'Cutting Day', '08:00:00', '17:00:00', 10, 'active');

-- ── Production machines ──────────────────────────────────
INSERT INTO production_machines
  (id, company_id, unit_id, line_id, work_center_id, machine_code, machine_name, machine_type, status, hourly_rate)
VALUES
  (1, 1, 1, 1, 3, 'MCH-0001', 'Overlock 5-Thread Juki #1', 'Overlock', 'in_use', 2.50),
  (2, 1, 1, 1, 4, 'MCH-0002', 'Overlock 5-Thread Juki #2', 'Overlock', 'in_use', 2.50),
  (3, 1, 1, 1, 5, 'MCH-0003', 'Coverstitch Juki #1', 'Coverstitch', 'in_use', 3.00),
  (4, 1, 1, 1, 6, 'MCH-0004', 'Coverstitch Juki #2', 'Coverstitch', 'available', 3.00),
  (5, 1, 1, 1, 7, 'MCH-0005', 'Single Needle Juki #1', 'Single Needle', 'in_use', 2.00),
  (6, 1, 1, 1, 7, 'MCH-0006', 'Single Needle Juki #2', 'Single Needle', 'in_use', 2.00),
  (7, 1, 1, 2, 8, 'MCH-0011', 'Overlock 5-Thread Line B #1', 'Overlock', 'in_use', 2.50),
  (8, 1, 1, 2, 8, 'MCH-0012', 'Overlock 5-Thread Line B #2', 'Overlock', 'available', 2.50),
  (9, 1, 1, 2, 7, 'MCH-0013', 'Single Needle Line B #1', 'Single Needle', 'in_use', 2.00),
  (10, 1, 1, 2, 6, 'MCH-0014', 'Coverstitch Line B', 'Coverstitch', 'in_use', 3.00),
  (11, 1, 1, 3, 1, 'MCH-0021', 'Auto Spreader SM-200', 'Cutting', 'in_use', 4.00),
  (12, 1, 1, 3, 2, 'MCH-0022', 'Straight Knife LK-3', 'Cutting', 'in_use', 3.50),
  (13, 1, 1, 5, 10, 'MCH-0031', 'Jet Dyeing Machine 2T', 'Dyeing', 'in_use', 12.00),
  (14, 1, 1, 5, 11, 'MCH-0032', 'Jet Dyeing Machine 1T', 'Dyeing', 'available', 9.00);

-- ── Operator assignments (order 2 on Line A, order 3 on Line B) ──
INSERT INTO production_operator_assignments
  (id, company_id, prod_order_id, operation_id, operator_id, operator_name, line_id, shift_id, machine_id,
   assigned_date, start_time, end_time, role, status)
VALUES
  (1, 1, 2, 17, 3,  'Rafiq Hasan',   1, 1, 1, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'in_progress'),
  (2, 1, 2, 18, 6,  'Nasrin Akter',  1, 1, 3, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'in_progress'),
  (3, 1, 2, 19, 10, 'Operator R10',  1, 1, 5, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'in_progress'),
  (4, 1, 2, 20, 11, 'Operator R11',  1, 1, 2, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'assigned'),
  (5, 1, 2, 21, 12, 'Operator R12',  1, 1, 6, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'assigned'),
  (6, 1, 3, 30, 3,  'Rafiq Hasan',   2, 3, 7, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'in_progress'),
  (7, 1, 3, 31, 6,  'Nasrin Akter',  2, 3, 9, '2026-08-01', '08:00:00', '17:00:00', 'sewing', 'in_progress');

-- ── Capacity plans for order 2 on Line A (week of 2026-08-01) ──
INSERT INTO production_capacity_plans
  (id, company_id, plan_date, unit_id, line_id, production_order_id, operation_id, planned_pcs, planned_hours, load_pct)
VALUES
  (9,  1, '2026-08-01', 1, 1, 2, NULL, 800, 70.00, 66.67),
  (10, 1, '2026-08-02', 1, 1, 2, NULL, 800, 70.00, 66.67),
  (11, 1, '2026-08-03', 1, 1, 2, NULL, 800, 70.00, 66.67),
  (12, 1, '2026-08-04', 1, 1, 2, NULL, 800, 70.00, 66.67),
  (13, 1, '2026-08-05', 1, 1, 2, NULL, 800, 70.00, 66.67);
