-- Phase 3: Production Plan Collaboration + Batch Inbound

-- ==================== Production Plan ====================

CREATE TABLE IF NOT EXISTS `pro_production_plan` (
    `id`                  BIGINT NOT NULL AUTO_INCREMENT,
    `plan_id`             VARCHAR(32) NOT NULL,
    `tenant_id`           VARCHAR(32) NOT NULL DEFAULT '',
    `plan_code`           VARCHAR(32) NOT NULL COMMENT 'PP-YYYYMMDD-XXXX',
    `product_id`          VARCHAR(32) NOT NULL,
    `product_name`        VARCHAR(100) NOT NULL DEFAULT '',
    `color`               VARCHAR(50) NOT NULL DEFAULT '',
    `fabric_code`         VARCHAR(50) NOT NULL DEFAULT '',
    `color_code`          VARCHAR(20) NOT NULL DEFAULT '',
    `target_quantity`     DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT 'target meters',
    `completed_quantity`  DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT 'completed meters = SUM(inbound)',
    `yarn_usage_per_meter` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT 'grams per meter',
    `production_price`    DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT 'yuan per meter',
    `supplier_id`         VARCHAR(32) NOT NULL DEFAULT '' COMMENT 'pur_supplier.supplier_id as textile factory',
    `status`              TINYINT NOT NULL DEFAULT 0 COMMENT '0:draft 1:pending 2:producing 3:completed 4:cancelled',
    `remark`              TEXT,
    `start_time`          DATETIME DEFAULT NULL,
    `creator`             VARCHAR(32) NOT NULL DEFAULT '',
    `operator`            VARCHAR(32) NOT NULL DEFAULT '',
    `created_at`          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`          DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_plan_id` (`plan_id`),
    UNIQUE KEY `uk_plan_code_tenant` (`plan_code`, `tenant_id`),
    KEY `idx_tenant_status` (`tenant_id`, `status`),
    KEY `idx_product` (`product_id`),
    KEY `idx_supplier` (`supplier_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='生产计划';

-- ==================== Yarn Ratio ====================

CREATE TABLE IF NOT EXISTS `pro_plan_yarn_ratio` (
    `id`               BIGINT NOT NULL AUTO_INCREMENT,
    `ratio_id`         VARCHAR(32) NOT NULL,
    `plan_id`          VARCHAR(32) NOT NULL,
    `yarn_name`        VARCHAR(100) NOT NULL DEFAULT '',
    `ratio`            DECIMAL(5,2) NOT NULL DEFAULT 0.00 COMMENT '0.00-1.00',
    `amount_per_meter` DECIMAL(10,2) NOT NULL DEFAULT 0.00 COMMENT 'grams',
    `total_amount`     DECIMAL(14,2) NOT NULL DEFAULT 0.00 COMMENT 'grams',
    `created_at`       DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_ratio_id` (`ratio_id`),
    KEY `idx_plan` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='纱线配比';

-- ==================== Process Step ====================

CREATE TABLE IF NOT EXISTS `pro_plan_process_step` (
    `id`           BIGINT NOT NULL AUTO_INCREMENT,
    `step_id`      VARCHAR(32) NOT NULL,
    `plan_id`      VARCHAR(32) NOT NULL,
    `step_type`    VARCHAR(30) NOT NULL COMMENT 'confirm|yarn_purchase|dyeing|machine_start|fabric_warehouse',
    `step_order`   TINYINT NOT NULL DEFAULT 0,
    `status`       TINYINT NOT NULL DEFAULT 0 COMMENT '0:pending 1:in_progress 2:completed',
    `operator_id`  VARCHAR(32) NOT NULL DEFAULT '',
    `completed_at` DATETIME DEFAULT NULL,
    `remark`       TEXT,
    `created_at`   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`   DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_step_id` (`step_id`),
    UNIQUE KEY `uk_plan_step` (`plan_id`, `step_type`),
    KEY `idx_plan` (`plan_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='生产流程节点';

-- ==================== Share Link ====================

CREATE TABLE IF NOT EXISTS `pro_share_link` (
    `id`             BIGINT NOT NULL AUTO_INCREMENT,
    `link_id`        VARCHAR(32) NOT NULL,
    `tenant_id`      VARCHAR(32) NOT NULL DEFAULT '',
    `plan_id`        VARCHAR(32) NOT NULL,
    `short_code`     VARCHAR(16) NOT NULL,
    `token`          VARCHAR(512) NOT NULL DEFAULT '',
    `factory_type`   VARCHAR(20) NOT NULL DEFAULT 'textile',
    `hide_sensitive` TINYINT NOT NULL DEFAULT 0,
    `status`         TINYINT NOT NULL DEFAULT 0 COMMENT '0:pending 1:clicked 2:confirmed 3:rejected 4:expired',
    `click_count`    INT NOT NULL DEFAULT 0,
    `clicked_at`     DATETIME DEFAULT NULL,
    `responded_at`   DATETIME DEFAULT NULL,
    `reject_reason`  VARCHAR(500) NOT NULL DEFAULT '',
    `expires_at`     DATETIME NOT NULL,
    `created_by`     VARCHAR(32) NOT NULL DEFAULT '',
    `created_at`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`     DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_link_id` (`link_id`),
    UNIQUE KEY `uk_short_code` (`short_code`),
    KEY `idx_plan` (`plan_id`),
    KEY `idx_tenant` (`tenant_id`),
    KEY `idx_status_expires` (`status`, `expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='分享链接';

-- ==================== Plan Factory ====================

CREATE TABLE IF NOT EXISTS `pro_plan_factory` (
    `id`                BIGINT NOT NULL AUTO_INCREMENT,
    `plan_id`           VARCHAR(32) NOT NULL,
    `factory_tenant_id` VARCHAR(32) NOT NULL,
    `factory_type`      VARCHAR(20) NOT NULL DEFAULT 'textile',
    `share_link_id`     VARCHAR(32) NOT NULL DEFAULT '',
    `created_at`        DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_plan_factory` (`plan_id`, `factory_tenant_id`),
    KEY `idx_factory` (`factory_tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='计划-工厂关联';

-- ==================== Inbound Record ====================

CREATE TABLE IF NOT EXISTS `pro_inbound_record` (
    `id`              BIGINT NOT NULL AUTO_INCREMENT,
    `record_id`       VARCHAR(32) NOT NULL,
    `tenant_id`       VARCHAR(32) NOT NULL DEFAULT '',
    `plan_id`         VARCHAR(32) NOT NULL,
    `product_id`      VARCHAR(32) NOT NULL DEFAULT '',
    `batch_no`        VARCHAR(50) NOT NULL DEFAULT '',
    `quantity`        DECIMAL(12,2) NOT NULL DEFAULT 0.00 COMMENT 'meters',
    `rolls`           INT NOT NULL DEFAULT 0,
    `price_per_meter` DECIMAL(10,2) NOT NULL DEFAULT 0.00,
    `operator_id`     VARCHAR(32) NOT NULL DEFAULT '',
    `remark`          TEXT,
    `created_at`      DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_record_id` (`record_id`),
    KEY `idx_plan` (`plan_id`),
    KEY `idx_tenant` (`tenant_id`),
    KEY `idx_product` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='按计划分批入库';
