<?php
/**
 * Library category taxonomy (hierarchical).
 *
 * Leaf categories are what templates reference via block.json `category`.
 * Hero variants (Simple, Image Left, …) stay as template `variant` folders.
 *
 * @package JOCBM
 */

namespace JOCBM\Database;

defined( 'ABSPATH' ) || exit;

/**
 * Class CategoryTaxonomy
 */
final class CategoryTaxonomy {

	/**
	 * Flat list of categories for seeding (parents first).
	 *
	 * @return array<int, array<string, mixed>>
	 */
	public static function definitions(): array {
		$groups = self::tree();
		$flat   = array();
		$order  = 10;

		foreach ( $groups as $group ) {
			$flat[] = array(
				'slug'        => $group['slug'],
				'name'        => $group['name'],
				'description' => $group['description'] ?? '',
				'parent_slug' => '',
				'is_pro'      => ! empty( $group['is_pro'] ) ? 1 : 0,
				'sort_order'  => $order,
			);
			$order += 10;

			$child_order = 10;
			foreach ( $group['children'] as $child ) {
				$flat[] = array(
					'slug'        => $child['slug'],
					'name'        => $child['name'],
					'description' => $child['description'] ?? '',
					'parent_slug' => $group['slug'],
					'is_pro'      => ! empty( $group['is_pro'] ) || ! empty( $child['is_pro'] ) ? 1 : 0,
					'sort_order'  => $child_order,
				);
				$child_order += 10;
			}
		}

		return $flat;
	}

	/**
	 * Hierarchical taxonomy for docs / UI.
	 *
	 * @return array<int, array<string, mixed>>
	 */
	public static function tree(): array {
		return array(
			array(
				'slug'        => 'website-sections',
				'name'        => 'Website Sections',
				'description' => 'Core page sections for marketing sites',
				'children'    => array(
					array( 'slug' => 'hero', 'name' => 'Hero', 'description' => 'Above-the-fold heroes (Simple, Split, Video, …)' ),
					array( 'slug' => 'header', 'name' => 'Header', 'description' => 'Site headers and navigation bands' ),
					array( 'slug' => 'about', 'name' => 'About', 'description' => 'About and story sections' ),
					array( 'slug' => 'features', 'name' => 'Features', 'description' => 'Feature highlights and benefit grids' ),
					array( 'slug' => 'services', 'name' => 'Services', 'description' => 'Service offerings' ),
					array( 'slug' => 'cta', 'name' => 'CTA', 'description' => 'Call-to-action bands' ),
					array( 'slug' => 'testimonials', 'name' => 'Testimonials', 'description' => 'Customer quotes and reviews' ),
					array( 'slug' => 'faq', 'name' => 'FAQ', 'description' => 'Frequently asked questions' ),
					array( 'slug' => 'contact', 'name' => 'Contact', 'description' => 'Contact and inquiry sections' ),
					array( 'slug' => 'footer', 'name' => 'Footer', 'description' => 'Site footers' ),
				),
			),
			array(
				'slug'        => 'business',
				'name'        => 'Business',
				'description' => 'Company and growth sections',
				'children'    => array(
					array( 'slug' => 'team', 'name' => 'Team' ),
					array( 'slug' => 'pricing', 'name' => 'Pricing' ),
					array( 'slug' => 'process', 'name' => 'Process' ),
					array( 'slug' => 'stats', 'name' => 'Stats' ),
					array( 'slug' => 'logo-clouds', 'name' => 'Logo Clouds' ),
					array( 'slug' => 'portfolio', 'name' => 'Portfolio' ),
					array( 'slug' => 'case-studies', 'name' => 'Case Studies' ),
				),
			),
			array(
				'slug'        => 'marketing',
				'name'        => 'Marketing',
				'description' => 'Conversion and trust sections',
				'children'    => array(
					array( 'slug' => 'lead-generation', 'name' => 'Lead Generation' ),
					array( 'slug' => 'trust', 'name' => 'Trust' ),
					array( 'slug' => 'countdown', 'name' => 'Countdown' ),
					array( 'slug' => 'comparison', 'name' => 'Comparison' ),
					array( 'slug' => 'social-proof', 'name' => 'Social Proof' ),
				),
			),
			array(
				'slug'        => 'content',
				'name'        => 'Content',
				'description' => 'Editorial and content blocks',
				'children'    => array(
					array( 'slug' => 'blog', 'name' => 'Blog' ),
					array( 'slug' => 'quotes', 'name' => 'Quotes' ),
					array( 'slug' => 'tables', 'name' => 'Tables' ),
					array( 'slug' => 'lists', 'name' => 'Lists' ),
					array( 'slug' => 'callouts', 'name' => 'Callouts' ),
				),
			),
			array(
				'slug'        => 'media',
				'name'        => 'Media',
				'description' => 'Image, video, and gallery blocks',
				'children'    => array(
					array( 'slug' => 'images', 'name' => 'Images' ),
					array( 'slug' => 'videos', 'name' => 'Videos' ),
					array( 'slug' => 'gallery', 'name' => 'Gallery' ),
					array( 'slug' => 'sliders', 'name' => 'Sliders' ),
				),
			),
			array(
				'slug'        => 'components',
				'name'        => 'Components',
				'description' => 'Reusable UI components',
				'children'    => array(
					array( 'slug' => 'buttons', 'name' => 'Buttons' ),
					array( 'slug' => 'cards', 'name' => 'Cards' ),
					array( 'slug' => 'alerts', 'name' => 'Alerts' ),
					array( 'slug' => 'badges', 'name' => 'Badges' ),
					array( 'slug' => 'tabs', 'name' => 'Tabs' ),
					array( 'slug' => 'accordion', 'name' => 'Accordion' ),
					array( 'slug' => 'timeline', 'name' => 'Timeline' ),
					array( 'slug' => 'modals', 'name' => 'Modals' ),
				),
			),
			array(
				'slug'        => 'commerce',
				'name'        => 'Commerce',
				'description' => 'Store and product sections',
				'children'    => array(
					array( 'slug' => 'products', 'name' => 'Products' ),
					array( 'slug' => 'reviews', 'name' => 'Reviews' ),
					array( 'slug' => 'checkout-cta', 'name' => 'Checkout CTA' ),
					array( 'slug' => 'commerce-pricing', 'name' => 'Pricing' ),
				),
			),
			array(
				'slug'        => 'layouts',
				'name'        => 'Layouts',
				'description' => 'Structural layout helpers',
				'children'    => array(
					array( 'slug' => 'containers', 'name' => 'Containers' ),
					array( 'slug' => 'columns', 'name' => 'Columns' ),
					array( 'slug' => 'grid', 'name' => 'Grid' ),
					array( 'slug' => 'sections', 'name' => 'Sections' ),
					array( 'slug' => 'utilities', 'name' => 'Utilities' ),
				),
			),
			// Empty Pro-only category trees belong in a separate Pro extension
			// (Plugin Directory guidelines: no teased unavailable features).
		);
	}

	/**
	 * Legacy category slug → new leaf slug.
	 *
	 * @return array<string, string>
	 */
	public static function legacy_map(): array {
		return array(
			'testimonial'   => 'testimonials',
			'button'        => 'buttons',
			'two-columns'   => 'columns',
			'feature-cards' => 'cards',
			'image-banner'  => 'images',
			'video-embed'   => 'videos',
			'cards'         => 'cards',
			'images'        => 'images',
			'videos'        => 'videos',
			'hero'          => 'hero',
		);
	}

	/**
	 * Resolve a template category slug (applies legacy remaps).
	 *
	 * @param string $slug Raw category from block.json.
	 */
	public static function resolve( string $slug ): string {
		$slug = sanitize_key( $slug );
		$map  = self::legacy_map();
		return $map[ $slug ] ?? $slug;
	}
}
