-- 大模型供应商管理表
CREATE TABLE IF NOT EXISTS `company_ai_provider` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `provider_code` varchar(50) DEFAULT NULL,
    `provider_name` varchar(100) DEFAULT NULL,
    `api_key` varchar(500) DEFAULT NULL,
    `api_endpoint` varchar(500) DEFAULT NULL,
    `model_name` varchar(100) DEFAULT NULL,
    `max_tokens` int DEFAULT 4096,
    `temperature` double DEFAULT 0.7,
    `is_default` int DEFAULT 0,
    `enabled` int DEFAULT 1,
    `del_flag` int DEFAULT 0,
    `create_by` varchar(64) DEFAULT NULL,
    `create_time` datetime DEFAULT NULL,
    `update_by` varchar(64) DEFAULT NULL,
    `update_time` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_company_id` (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业AI供应商配置';

-- 工作流实例表
CREATE TABLE IF NOT EXISTS `lobster_workflow_instance` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `workflow_id` bigint DEFAULT NULL,
    `instance_name` varchar(200) DEFAULT NULL,
    `status` varchar(20) DEFAULT 'pending',
    `contact_id` bigint DEFAULT NULL,
    `contact_name` varchar(100) DEFAULT NULL,
    `current_node_index` int DEFAULT 0,
    `current_node_name` varchar(100) DEFAULT NULL,
    `total_nodes` int DEFAULT 0,
    `completed_nodes` int DEFAULT 0,
    `context_snapshot` text DEFAULT NULL,
    `variables` text DEFAULT NULL,
    `start_time` varchar(30) DEFAULT NULL,
    `end_time` varchar(30) DEFAULT NULL,
    `last_activity_time` varchar(30) DEFAULT NULL,
    `error_message` text DEFAULT NULL,
    `del_flag` int DEFAULT 0,
    `create_by` varchar(64) DEFAULT NULL,
    `create_time` datetime DEFAULT NULL,
    `update_by` varchar(64) DEFAULT NULL,
    `update_time` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_company_id` (`company_id`),
    KEY `idx_workflow_id` (`workflow_id`),
    KEY `idx_contact_id` (`contact_id`),
    KEY `idx_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='龙虾工作流实例';

-- 节点执行日志表
CREATE TABLE IF NOT EXISTS `lobster_node_execution_log` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `instance_id` bigint DEFAULT NULL,
    `workflow_id` bigint DEFAULT NULL,
    `node_index` int DEFAULT NULL,
    `node_type` varchar(30) DEFAULT NULL,
    `node_name` varchar(100) DEFAULT NULL,
    `input_content` text DEFAULT NULL,
    `output_content` text DEFAULT NULL,
    `ai_model` varchar(50) DEFAULT NULL,
    `status` varchar(20) DEFAULT NULL,
    `duration_ms` bigint DEFAULT NULL,
    `token_usage` int DEFAULT NULL,
    `error_message` text DEFAULT NULL,
    `retry_count` int DEFAULT 0,
    `del_flag` int DEFAULT 0,
    `create_by` varchar(64) DEFAULT NULL,
    `create_time` datetime DEFAULT NULL,
    `update_by` varchar(64) DEFAULT NULL,
    `update_time` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_instance_id` (`instance_id`),
    KEY `idx_workflow_id` (`workflow_id`),
    KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='龙虾节点执行日志';

-- 对话摘要表
CREATE TABLE IF NOT EXISTS `lobster_conversation_summary` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `instance_id` bigint DEFAULT NULL,
    `contact_id` bigint DEFAULT NULL,
    `summary_type` varchar(30) DEFAULT NULL,
    `summary_content` text DEFAULT NULL,
    `key_points` text DEFAULT NULL,
    `sentiment_analysis` varchar(20) DEFAULT NULL,
    `next_action_suggestion` text DEFAULT NULL,
    `message_count` int DEFAULT 0,
    `del_flag` int DEFAULT 0,
    `create_by` varchar(64) DEFAULT NULL,
    `create_time` datetime DEFAULT NULL,
    `update_by` varchar(64) DEFAULT NULL,
    `update_time` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_instance_id` (`instance_id`),
    KEY `idx_contact_id` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='龙虾对话摘要';

-- 合规规则表
CREATE TABLE IF NOT EXISTS `lobster_compliance_rule` (
    `id` bigint NOT NULL AUTO_INCREMENT,
    `company_id` bigint DEFAULT NULL,
    `rule_name` varchar(100) DEFAULT NULL,
    `rule_type` varchar(30) DEFAULT 'keyword',
    `pattern` text DEFAULT NULL,
    `description` varchar(500) DEFAULT NULL,
    `action` varchar(50) DEFAULT 'warn',
    `severity` int DEFAULT 1,
    `enabled` int DEFAULT 1,
    `del_flag` int DEFAULT 0,
    `create_by` varchar(64) DEFAULT NULL,
    `create_time` datetime DEFAULT NULL,
    `update_by` varchar(64) DEFAULT NULL,
    `update_time` datetime DEFAULT NULL,
    PRIMARY KEY (`id`),
    KEY `idx_company_id` (`company_id`),
    KEY `idx_enabled` (`enabled`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='龙虾合规规则';
