DROP TABLE IF EXISTS sys_user_role;
DROP TABLE IF EXISTS sys_role;

CREATE TABLE IF NOT EXISTS sys_user (
    user_id VARCHAR(36) PRIMARY KEY,
    student_id VARCHAR(255) NULL,
    password VARCHAR(255) NOT NULL,
    user_name VARCHAR(100) NULL,
    class_id VARCHAR(36) NULL,
    class_name VARCHAR(100) NULL,
    phone VARCHAR(100) NULL,
    school_name VARCHAR(100) NULL,
    school_id VARCHAR(36) NULL,
    authorize_time DATE NULL,
    authorize_end_time DATE NULL,
    role_id TINYINT NULL,
    create_time DATETIME NULL,
    is_deleted TINYINT NOT NULL DEFAULT 0,
    zy_user_id VARCHAR(50) NULL,
    UNIQUE KEY uk_sys_user_student_id (student_id)
);

CREATE TABLE IF NOT EXISTS platform_user_snapshot (
    platform_user_id VARCHAR(36) PRIMARY KEY,
    account VARCHAR(64) NOT NULL,
    display_name VARCHAR(64) NOT NULL,
    source_updated_at TIMESTAMP NOT NULL,
    synced_at TIMESTAMP NOT NULL,
    school_id VARCHAR(64) NULL,
    school_name VARCHAR(128) NULL,
    college_id VARCHAR(64) NULL,
    college_name VARCHAR(128) NULL,
    major_id VARCHAR(64) NULL,
    major_name VARCHAR(128) NULL,
    class_id VARCHAR(64) NULL,
    class_name VARCHAR(128) NULL,
    student_id VARCHAR(64) NULL
);

CREATE TABLE IF NOT EXISTS auth_login_exchange_code (
    code_hash CHAR(64) PRIMARY KEY,
    platform_user_id VARCHAR(36) NOT NULL,
    expires_at TIMESTAMP NOT NULL,
    consumed_at TIMESTAMP NULL
);

CREATE TABLE IF NOT EXISTS auth_refresh_token (
    token_hash CHAR(64) PRIMARY KEY,
    platform_user_id VARCHAR(36) NOT NULL,
    expires_at TIMESTAMP NOT NULL,
    revoked_at TIMESTAMP NULL
);

CREATE TABLE IF NOT EXISTS institution_sm2_key (
    id BIGINT PRIMARY KEY,
    key_id VARCHAR(64) NOT NULL,
    key_name VARCHAR(64) NOT NULL,
    key_owner VARCHAR(32) NOT NULL,
    key_purpose VARCHAR(32) NOT NULL,
    user_id VARCHAR(36) NOT NULL,
    school_id BIGINT NOT NULL,
    class_id BIGINT NOT NULL,
    public_key VARCHAR(256) NOT NULL,
    encrypted_private_key VARCHAR(512) NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    created_by VARCHAR(64) NOT NULL,
    updated_by VARCHAR(64) NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS institution_sm2_key_audit (
    id BIGINT PRIMARY KEY,
    key_record_id BIGINT NOT NULL,
    key_id VARCHAR(64) NOT NULL,
    operation VARCHAR(32) NOT NULL,
    user_id VARCHAR(36) NOT NULL,
    school_id BIGINT NOT NULL,
    class_id BIGINT NOT NULL,
    operation_detail VARCHAR(512) NOT NULL,
    created_at TIMESTAMP NOT NULL,
    created_by VARCHAR(64) NOT NULL,
    CONSTRAINT fk_institution_key_audit_key
        FOREIGN KEY (key_record_id) REFERENCES institution_sm2_key(id)
);

INSERT INTO sys_user (
    user_id, student_id, password, user_name, role_id, is_deleted, zy_user_id
)
VALUES (
    '00000000-0000-0000-0000-000000000487',
    'tzs001',
    '$2a$10$ufcw5KFHtOmLzxAsV4C.MuIDOErMlw0iw5J5hc8OMzaTx0u9QYxG6',
    'tzs001',
    4,
    0,
    '487'
)
ON DUPLICATE KEY UPDATE
    student_id = VALUES(student_id),
    password = VALUES(password),
    user_name = VALUES(user_name),
    role_id = VALUES(role_id),
    is_deleted = 0;

CREATE TABLE IF NOT EXISTS issuance_bank_inventory (
    bank_code VARCHAR(32) PRIMARY KEY,
    current_balance DECIMAL(20, 2) NOT NULL,
    warning_threshold DECIMAL(20, 2) NOT NULL,
    updated_at TIMESTAMP NOT NULL
);

CREATE TABLE IF NOT EXISTS issuance_request (
    id CHAR(36) PRIMARY KEY,
    request_no VARCHAR(64) NOT NULL UNIQUE,
    bank_code VARCHAR(32) NOT NULL,
    organization_id VARCHAR(64) NOT NULL,
    total_amount DECIMAL(20, 2) NOT NULL,
    currency VARCHAR(16) NOT NULL,
    request_timestamp VARCHAR(32),
    message_text TEXT,
    digest CHAR(64),
    signature TEXT,
    signing_key_ref VARCHAR(64),
    payload_json TEXT,
    status VARCHAR(32) NOT NULL,
    central_receive_status VARCHAR(32) NOT NULL,
    central_received_at TIMESTAMP NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    created_by VARCHAR(64) NOT NULL,
    updated_by VARCHAR(64) NOT NULL,
    created_by_user_id BIGINT NOT NULL,
    updated_by_user_id BIGINT NOT NULL,
    deleted BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE IF NOT EXISTS issuance_request_denomination (
    request_id CHAR(36) NOT NULL,
    denomination DECIMAL(10, 2) NOT NULL,
    quantity INT NOT NULL,
    created_at TIMESTAMP NOT NULL,
    updated_at TIMESTAMP NOT NULL,
    created_by VARCHAR(64) NOT NULL,
    updated_by VARCHAR(64) NOT NULL,
    created_by_user_id BIGINT NOT NULL,
    updated_by_user_id BIGINT NOT NULL,
    PRIMARY KEY (request_id, denomination)
);

INSERT INTO issuance_bank_inventory (bank_code, current_balance, warning_threshold, updated_at)
VALUES ('BKCHCNBJ00001', 49950000.00, 50000000.00, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE current_balance = VALUES(current_balance),
                        warning_threshold = VALUES(warning_threshold),
                        updated_at = CURRENT_TIMESTAMP;

INSERT INTO platform_user_snapshot (
    platform_user_id, account, display_name, source_updated_at, synced_at
)
VALUES (
    '00000000-0000-0000-0000-000000000487',
    'tzs001',
    'tzs001',
    CURRENT_TIMESTAMP,
    CURRENT_TIMESTAMP
)
ON DUPLICATE KEY UPDATE
    account = VALUES(account),
    display_name = VALUES(display_name),
    source_updated_at = CURRENT_TIMESTAMP,
    synced_at = CURRENT_TIMESTAMP;
