<?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 GetAssets 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;

    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
    {
        // No required parameters for this endpoint
        return true;
    }

    /**
     * Send request to get asset
     *
     * @param array $params Request parameters (optional)
     * @return ApiResponse
     */
    public function sendRequest(array $params = []): ApiResponse
    {
        $this->ApiClient->setUser($this->user);

        // Prepare request data
        $queryParams = [];


        if($params['sort'] && !in_array($params['sort'], ['date_desc', 'date_asc', 'name_asc', 'name_desc']))
        {
            $params['sort'] = '';
        }

        // trim each param to max 100 chars to avoid abuse
        foreach($params as $key => $value)
        {
            if(is_string($value))
            {
                $params[$key] = trim(substr($value, 0, 100));
            }
        }

        $queryParams['is_element'] = isset($params['isElement']) && $params['isElement'] === true ? '1' : '0';
        if (!empty($params['search'])) {
            $queryParams['search'] = $params['search'];
        }

        if (!empty($params['sort'])) {
            $queryParams['sort'] = $params['sort'];
        }

        if (!empty($params['origin'])) {
            $queryParams['origin'] = intval($params['origin']);
        }

        if (!empty($params['type'])) {
            $queryParams['type'] = $params['type'];
        }

        if (!empty($params['category'])) {
            $queryParams['category'] = intval($params['category']);
        }

        if (!empty($params['page'])) {
            $queryParams['page'] = intval($params['page']);
        }

        if (!empty($params['limit'])) {
            $queryParams['limit'] = intval($params['limit']);
        }

        $queryParams['meta'] = $params['meta'] === true ? '1' : '0';

        $response = $this->ApiClient->get('assets', $queryParams);

        return $response;
    }
}