CREATE TABLE IF NOT EXISTS platform_user (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    user_name VARCHAR(64) NOT NULL,
    phone VARCHAR(32) NOT NULL,
    email VARCHAR(128) DEFAULT NULL,
    password_hash VARCHAR(255) NOT NULL,
    status VARCHAR(32) NOT NULL DEFAULT 'ENABLED',
    valid_from DATETIME DEFAULT NULL,
    valid_to DATETIME DEFAULT NULL,
    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_platform_phone (phone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS platform_role (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    role_code VARCHAR(64) NOT NULL,
    role_name VARCHAR(64) NOT 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_platform_role_code (role_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS platform_permission (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    permission_code VARCHAR(128) NOT NULL,
    permission_name VARCHAR(128) NOT NULL,
    module VARCHAR(64) NOT NULL,
    UNIQUE KEY uk_platform_permission_code (permission_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS platform_user_role (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT UNSIGNED NOT NULL,
    role_id BIGINT UNSIGNED NOT NULL,
    UNIQUE KEY uk_platform_user_role (user_id, role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS platform_role_permission (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    role_id BIGINT UNSIGNED NOT NULL,
    permission_id BIGINT UNSIGNED NOT NULL,
    UNIQUE KEY uk_platform_role_permission (role_id, permission_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT INTO platform_role (id, role_code, role_name, status, is_deleted, created_by, updated_by)
VALUES (1, 'PLATFORM_SUPER_ADMIN', '缁崵绮虹搾鍛獓缁狅紕鎮婇崨?, 'ENABLED', 0, 0, 0)
ON DUPLICATE KEY UPDATE role_name = VALUES(role_name), status = VALUES(status), updated_at = CURRENT_TIMESTAMP;

INSERT INTO platform_permission (id, permission_code, permission_name, module)
VALUES
    (1, 'platform.tenant.manage', '楠炲啿褰寸粔鐔稿煕缁狅紕鎮?, 'platform'),
    (2, 'platform.user.manage', '楠炲啿褰寸拹锕€褰跨粻锛勬倞', 'platform'),
    (3, 'platform.audit.read', '楠炲啿褰寸€孤ゎ吀閺屻儳婀?, 'platform')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name), module = VALUES(module);

INSERT INTO platform_role_permission (id, role_id, permission_id)
VALUES
    (1, 1, 1),
    (2, 1, 2),
    (3, 1, 3)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);

INSERT INTO platform_user (id, user_name, phone, email, password_hash, status, valid_from, valid_to, is_deleted, created_by, updated_by)
VALUES (1, '缁崵绮虹搾鍛獓缁狅紕鎮婇崨?, '13900000000', 'platform-admin@writeoff.local', '123456', 'ENABLED', NOW(), '2099-12-31 23:59:59', 0, 0, 0)
ON DUPLICATE KEY UPDATE user_name = VALUES(user_name), status = VALUES(status), updated_at = CURRENT_TIMESTAMP;

INSERT INTO platform_user_role (id, user_id, role_id)
VALUES (1, 1, 1)
ON DUPLICATE KEY UPDATE role_id = VALUES(role_id);
