<?php
/**
 * Orientation read handler for Bricks overview.
 *
 * @package LolaCoreForBricks
 */

declare(strict_types=1);

namespace LolaCoreForBricks\Handlers;

use LolaCoreForBricks\BricksEnv;
use LolaCoreForBricks\Gateways\OrientationGateway;

/**
 * Executes get_bricks_overview read logic.
 */
class OrientationHandler
{
    public function __construct(
        private readonly ?OrientationGateway $gateway = null
    ) {
    }

    /**
     * @param array<string, mixed> $params
     * @return array<string, mixed>
     */
    public function getBricksOverview(array $params = []): array
    {
        unset($params);

        if (!BricksEnv::userCanReadDesignSystem()) {
            return [
                'success' => false,
                'message' => 'I need administrator access to read the Bricks design system overview.',
            ];
        }

        if (!BricksEnv::isAvailable() || !BricksEnv::meetsFloor()) {
            return [
                'success' => false,
                'message' => 'Bricks is not available on this site yet. Activate Bricks '
                    . BricksEnv::FLOOR_VERSION . ' or newer, then try again.',
            ];
        }

        $overview = ($this->gateway ?? new OrientationGateway())->getOverview();

        return array_merge(
            [
                'success' => true,
                'message' => 'Bricks overview is ready.',
            ],
            $overview
        );
    }
}
