<?php

declare(strict_types=1);

namespace CliqThemes\StoreLocator\Layouts;

defined('ABSPATH') || exit;

/**
 * The canonical locator this plugin ships: a fixed, read-only definition that
 * {@see BundledLocatorBuilder} shapes with the site's settings.
 *
 * Reserved negative IDs cannot collide with database-backed designs and
 * still work with the existing integer-based frontend/SSR design contracts.
 */
final class BundledLocatorPreset
{
    public const TEMPLATE_ID = -1;
    public const CARD_ID = -101;
    public const INFOBOX_ID = -102;
    public const SEARCH_ID = -103;
    public const DETAIL_ID = -104;
    public const KEY = 'standard';

    /** @return array<string, mixed> */
    public static function definition(): array
    {
        return [
            'key' => self::KEY,
            'id' => self::TEMPLATE_ID,
            'name' => __('Standard locator', 'cliq-store-locator'),
            'model' => self::locatorModel(),
            'designs' => [
                (string) self::CARD_ID => [
                    'id' => self::CARD_ID,
                    'purpose' => 'list_item',
                    'name' => __('Standard store card', 'cliq-store-locator'),
                    'model' => self::cardModel(),
                ],
                (string) self::INFOBOX_ID => [
                    'id' => self::INFOBOX_ID,
                    'purpose' => 'infobox',
                    'name' => __('Standard map infobox', 'cliq-store-locator'),
                    'model' => self::infoboxModel(),
                ],
                (string) self::SEARCH_ID => [
                    'id' => self::SEARCH_ID,
                    'purpose' => 'search',
                    'name' => __('Standard search form', 'cliq-store-locator'),
                    'model' => self::searchModel(),
                ],
                (string) self::DETAIL_ID => [
                    'id' => self::DETAIL_ID,
                    'purpose' => 'detail',
                    'name' => __('Standard store detail', 'cliq-store-locator'),
                    'model' => self::detailModel(),
                ],
            ],
        ];
    }

    /** @return array<string, mixed> */
    private static function locatorModel(): array
    {
        return [
            'rows' => [
                [
                    'id' => 'locator-search-row',
                    'settings' => ['gap' => 12],
                    'columns' => [[
                        'id' => 'locator-search-column',
                        'span' => 12,
                        'items' => [[
                            'id' => 'locator-search',
                            'type' => 'widget',
                            'widget' => 'search',
                            'colSpan' => 12,
                            'widgetSettings' => ['searchFormId' => self::SEARCH_ID],
                        ]],
                    ]],
                ],
                [
                    'id' => 'locator-content-row',
                    'settings' => [
                        'gap' => 16,
                        'equalColumnHeight' => true,
                        'equalColumnHeightValue' => '560px',
                    ],
                    'columns' => [
                        [
                            'id' => 'locator-results-column',
                            'span' => 5,
                            'spanMobile' => 12,
                            'items' => [
                                [
                                    'id' => 'locator-count',
                                    'type' => 'widget',
                                    'widget' => 'count',
                                    'colSpan' => 12,
                                    // Small breathing room so the count doesn't sit
                                    // flush against the column/canvas edge and lines
                                    // up with the padded store cards below it.
                                    'css' => ['padding' => '4px 6px'],
                                    'widgetSettings' => [
                                        'label' => __('Stores', 'cliq-store-locator'),
                                        'showPrint' => false,
                                    ],
                                ],
                                [
                                    'id' => 'locator-results',
                                    'type' => 'widget',
                                    'widget' => 'results',
                                    'colSpan' => 12,
                                    'widgetSettings' => [
                                        'listItemDesignId' => self::CARD_ID,
                                        'displayMode' => 'list',
                                        'layout' => 'flex',
                                        'direction' => 'column',
                                        'scroll' => true,
                                        'pageSize' => 20,
                                        'statuses' => ['active'],
                                        'emptyText' => __('No stores found.', 'cliq-store-locator'),
                                    ],
                                ],
                            ],
                        ],
                        [
                            'id' => 'locator-map-column',
                            'span' => 7,
                            'spanMobile' => 12,
                            'items' => [[
                                'id' => 'locator-map',
                                'type' => 'widget',
                                'widget' => 'map',
                                'colSpan' => 12,
                                'widgetSettings' => [
                                    'infoboxDesignId' => self::INFOBOX_ID,
                                    'height' => 560,
                                    'clustering' => true,
                                    'zoom' => 5,
                                ],
                            ]],
                        ],
                    ],
                ],
            ],
        ];
    }

    /** @return array<string, mixed> */
    private static function cardModel(): array
    {
        return [
            'container' => ['css' => [
                'background' => 'var(--cliq-sl-surface)',
                'borderRadius' => '12px',
                'padding' => '16px',
                'borderWidth' => '1px',
                'borderColor' => 'var(--cliq-sl-border-soft)',
            ]],
            'rows' => [[
                'id' => 'locator-card-row',
                'settings' => ['gap' => 8],
                'columns' => [[
                    'id' => 'locator-card-column',
                    'span' => 12,
                    'items' => [
                        self::field('locator-card-name', 'name', [
                            'fontSize' => '18px',
                            'fontWeight' => '700',
                            'color' => 'var(--cliq-sl-heading)',
                        ]),
                        self::field('locator-card-address', 'address', [
                            'color' => 'var(--cliq-sl-muted)',
                            'fontSize' => '13px',
                        ], 'map-pin'),
                        self::field('locator-card-distance', 'distance', [
                            'color' => 'var(--cliq-sl-primary)',
                            'fontSize' => '12px',
                            'fontWeight' => '600',
                        ]),
                        self::field('locator-card-details', 'details', [
                            'fit' => 'auto',
                            'background' => 'var(--cliq-sl-primary)',
                            'color' => 'var(--cliq-sl-on-primary)',
                            'borderRadius' => '8px',
                            'padding' => '8px 14px',
                            'fontWeight' => '600',
                        ]),
                    ],
                ]],
            ]],
        ];
    }

    /** @return array<string, mixed> */
    private static function infoboxModel(): array
    {
        return [
            'container' => ['css' => [
                'background' => 'var(--cliq-sl-surface)',
                'borderRadius' => '10px',
                'padding' => '12px',
                'maxWidth' => '280px',
            ]],
            'rows' => [[
                'id' => 'locator-infobox-row',
                'settings' => ['gap' => 6],
                'columns' => [[
                    'id' => 'locator-infobox-column',
                    'span' => 12,
                    'items' => [
                        self::field('locator-infobox-name', 'name', [
                            'fontSize' => '15px',
                            'fontWeight' => '700',
                            'color' => 'var(--cliq-sl-heading)',
                        ]),
                        self::field('locator-infobox-address', 'address', [
                            'fontSize' => '12px',
                            'color' => 'var(--cliq-sl-muted)',
                        ], 'map-pin'),
                        self::field('locator-infobox-directions', 'directions', [
                            'fit' => 'auto',
                            'background' => 'var(--cliq-sl-primary)',
                            'color' => 'var(--cliq-sl-on-primary)',
                            'borderRadius' => '7px',
                            'padding' => '7px 12px',
                            'fontWeight' => '600',
                        ]),
                    ],
                ]],
            ]],
        ];
    }

    /** @return array<string, mixed> */
    private static function searchModel(): array
    {
        return [
            'rows' => [[
                'id' => 'locator-form-row',
                'settings' => ['gap' => 12, 'align' => 'flex-end'],
                'columns' => [
                    [
                        'id' => 'locator-location-column',
                        'span' => 5,
                        'spanMobile' => 12,
                        'items' => [[
                            'id' => 'locator-location',
                            'type' => 'text',
                            'name' => 'address',
                            'label' => __('Location', 'cliq-store-locator'),
                            'placeholder' => __('City, postcode or address', 'cliq-store-locator'),
                            'mapTo' => 'location',
                            'colSpan' => 12,
                            'css' => [],
                        ]],
                    ],
                    [
                        'id' => 'locator-category-column',
                        'span' => 3,
                        'spanMobile' => 12,
                        'items' => [[
                            'id' => 'locator-category',
                            'type' => 'select',
                            'name' => 'category',
                            'label' => __('Category', 'cliq-store-locator'),
                            'mapTo' => 'category',
                            'choicesSource' => 'categories',
                            'op' => 'IN',
                            'colSpan' => 12,
                            'css' => [],
                        ]],
                    ],
                    [
                        'id' => 'locator-radius-column',
                        'span' => 3,
                        'spanMobile' => 12,
                        'items' => [[
                            'id' => 'locator-radius',
                            'type' => 'select',
                            'name' => 'distance',
                            'label' => __('Distance', 'cliq-store-locator'),
                            'mapTo' => 'distance',
                            'choicesSource' => 'range',
                            'rangeMin' => 10,
                            'rangeMax' => 200,
                            'rangeStep' => 10,
                            'colSpan' => 12,
                            'css' => [],
                        ]],
                    ],
                    [
                        'id' => 'locator-submit-column',
                        'span' => 1,
                        'spanMobile' => 12,
                        'items' => [[
                            'id' => 'locator-submit',
                            'type' => 'submit',
                            'text' => __('Search', 'cliq-store-locator'),
                            'colSpan' => 12,
                            'css' => [
                                'background' => 'var(--cliq-sl-primary)',
                                'color' => 'var(--cliq-sl-on-primary)',
                                'borderRadius' => '8px',
                                'fontWeight' => '600',
                            ],
                        ]],
                    ],
                ],
            ]],
        ];
    }

    /**
     * The bundled store-detail layout.
     *
     * Deliberately no gallery token: a gallery is a field a feature adds, and
     * this layout has to render on any install.
     *
     * @return array<string, mixed>
     */
    private static function detailModel(): array
    {
        return [
            // The store detail is a full page, not a card in a list. The outer
            // wrapper stays a plain layout container — no frame (border) or
            // surface fill imposed here; individual fields/cards keep their own
            // authored styling below.
            'container' => ['css' => [
                'padding' => '0',
            ]],
            'rows' => [[
                'id' => 'locator-detail-row',
                'settings' => ['gap' => 28, 'align' => 'start'],
                'columns' => [
                    [
                        'id' => 'locator-detail-media',
                        'span' => 5,
                        'spanMobile' => 12,
                        'items' => [
                            self::field('locator-detail-hero', 'logo', [
                                'width' => '100%',
                                'height' => '360px',
                                'objectFit' => 'cover',
                                'borderRadius' => '12px',
                            ]),
                        ],
                    ],
                    [
                        'id' => 'locator-detail-content',
                        'span' => 7,
                        'spanMobile' => 12,
                        'settings' => ['gap' => 12],
                        'items' => [
                            self::field('locator-detail-name', 'name', [
                                'fontSize' => '32px',
                                'fontWeight' => '700',
                                'color' => 'var(--cliq-sl-heading)',
                            ]),
                            self::field('locator-detail-address', 'address', [
                                'fontSize' => '15px',
                                'color' => 'var(--cliq-sl-text-soft)',
                            ], 'map-pin'),
                            self::field('locator-detail-description', 'description', [
                                'fontSize' => '15px',
                                'color' => 'var(--cliq-sl-text)',
                            ]),
                            self::field('locator-detail-phone', 'phone', [
                                'color' => 'var(--cliq-sl-text)',
                            ], 'phone'),
                            self::field('locator-detail-email', 'email', [
                                'color' => 'var(--cliq-sl-primary)',
                            ], 'mail'),
                            self::field('locator-detail-website', 'website', [
                                'color' => 'var(--cliq-sl-primary)',
                            ], 'globe'),
                            self::field('locator-detail-hours', 'opening_hours', [
                                'color' => 'var(--cliq-sl-text)',
                            ], 'clock'),
                        ],
                    ],
                ],
            ]],
        ];
    }

    /** @return array<string, mixed> */
    private static function field(string $id, string $token, array $css, string $icon = ''): array
    {
        $field = [
            'id' => $id,
            'type' => 'field',
            'token' => $token,
            'colSpan' => 12,
            'css' => $css,
        ];
        if ($icon !== '') {
            $field['icon'] = $icon;
        }
        if ($token === 'details') {
            $field['buttonLabel'] = __('View More', 'cliq-store-locator');
        }

        return $field;
    }
}
