-- Lobster dual-mode execution: tenant switches + template execution_mode + task contact id

CREATE TABLE IF NOT EXISTS `lobster_execution_config`
(
    `id`                             bigint       NOT NULL AUTO_INCREMENT,
    `company_id`                     bigint       NOT NULL COMMENT 'tenant company id',
    `default_execution_mode`         varchar(16)  NOT NULL DEFAULT 'HYBRID' COMMENT 'PROMPT/INSTANCE/HYBRID',
    `enable_content_personalization` tinyint      NOT NULL DEFAULT 1 COMMENT 'content personalization',
    `enable_flow_personalization`    tinyint      NOT NULL DEFAULT 1 COMMENT 'flow personalization',
    `strict_fixed_workflow`          tinyint      NOT NULL DEFAULT 0 COMMENT '1=strict fixed canvas nodes',
    `auto_start_instance_on_tag`     tinyint      NOT NULL DEFAULT 0 COMMENT 'auto startWorkflow on tag bind',
    `create_by`                      varchar(64)           DEFAULT NULL,
    `create_time`                    datetime              DEFAULT CURRENT_TIMESTAMP,
    `update_by`                      varchar(64)           DEFAULT NULL,
    `update_time`                    datetime              DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_company_id` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='lobster execution policy';

SET @col_exists := (SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'company_workflow_lobster' AND COLUMN_NAME = 'execution_mode');
SET @ddl := IF(@col_exists = 0,
    'ALTER TABLE company_workflow_lobster ADD COLUMN execution_mode varchar(16) DEFAULT NULL COMMENT ''PROMPT/INSTANCE/HYBRID'' AFTER canvas_data',
    'SELECT 1');
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @col_exists := (SELECT COUNT(*) FROM information_schema.COLUMNS
    WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'company_workflow_lobster_task' AND COLUMN_NAME = 'external_contact_id');
SET @ddl := IF(@col_exists = 0,
    'ALTER TABLE company_workflow_lobster_task ADD COLUMN external_contact_id bigint DEFAULT NULL COMMENT ''qw_external_contact.id'' AFTER binding_id',
    'SELECT 1');
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
