CREATE TABLE IF NOT EXISTS meeting_field (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    field_code VARCHAR(64) NOT NULL,
    field_name VARCHAR(128) NOT NULL,
    field_values TEXT NOT NULL,
    scope_type VARCHAR(32) NOT NULL DEFAULT 'GLOBAL',
    project_id BIGINT UNSIGNED DEFAULT NULL,
    sort_no INT NOT NULL DEFAULT 0,
    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_field_code (tenant_id, field_code),
    KEY idx_tenant_status_sort (tenant_id, status, sort_no)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS invoice_profile (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    company_name VARCHAR(200) NOT NULL,
    tax_no VARCHAR(64) NOT NULL,
    bank_name VARCHAR(128) NOT NULL,
    account_no VARCHAR(128) NOT NULL,
    address VARCHAR(255) DEFAULT NULL,
    phone VARCHAR(64) DEFAULT NULL,
    default_project_id BIGINT UNSIGNED 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_tax_account (tenant_id, tax_no, account_no),
    KEY idx_tenant_status_project (tenant_id, status, default_project_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1061, 'meeting.withdraw', '撤回会议提交', 'meeting'),
    (1062, 'meeting.field.read', '查看会议字段配置', 'meeting'),
    (1063, 'meeting.field.manage', '管理会议字段配置', 'meeting'),
    (1064, 'invoice.profile.read', '查看发票抬头', 'invoice'),
    (1065, 'invoice.profile.manage', '管理发票抬头', 'invoice')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (70, 1, 101, 1061),
    (71, 1, 101, 1062),
    (72, 1, 101, 1063),
    (73, 1, 101, 1064),
    (74, 1, 101, 1065)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
