<?php

namespace Modules\RemoteData\Mcp\Entity;

use Mcp\Capability\Attribute\McpResource;
use Mcp\Capability\Attribute\McpTool;
use Psr\Log\LoggerInterface;

final class Corporate
{
    public function __construct(
        private readonly \Plugins\AiAssistant\Contexts\Entity\Corporate $context,
        private readonly LoggerInterface                                $logger,
        private readonly \Engine\Cache\Disk                             $cache,
    )
    {
        $this->context->setCache($this->cache);

        $this->logger->info('Handler `' . __CLASS__ . '` instantiated with dependencies.');
    }

    #[McpTool(name: 'create_corporate_draft')]
    public function createCorporateDraft(?string $type, ?string $companyPrefix, ?string $company, ?string $companySuffix, ?string $alias, ?int $idTrust, ?string $fiscalCode, ?string $regNo, ?bool $vatRegistered, ?string $vatCertificateNumber, ?string $accountingNumber, ?string $countryCode, ?string $idCity): array
    {
        $args = compact(
            'type', 'companyPrefix', 'company', 'companySuffix', 'alias', 'idTrust',
            'fiscalCode', 'regNo', 'vatRegistered', 'vatCertificateNumber', 'accountingNumber',
            'countryCode', 'idCity',
        );

        $this->logger->info("Tool 'create_corporate_draft' invoked", ['args' => new \Engine\Logger\Context\ArrayAssoc($args), ]);

        $response = $this->context->executeMcpTool('create_corporate_draft', $args);

        return $response;
    }

    #[McpTool(name: 'update_corporate_draft')]
    public function updateCorporateDraft(?string $draft_id, ?array $patch): array
    {
        $args = compact(
            'draft_id', 'patch',
        );

        $this->logger->info("Tool 'update_corporate_draft' invoked", ['args' => new \Engine\Logger\Context\ArrayAssoc($args), ]);

        $response = $this->context->executeMcpTool('update_corporate_draft', $args);

        return $response;
    }

    #[McpTool(name: 'preview_corporate')]
    public function previewCorporate(?int $draft_id): array
    {
        $args = compact(
            'draft_id'
        );

        $this->logger->info("Tool 'preview_corporate' invoked", ['args' => new \Engine\Logger\Context\ArrayAssoc($args), ]);

        $response = $this->context->executeMcpTool('preview_corporate', $args);

        return $response;
    }

    #[McpTool(name: 'submit_corporate')]
    public function submitCorporate(?int $draft_id, ?string $user_confirmation_text): array
    {
        // TODO AUTHENTICATE
        // grant full access level
        \App\UserAccess\CurrentUserAccessFast::getInstance()->becomeSuperUser();

        $args = compact(
            'draft_id', 'user_confirmation_text'
        );

        $this->logger->info("Tool 'submit_corporate' invoked", ['args' => new \Engine\Logger\Context\ArrayAssoc($args), ]);

        $response = $this->context->executeMcpTool('submit_corporate', $args);

        return $response;
    }


}
