CREATE TABLE IF NOT EXISTS sys_user (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    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',
    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_phone (tenant_id, phone)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS role (
    id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    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_tenant_role_code (tenant_id, role_code)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

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

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

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

INSERT INTO role (id, tenant_id, role_code, role_name, status, created_by, updated_by)
VALUES
    (101, 1, 'TENANT_ADMIN', '閸楁洑缍呯粻锛勬倞閸?, 'ENABLED', 0, 0),
    (102, 1, 'PROJECT_OWNER', '妞ゅ湱娲扮拹鐔荤煑娴?, 'ENABLED', 0, 0),
    (103, 1, 'EXECUTOR', '妞ゅ湱娲伴幍褑顢戞禍?, 'ENABLED', 0, 0),
    (104, 1, 'AUDITOR', '鐎光剝鐗虫禍?, 'ENABLED', 0, 0),
    (105, 1, 'FINANCE', '鐠愩垹濮?, 'ENABLED', 0, 0)
ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;

INSERT INTO permission (id, permission_code, permission_name, module)
VALUES
    (1001, 'project.create', '閸掓稑缂撴い鍦窗', 'project'),
    (1002, 'project.freeze', '閸愯崵绮ㄦい鍦窗', 'project'),
    (1003, 'meeting.create', '閸掓稑缂撴导姘愁唴', 'meeting'),
    (1004, 'meeting.submit', '娴兼俺顔呯痪褎褰佹禍?, 'meeting'),
    (1005, 'audit.approve', '鐎光剝鐗抽柅姘崇箖', 'audit'),
    (1006, 'audit.reject', '鐎光剝鐗抽幏鎺旂卜', 'audit'),
    (1007, 'audit.return', '鐎光剝鐗抽柅鈧崶?, 'audit'),
    (1008, 'finance.payment.confirm', '閺€顖欑帛绾喛顓?, 'finance')
ON DUPLICATE KEY UPDATE permission_name = VALUES(permission_name);

INSERT INTO sys_user (id, tenant_id, user_name, phone, email, password_hash, status, created_by, updated_by)
VALUES (1, 1, '缁狅紕鎮婇崨?, '13800000000', 'admin@writeoff.local', '123456', 'ENABLED', 0, 0)
ON DUPLICATE KEY UPDATE updated_at = CURRENT_TIMESTAMP;

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

INSERT INTO role_permission (id, tenant_id, role_id, permission_id)
VALUES
    (1, 1, 101, 1001),
    (2, 1, 101, 1002),
    (3, 1, 101, 1003),
    (4, 1, 101, 1004),
    (5, 1, 101, 1005),
    (6, 1, 101, 1006),
    (7, 1, 101, 1007),
    (8, 1, 101, 1008)
ON DUPLICATE KEY UPDATE permission_id = VALUES(permission_id);
