<?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 ShapesPage
{
    use CatalogPageTrait;

    private function columns() : array
    {
        return [
            'id' => ['label' => __('ID', 'asuka-product-designer'), 'placeholder' => 'rounded-badge'],
            'label' => ['label' => __('Label', 'asuka-product-designer'), 'placeholder' => __('Rounded badge', 'asuka-product-designer')],
            'category' => ['label' => __('Category', 'asuka-product-designer'), 'placeholder' => __('Badges', 'asuka-product-designer')],
            'type' => ['label' => __('Type', 'asuka-product-designer'), 'type' => 'select', 'options' => [
                'svg' => __('SVG shape', 'asuka-product-designer'),
                'rect' => __('Rectangle preset', 'asuka-product-designer'),
                'circle' => __('Circle preset', 'asuka-product-designer'),
            ]],
            'viewBox' => ['label' => __('ViewBox', 'asuka-product-designer'), 'placeholder' => '0 0 100 100'],
            'fill' => ['label' => __('Default fill', 'asuka-product-designer'), 'placeholder' => '#111827'],
            'stroke' => ['label' => __('Default stroke', 'asuka-product-designer'), 'placeholder' => '#000000'],
            'price' => ['label' => __('Extra price', 'asuka-product-designer'), 'placeholder' => '0'],
            'sourceUrl' => ['label' => __('Source URL', 'asuka-product-designer'), 'placeholder' => 'https://…/shape.svg', 'media' => 'file'],
            'svgPath' => ['label' => __('SVG path', 'asuka-product-designer'), 'type' => 'textarea', 'full' => true],
            'enabled' => ['label' => __('Visibility', 'asuka-product-designer'), 'type' => 'checkbox', 'checkbox_label' => __('Expose this shape', 'asuka-product-designer')],
        ];
    }

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

    public function render() : void
    {
        $catalog = (new AssetCatalog())->all();
        $this->pageShellStart(
            __('Reusable vector elements', 'asuka-product-designer'),
            __('Shapes library', 'asuka-product-designer'),
            __('Manage reusable shapes or SVG paths that can be exposed selectively inside each custom product.', 'asuka-product-designer')
        );
        $this->renderCatalogNotice();

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

        $this->pageShellEnd();
    }

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

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

        $this->pageShellEnd();
    }
}
