<?php

namespace XZD\AI\V5\AI;

/**
 * AI 类型定义 — 统一消息和响应格式
 */
class Types
{
    /**
     * 构建系统消息
     */
    public static function system(string $content): array
    {
        return ['role' => 'system', 'content' => $content];
    }

    /**
     * 构建用户消息
     */
    public static function user(string|array $content): array
    {
        return ['role' => 'user', 'content' => $content];
    }

    /**
     * 构建助手消息
     */
    public static function assistant(string $content, ?array $toolCalls = null): array
    {
        $msg = ['role' => 'assistant', 'content' => $content];
        if ($toolCalls !== null) {
            $msg['tool_calls'] = $toolCalls;
        }
        return $msg;
    }

    /**
     * 构建工具结果消息
     */
    public static function tool(string $toolCallId, string $content): array
    {
        return [
            'role'       => 'tool',
            'tool_call_id' => $toolCallId,
            'content'    => $content,
        ];
    }

    /**
     * 构建成功响应
     */
    public static function response(string $content, int $inputTokens = 0, int $outputTokens = 0, ?array $toolCalls = null): array
    {
        return [
            'content'       => $content,
            'tool_calls'    => $toolCalls,
            'input_tokens'  => $inputTokens,
            'output_tokens' => $outputTokens,
        ];
    }

    /**
     * 构建错误响应
     */
    public static function error(string $message, string $code = 'ai_error'): array
    {
        return [
            'content'       => '',
            'tool_calls'    => null,
            'input_tokens'  => 0,
            'output_tokens' => 0,
            'error'         => $message,
            'error_code'    => $code,
        ];
    }
}
