<?php

namespace Modules\RemoteData\Mcp\Entity;

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

final class CorporateOffice
{
    public function __construct(
        private readonly \Plugins\AiAssistant\Contexts\Entity\CorporateOffice $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_office_draft')]
    public function createCorporateOfficeDraft(?int $idCorporate, ?string $name, ?string $address, ?string $countryCode, ?int $cityId, ?string $telephone, ?string $email, ?string $comments): array
    {
        $args = compact(
            'idCorporate', 'name', 'address', 'countryCode', 'cityId', 'telephone', 'email', 'comments',
        );

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

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

        return $response;
    }

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

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

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

        return $response;
    }

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

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

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

        return $response;
    }

    #[McpTool(name: 'submit_corporate_office')]
    public function submitCorporateOffice(?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_office' invoked", ['args' => new \Engine\Logger\Context\ArrayAssoc($args), ]);

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

        return $response;
    }


}
