<?php
declare(strict_types=1);

namespace Instinctor\Core;

if (!defined('ABSPATH')) { exit; }

/**
 * Directory trade taxonomy — ONE registry, in code, same philosophy as
 * Verticals: adding a trade is a commit, not a data row.
 *
 * The array key is the public URL segment (/directory/<trade>/<city-slug>),
 * so it is plural, lowercase, hyphenated, and PERMANENT once a category page
 * is indexed (never change slugs on indexed pages). 'vertical' ties a trade
 * back to the Verticals registry key so tenant listings can classify
 * automatically (trade keyword -> Verticals family -> vertical -> trade);
 * NULL means no builder vertical exists for that trade yet.
 */
final class DirectoryTrades
{
    /** Trade with no better match, and the safety net for bad URLs. */
    public const FALLBACK = 'other';

    private const REG = [
        'salons'      => ['singular' => 'Salon',              'plural' => 'Salons',               'vertical' => 'salon'],
        'installers'  => ['singular' => 'Installer',          'plural' => 'Installers',           'vertical' => 'installer'],
        'instructors' => ['singular' => 'Instructor',         'plural' => 'Instructors',          'vertical' => 'instructor'],
        'auto-shops'  => ['singular' => 'Auto shop',          'plural' => 'Auto shops',           'vertical' => 'ingine'],
        'real-estate' => ['singular' => 'Real estate agency', 'plural' => 'Real estate agencies', 'vertical' => 'instate'],
        'restaurants' => ['singular' => 'Restaurant',         'plural' => 'Restaurants',          'vertical' => null],
        'chauffeurs'      => ['singular' => 'Chauffeur service', 'plural' => 'Chauffeur services', 'vertical' => null],
        'freight-brokers' => ['singular' => 'Freight broker',    'plural' => 'Freight brokers',    'vertical' => null],
        'entertainers'    => ['singular' => 'Entertainer',       'plural' => 'Entertainers',       'vertical' => null],
        'vending'          => ['singular' => 'Vending service',         'plural' => 'Vending services',         'vertical' => null],
        'video-production' => ['singular' => 'Video production studio', 'plural' => 'Video production studios', 'vertical' => null],
        'other'       => ['singular' => 'Business',           'plural' => 'Local businesses',     'vertical' => 'generic'],
    ];

    /** @return array<string,array{singular:string,plural:string,vertical:?string}> */
    public static function all(): array
    {
        return self::REG;
    }

    public static function has(string $slug): bool
    {
        return isset(self::REG[$slug]);
    }

    public static function singular(string $slug): string
    {
        return self::REG[$slug]['singular'] ?? self::REG[self::FALLBACK]['singular'];
    }

    public static function plural(string $slug): string
    {
        return self::REG[$slug]['plural'] ?? self::REG[self::FALLBACK]['plural'];
    }

    /** Trade slug for a Verticals registry key ('salon' -> 'salons'). */
    public static function forVertical(string $vertical): string
    {
        foreach (self::REG as $slug => $def) {
            if (($def['vertical'] ?? null) === $vertical) return $slug;
        }
        return self::FALLBACK;
    }
}
