<?php
/**
 * Admin Page Factory
 *
 * @package Kutby
 * @since   1.0.0
 */

declare(strict_types=1);

namespace Kutby\Pages;

use Kutby\ThemeOptions\FieldRegistry;
use Kutby\ThemeOptions\Manager;
use Kutby\ThemeOptions\Registry;

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

/**
 * Class PageFactory
 */
final class PageFactory {

	/**
	 * Custom (non-schema) panels.
	 *
	 * @var array<string, class-string>
	 */
	private static array $custom = array(
		'dashboard'     => DashboardPage::class,
		'appearance'    => AppearancePage::class,
		'header'        => HeaderPage::class,
		'footer'        => FooterPage::class,
		'hero'          => HeroPage::class,
		'import_export' => ImportExportPage::class,
		'system'        => SystemPage::class,
	);

	/**
	 * @param string $panel Panel ID.
	 * @return void
	 */
	public static function render( string $panel ): void {
		$options = Manager::all();

		if ( isset( self::$custom[ $panel ] ) ) {
			$class = self::$custom[ $panel ];
			/** @var AbstractPage $page */
			$page = new $class();
			$page->render( $options );
			return;
		}

		$meta  = Registry::panel( $panel );
		$title = is_array( $meta ) ? (string) ( $meta['label'] ?? $panel ) : $panel;

		if ( empty( FieldRegistry::all()[ $panel ] ) ) {
			echo '<p>الصفحة غير موجودة.</p>';
			return;
		}

		( new SchemaDrivenPage( $panel, $title ) )->render( $options );
	}
}
