CREATE TABLE IF NOT EXISTS expert (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    expert_name VARCHAR(128) NOT NULL,
    id_no VARCHAR(64) NOT NULL,
    phone VARCHAR(32) NOT NULL,
    title VARCHAR(128) DEFAULT NULL,
    organization VARCHAR(255) DEFAULT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    is_deleted TINYINT(1) NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_tenant_id_no (tenant_id, id_no),
    KEY idx_tenant_name (tenant_id, expert_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS expert_bank_card (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    expert_id BIGINT UNSIGNED NOT NULL,
    bank_name VARCHAR(128) NOT NULL,
    bank_card_no VARCHAR(128) NOT NULL,
    account_name VARCHAR(128) NOT NULL,
    is_default CHAR(1) NOT NULL DEFAULT 'N',
    is_deleted TINYINT(1) NOT NULL DEFAULT 0,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    KEY idx_tenant_expert (tenant_id, expert_id),
    KEY idx_tenant_card (tenant_id, bank_card_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS expert_merge_log (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    target_expert_id BIGINT UNSIGNED NOT NULL,
    source_expert_id BIGINT UNSIGNED NOT NULL,
    reason VARCHAR(500) DEFAULT NULL,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tenant_target (tenant_id, target_expert_id),
    KEY idx_tenant_source (tenant_id, source_expert_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS meeting_expert_snapshot (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    meeting_id BIGINT UNSIGNED NOT NULL,
    expert_id BIGINT UNSIGNED NOT NULL,
    snapshot_json TEXT NOT NULL,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_tenant_meeting (tenant_id, meeting_id),
    KEY idx_tenant_expert (tenant_id, expert_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1051, 'expert.read', '查看专家', 'expert'),
    (1052, 'expert.create', '创建专家', 'expert'),
    (1053, 'expert.merge', '专家合并', 'expert'),
    (1054, 'expert.import', '导入专家', 'expert'),
    (1055, 'expert.export', '导出专家', 'expert'),
    (1056, 'expert.card.manage', '管理专家银行卡', 'expert')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (52, 1, 101, 1051),
    (53, 1, 101, 1052),
    (54, 1, 101, 1053),
    (55, 1, 101, 1054),
    (56, 1, 101, 1055),
    (57, 1, 101, 1056)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
