<?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\Admin;

use APD\Support\AssetCatalog;

defined('ABSPATH') || exit;

class DesignsPage
{
    use CatalogPageTrait;

    private function columns() : array
    {
        return [
            'id' => ['label' => __('ID', 'asuka-product-designer'), 'placeholder' => 'flower-outline'],
            'label' => ['label' => __('Label', 'asuka-product-designer'), 'placeholder' => __('Flower outline', 'asuka-product-designer')],
            'category' => ['label' => __('Category', 'asuka-product-designer'), 'placeholder' => __('Nature', 'asuka-product-designer')],
            'price' => ['label' => __('Extra price', 'asuka-product-designer'), 'placeholder' => '0'],
            'url' => ['label' => __('Asset URL', 'asuka-product-designer'), 'placeholder' => 'https://…/design.png', 'media' => 'file'],
            'description' => ['label' => __('Description', 'asuka-product-designer'), 'type' => 'textarea', 'full' => true],
            'enabled' => ['label' => __('Visibility', 'asuka-product-designer'), 'type' => 'checkbox', 'checkbox_label' => __('Expose this design in the library', 'asuka-product-designer')],
        ];
    }

    private function defaults() : array
    {
        return [
            'label' => '',
            'price' => '0',
            'enabled' => true,
        ];
    }

    public function render() : void
    {
        $catalog = (new AssetCatalog())->all();
        $this->pageShellStart(
            __('Global catalog', 'asuka-product-designer'),
            __('Design library', 'asuka-product-designer'),
            __('Manage reusable visual designs that can later be exposed selectively inside each custom product.', 'asuka-product-designer')
        );
        $this->renderCatalogNotice();

        $this->renderCrudSection(
            'apd-designs',
            'designs',
            __('Reusable designs', 'asuka-product-designer'),
            __('Browse the library here, then open a dedicated page to add or edit one design at a time.', 'asuka-product-designer'),
            (array) ($catalog['designs'] ?? []),
            $this->columns(),
            $this->defaults(),
            'apd-designs-editor'
        );

        $this->pageShellEnd();
    }

    public function renderEditor() : void
    {
        $catalog = (new AssetCatalog())->all();
        $this->pageShellStart(
            __('Global catalog', 'asuka-product-designer'),
            __('Design editor', 'asuka-product-designer'),
            __('Create or update one reusable design at a time, then return to the library list.', 'asuka-product-designer')
        );
        $this->renderCatalogNotice();

        $this->renderCrudStandaloneEditor(
            'apd-designs',
            'apd-designs-editor',
            'designs',
            __('Reusable designs', 'asuka-product-designer'),
            __('Use this focused editor page for a single design entry.', 'asuka-product-designer'),
            (array) ($catalog['designs'] ?? []),
            $this->columns(),
            $this->defaults()
        );

        $this->pageShellEnd();
    }
}
