<?php

namespace XZD\AI\V5\AI;

/**
 * AI message and response type helpers
 */
class Types
{
    /**
     * Build system message
     *
     * @param string $content
     * @return array
     */
    public static function system(string $content): array
    {
        return ['role' => 'system', 'content' => $content];
    }

    /**
     * Build user message
     *
     * @param string|array $content
     * @return array
     */
    public static function user(string|array $content): array
    {
        return ['role' => 'user', 'content' => $content];
    }

    /**
     * Build assistant message
     *
     * @param string $content
     * @param array|null $toolCalls
     * @return array
     */
    public static function assistant(string $content, ?array $toolCalls = null): array
    {
        $msg = ['role' => 'assistant', 'content' => $content];
        if ($toolCalls !== null) {
            $msg['tool_calls'] = $toolCalls;
        }
        return $msg;
    }

    /**
     * Build tool result message
     *
     * @param string $toolCallId
     * @param string $content
     * @return array
     */
    public static function tool(string $toolCallId, string $content): array
    {
        return [
            'role'       => 'tool',
            'tool_call_id' => $toolCallId,
            'content'    => $content,
        ];
    }

    /**
     * Build success response
     *
     * @param string $content
     * @param int $inputTokens
     * @param int $outputTokens
     * @param array|null $toolCalls
     * @param int $cacheTokens
     * @return array
     */
    public static function response(string $content, int $inputTokens = 0, int $outputTokens = 0, ?array $toolCalls = null, int $cacheTokens = 0): array
    {
        return [
            'content'            => $content,
            'tool_calls'         => $toolCalls,
            'input_tokens'       => $inputTokens,
            'output_tokens'      => $outputTokens,
            'cache_read_tokens'  => $cacheTokens,
        ];
    }

    /**
     * Build error response
     *
     * @param string $message
     * @param string $code
     * @return array
     */
    public static function error(string $message, string $code = 'ai_error'): array
    {
        return [
            'content'           => '',
            'tool_calls'        => null,
            'input_tokens'      => 0,
            'output_tokens'     => 0,
            'cache_read_tokens' => 0,
            'error'             => $message,
            'error_code'        => $code,
        ];
    }
}
