<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Api\Controllers;

use APD\Api\RestNamespace;
use APD\Support\AssetCatalog;
use WP_REST_Response;

defined('ABSPATH') || exit;

class AssetController
{
    public function registerRoutes() : void
    {
        register_rest_route(RestNamespace::NAME, '/assets/designs', [
            'methods' => 'GET',
            'permission_callback' => '__return_true',
            'callback' => fn() => $this->collection('designs'),
        ]);

        register_rest_route(RestNamespace::NAME, '/assets/colors', [
            'methods' => 'GET',
            'permission_callback' => '__return_true',
            'callback' => fn() => $this->collection('colors'),
        ]);

        register_rest_route(RestNamespace::NAME, '/assets/fonts', [
            'methods' => 'GET',
            'permission_callback' => '__return_true',
            'callback' => fn() => $this->collection('fonts'),
        ]);

        register_rest_route(RestNamespace::NAME, '/assets/printing-methods', [
            'methods' => 'GET',
            'permission_callback' => '__return_true',
            'callback' => fn() => $this->collection('printingMethods'),
        ]);

        register_rest_route(RestNamespace::NAME, '/assets/shapes', [
            'methods' => 'GET',
            'permission_callback' => '__return_true',
            'callback' => fn() => $this->collection('shapes'),
        ]);
    }

    private function collection(string $key) : WP_REST_Response
    {
        // These catalogs are storefront content. buildPayload() removes disabled
        // entries and normalizes each item before it reaches a public response.
        $catalog = (new AssetCatalog())->buildPayload();

        return new WP_REST_Response([
            'success' => true,
            'items' => is_array($catalog[$key] ?? null) ? $catalog[$key] : [],
        ]);
    }
}
