<?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;
use MefisAssetBridgeCore\Traits\Encryptor;
/**
 * GetAssets API Endpoint
 */
class GetAsset implements EndpointInterface
{
    use Encryptor;

    /** @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',
    ];
    private string $validationError = '';
    public function __construct(string $uuid, ApiClient $ApiClient, ?AuthenticatedUser $user, string $pathHash = '', string $masterKey = '')
    {
        $this->ApiClient = $ApiClient;
        $this->user = $user;
        $this->uuid = $uuid;
        $this->pathHash = $pathHash;

        if(empty($masterKey)) {
            throw new \RuntimeException('Master key is required to load asset');
        }
        $this->setEncryptorKey($masterKey);
    }

    /**
     * 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 get asset
     *
     * @param array $params Request parameters (optional)
     * @return ApiResponse
     */
    public function sendRequest(array $params = []): ApiResponse
    {
        // Validate required parameters
        if (! $this->validateParams($params)) {
            return ApiResponse::error($this->validationError);
        }
        $id = (int) $params['id'];
        $this->ApiClient->setUser($this->user);

        $response = $this->ApiClient->get('assets/'.$id);

        return $response;
    }
}