<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  MefisAssetBridgeCore_Api_Endpoints
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

namespace MefisAssetBridgeCore\Api\Endpoints\Asset;

use MefisAssetBridgeCore\Api\ApiClient;
use MefisAssetBridgeCore\Auth\AuthenticatedUser;
use MefisAssetBridgeCore\Api\Response\ApiResponse;
use MefisAssetBridgeCore\Interfaces\EndpointInterface;

/**
 * RenameAsset API Endpoint
 */
class RenameAsset implements EndpointInterface
{
    /** @var ApiClient API client instance */
    private ApiClient $ApiClient;

    /** @var AuthenticatedUser|null Authenticated user */
    private ?AuthenticatedUser $user;

    private string $uuid;
    private string $pathHash;

    /** @var array Required parameters */
    private array $requiredParams = [
        'id',
        'name'
    ];

    private string $validationError = '';

    public function __construct(string $uuid, ApiClient $ApiClient, ?AuthenticatedUser $user, string $pathHash = '')
    {
        $this->ApiClient = $ApiClient;
        $this->user = $user;
        $this->uuid = $uuid;
        $this->pathHash = $pathHash;
    }

    /**
     * Validate request parameters
     *
     * @param array $params Request parameters
     * @return bool True if parameters are valid
     */
    public function validateParams(array $params): bool
    {

        foreach ($this->requiredParams as $param) {
            if (!isset($params[$param])) {
                $this->validationError = sprintf('Missing required parameter: %s', $param);
                return false;
            }
        }

        return true;
    }

    /**
     * Send request to save a asset
     *
     * @param array $params Request parameters
     * @return ApiResponse
     */
    public function sendRequest(array $params): ApiResponse
    {

        // Validate required parameters
        if (!$this->validateParams($params)) {
            return ApiResponse::error($this->validationError);
        }

        $this->ApiClient->setUser($this->user);

        $requestData = [
            'name' => $params['name']
        ];

        // Create a new asset
        return $this->ApiClient->patch('assets/'.$params['id'].'/name', $requestData);

    }
}