-- ============================================================
-- Operation Templates — reusable sets of operations (+ machines)
-- applied to a Style's Operations tab to reduce manual entry
-- ============================================================

SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS `operation_templates` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `company_id` int unsigned NOT NULL DEFAULT 1,
  `template_code` varchar(50) DEFAULT NULL COMMENT 'Optional code e.g. OPR-WOV-SHIRT',
  `template_name` varchar(150) NOT NULL,
  `description` text,
  `status` enum('draft','active','archived') NOT NULL DEFAULT 'active',
  `total_smv` decimal(10,2) DEFAULT NULL,
  `created_by` int unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `company_id` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `operation_template_ops` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `company_id` int unsigned NOT NULL DEFAULT 1,
  `template_id` int unsigned NOT NULL,
  `sequence` int unsigned NOT NULL DEFAULT 0,
  `operation_name` varchar(255) NOT NULL,
  `machine_type` varchar(100) DEFAULT NULL,
  `operation_type` enum('cutting','sewing','finishing','packing','quality','other') NOT NULL DEFAULT 'sewing',
  `difficulty` enum('easy','medium','hard') NOT NULL DEFAULT 'medium',
  `smv` decimal(8,2) NOT NULL DEFAULT 0.00,
  `sam` decimal(8,2) DEFAULT NULL,
  `notes` text,
  `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `company_id` (`company_id`),
  KEY `template_id` (`template_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

SET FOREIGN_KEY_CHECKS = 1;
