-- NAC资产一键上链系统 - 数据库初始化脚本

-- 创建数据库
CREATE DATABASE IF NOT EXISTS nac_onboarding CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE nac_onboarding;

-- 用户表
CREATE TABLE IF NOT EXISTS users (
    id VARCHAR(36) PRIMARY KEY,
    username VARCHAR(50) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    full_name VARCHAR(100) NOT NULL,
    role ENUM('user', 'admin') DEFAULT 'user',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_username (username),
    INDEX idx_email (email),
    INDEX idx_role (role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 资产表
CREATE TABLE IF NOT EXISTS assets (
    id VARCHAR(36) PRIMARY KEY,
    user_id VARCHAR(36) NOT NULL,
    asset_type VARCHAR(50) NOT NULL,
    asset_info JSON NOT NULL,
    legal_docs JSON,
    kyc_level INT NOT NULL,
    jurisdiction VARCHAR(10) NOT NULL,
    state ENUM(
        'Pending',
        'ComplianceChecking',
        'Valuating',
        'GeneratingDNA',
        'Custodying',
        'MintingXTZH',
        'IssuingToken',
        'Listing',
        'Listed',
        'Failed'
    ) DEFAULT 'Pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
    INDEX idx_user_id (user_id),
    INDEX idx_state (state),
    INDEX idx_asset_type (asset_type),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 上链记录表
CREATE TABLE IF NOT EXISTS onboarding_records (
    id VARCHAR(36) PRIMARY KEY,
    asset_id VARCHAR(36) NOT NULL,
    state ENUM(
        'Pending',
        'ComplianceChecking',
        'Valuating',
        'GeneratingDNA',
        'Custodying',
        'MintingXTZH',
        'IssuingToken',
        'Listing',
        'Listed',
        'Failed'
    ) DEFAULT 'Pending',
    recipient_address VARCHAR(66) NOT NULL,
    custody_provider VARCHAR(50) NOT NULL,
    compliance_result JSON,
    valuation_result JSON,
    dna_result JSON,
    custody_result JSON,
    xtzh_result JSON,
    token_result JSON,
    listing_result JSON,
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    completed_at TIMESTAMP NULL,
    FOREIGN KEY (asset_id) REFERENCES assets(id) ON DELETE CASCADE,
    INDEX idx_asset_id (asset_id),
    INDEX idx_state (state),
    INDEX idx_created_at (created_at),
    INDEX idx_completed_at (completed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- 创建默认管理员账号
-- 密码: admin123 (实际部署时请修改)
INSERT INTO users (id, username, password_hash, email, full_name, role)
VALUES (
    UUID(),
    'admin',
    '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYIvAprzO3i',
    'admin@newassetchain.io',
    '系统管理员',
    'admin'
) ON DUPLICATE KEY UPDATE username=username;

-- 创建测试用户
-- 密码: user123 (实际部署时请删除)
INSERT INTO users (id, username, password_hash, email, full_name, role)
VALUES (
    UUID(),
    'testuser',
    '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/LewY5GyYIvAprzO3i',
    'test@newassetchain.io',
    '测试用户',
    'user'
) ON DUPLICATE KEY UPDATE username=username;
