<?php
namespace boru\ai\Compat\LegacyTool;

use boru\ai\Runtime\ToolDefinition;
use boru\ai\Runtime\ToolExecutionContext;

class LegacyToolAdapter
{
    public static function fromClass($legacyClass)
    {
        if (!is_string($legacyClass) || trim($legacyClass) === '') {
            throw new \InvalidArgumentException('Legacy tool class name is required');
        }

        if (!class_exists($legacyClass)) {
            throw new \InvalidArgumentException('Legacy tool class not found: ' . $legacyClass);
        }

        if (!method_exists($legacyClass, 'functionDefinitions')) {
            throw new \InvalidArgumentException('Legacy tool class must define functionDefinitions(): ' . $legacyClass);
        }

        if (!method_exists($legacyClass, 'handle')) {
            throw new \InvalidArgumentException('Legacy tool class must define handle(): ' . $legacyClass);
        }

        $legacyDefinition = call_user_func(array($legacyClass, 'functionDefinitions'));
        if (!is_object($legacyDefinition) || !method_exists($legacyDefinition, 'compile')) {
            throw new \InvalidArgumentException('Legacy tool class did not return a compilable ToolDefinition: ' . $legacyClass);
        }

        $compiled = $legacyDefinition->compile(false);
        if (!is_array($compiled)) {
            throw new \InvalidArgumentException('Legacy tool definition compile() must return an array: ' . $legacyClass);
        }

        $name = isset($compiled['name']) ? (string)$compiled['name'] : '';
        if ($name === '') {
            throw new \InvalidArgumentException('Legacy tool definition is missing a name: ' . $legacyClass);
        }

        $description = isset($compiled['description']) ? (string)$compiled['description'] : '';
        $schema = isset($compiled['parameters']) && is_array($compiled['parameters'])
            ? $compiled['parameters']
            : array('type' => 'object', 'properties' => array());

        $metadata = array(
            'legacy_adapter' => true,
            'legacy_class' => $legacyClass,
            'legacy_compiled' => $compiled,
        );

        if (array_key_exists('strict', $compiled)) {
            $metadata['legacy_strict'] = $compiled['strict'] ? true : false;
        }

        if (method_exists($legacyClass, 'agentInstructions')) {
            $agentInstructions = call_user_func(array($legacyClass, 'agentInstructions'));
            if (is_string($agentInstructions) && trim($agentInstructions) !== '') {
                $metadata['legacy_agent_instructions'] = $agentInstructions;
            }
        }

        $definition = new ToolDefinition();
        $definition->name($name)
            ->description($description)
            ->schema($schema)
            ->metadata($metadata)
            ->callback(function (array $arguments, ToolExecutionContext $context = null) use ($legacyClass) {
                $invocation = new LegacyToolInvocation($arguments, $context);
                return call_user_func(array($legacyClass, 'handle'), $invocation);
            });

        return $definition;
    }
}
