<?php
/**
 * BookiFlex Core
 *
 * @package    BookiFlex
 * @subpackage Core
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Core\Api\Controller;

use BookiFlex\Core\Api\Transformer\AccommodationTypeTransformer;
use BookiFlex\Core\Contract\RoleCapabilitiesInterface;
use BookiFlex\Core\Repository\AccommodationTypeRepository;
use BookiFlex\Core\Util\Iter;
use Throwable;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
 * AccommodationType REST API Controller
 *
 * Endpoints:
 * - GET /accommodation-types - List all accommodation types
 */
class AccommodationTypeController extends AbstractController
{
    public const API_ENDPOINT_NAME = 'accommodation-types';

    public function __construct(
        private readonly AccommodationTypeRepository $accommodationTypeRepository,
        private readonly AccommodationTypeTransformer $accommodationTypeTransformer,
        string $namespace = 'bflex/v1'
    ) {
        parent::__construct($namespace);
    }

    public function registerRoutes(): void
    {
        // GET /accommodation-types - List all accommodation types
        register_rest_route($this->namespace, '/' . self::API_ENDPOINT_NAME, [
            [
                'methods' => 'GET',
                'callback' => [$this, 'getAccommodationTypes'],
                'permission_callback' => [$this, 'hasAccess'],
            ],
        ]);
    }

    /**
     * GET /accommodation-types - List all accommodation types
     *
     * @param WP_REST_Request $request
     * @return WP_REST_Response|WP_Error
     */
    public function getAccommodationTypes(WP_REST_Request $request): WP_REST_Response|WP_Error
    {
        try {
            $accommodationTypes = $this->accommodationTypeRepository->findAll();

            $accommodationTypesArray = Iter::toArray($accommodationTypes);
            $accommodationTypesArray = array_filter($accommodationTypesArray, fn($at) => $at->isActive());

            return $this->success([
                'status' => 'success',
                'result' => [
                    'accommodationTypes' => $this->accommodationTypeTransformer->transformCollection($accommodationTypesArray),
                    'total' => count($accommodationTypesArray),
                ],
            ]);
        } catch (Throwable $e) {
            return $this->handleError($e, 'accommodation_type_fetch_failed', __('Failed to fetch accommodation types.', 'bookiflex'));
        }
    }

    /**
     * Check if current user has access to the endpoint
     */
    public function hasAccess(): bool|WP_Error
    {
        if (!current_user_can(RoleCapabilitiesInterface::MANAGE_CONFIGURATION)) {
            return $this->error(
                'bookiflex_forbidden',
                __('You are not allowed to manage configuration.', 'bookiflex'),
                401
            );
        }

        return true;
    }
}
