-- Incremental migration: multi-tenant CRM baseline
-- Apply to existing muyu_wms schema.

USE `muyu_wms`;

-- MySQL 8 does not support ADD COLUMN IF NOT EXISTS; use separate statements.
SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='muyu_wms' AND TABLE_NAME='sys_operation_log' AND COLUMN_NAME='tenant_id');
SET @sql = IF(@col_exists = 0, 'ALTER TABLE `sys_operation_log` ADD COLUMN `tenant_id` VARCHAR(32) NOT NULL DEFAULT \'\' AFTER `user_id`', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @col_exists = (SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='muyu_wms' AND TABLE_NAME='sys_operation_log' AND COLUMN_NAME='target_tenant_id');
SET @sql = IF(@col_exists = 0, 'ALTER TABLE `sys_operation_log` ADD COLUMN `target_tenant_id` VARCHAR(32) NOT NULL DEFAULT \'\' AFTER `tenant_id`', 'SELECT 1');
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

CREATE TABLE IF NOT EXISTS `crm_tenant` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `tenant_id` VARCHAR(32) NOT NULL,
    `tenant_name` VARCHAR(100) NOT NULL,
    `tenant_code` VARCHAR(64) NOT NULL DEFAULT '',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1:enabled 0:disabled',
    `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_tenant_id` (`tenant_id`),
    UNIQUE KEY `uk_tenant_code` (`tenant_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `crm_tenant_user` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `tenant_user_id` VARCHAR(32) NOT NULL,
    `tenant_id` VARCHAR(32) NOT NULL,
    `user_id` VARCHAR(32) NOT NULL,
    `is_primary` TINYINT NOT NULL DEFAULT 1 COMMENT '1:primary 0:secondary',
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1:enabled 0:disabled',
    `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_tenant_user_id` (`tenant_user_id`),
    UNIQUE KEY `uk_tenant_user_pair` (`tenant_id`, `user_id`),
    KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `crm_tenant_relationship` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `relationship_id` VARCHAR(40) NOT NULL,
    `owner_tenant_id` VARCHAR(32) NOT NULL,
    `from_tenant_id` VARCHAR(32) NOT NULL,
    `to_tenant_id` VARCHAR(32) NOT NULL,
    `relation_type` VARCHAR(32) NOT NULL,
    `status` TINYINT NOT NULL DEFAULT 1 COMMENT '1:active 0:inactive',
    `valid_from` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `valid_to` DATETIME DEFAULT NULL,
    `source` VARCHAR(32) NOT NULL DEFAULT 'manual',
    `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_relationship_id` (`relationship_id`),
    UNIQUE KEY `uk_relationship_unique` (`owner_tenant_id`, `from_tenant_id`, `to_tenant_id`, `relation_type`, `valid_from`),
    KEY `idx_owner_from_status` (`owner_tenant_id`, `from_tenant_id`, `status`),
    KEY `idx_owner_to_status` (`owner_tenant_id`, `to_tenant_id`, `status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `crm_tenant_relationship_history` (
    `id` BIGINT NOT NULL AUTO_INCREMENT,
    `history_id` VARCHAR(40) NOT NULL,
    `relationship_id` VARCHAR(40) NOT NULL,
    `change_type` VARCHAR(32) NOT NULL,
    `before_json` JSON NULL,
    `after_json` JSON NULL,
    `changed_by` VARCHAR(32) NOT NULL DEFAULT '',
    `changed_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uk_history_id` (`history_id`),
    KEY `idx_relationship_id` (`relationship_id`),
    KEY `idx_changed_at` (`changed_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `crm_tenant` (`tenant_id`, `tenant_name`, `tenant_code`, `status`)
VALUES ('t_default_001', '默认租户', 'DEFAULT', 1)
ON DUPLICATE KEY UPDATE tenant_name = VALUES(tenant_name), status = VALUES(status);

INSERT INTO `crm_tenant_user` (`tenant_user_id`, `tenant_id`, `user_id`, `is_primary`, `status`)
SELECT 'tu_admin_001', 't_default_001', 'u_admin_001', 1, 1
WHERE NOT EXISTS (
  SELECT 1 FROM `crm_tenant_user` WHERE `tenant_id` = 't_default_001' AND `user_id` = 'u_admin_001'
);
