<?php
/**
 * Template Registry - Singleton registry for all Palace Properties templates.
 *
 * @package Integrate_Palace_Properties
 * @since   1.0.0
 */

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

class IPP_Template_Registry {

    /**
     * Singleton instance.
     *
     * @var IPP_Template_Registry|null
     */
    private static $instance = null;

    /**
     * Registered templates.
     *
     * @var array
     */
    private $templates = [];

    /**
     * Get the singleton instance.
     *
     * @return IPP_Template_Registry
     */
    public static function instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Private constructor - registers default templates.
     */
    private function __construct() {
        $this->register_defaults();
    }

    /**
     * Prevent cloning.
     */
    private function __clone() {}

    /**
     * Prevent unserialization.
     */
    public function __wakeup() {
        throw new \Exception( 'Cannot unserialize singleton.' );
    }

    /**
     * Register the default set of templates.
     *
     * @return void
     */
    private function register_defaults() {

        $this->register( 'palace-property', [
            'name'        => __( 'Palace Property', 'integrate-palace-properties' ),
            'description' => __( 'Clean modern design with balanced layout. The default template shipped with the plugin.', 'integrate-palace-properties' ),
            'screenshot'  => JEstarter_STARTER_DIR_URL . 'public/assets/img/templates/palace-property.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => true,
        ] );

        $this->register( 'classic', [
            'name'        => __( 'Classic', 'integrate-palace-properties' ),
            'description' => __( 'Traditional real estate listing layout with sidebar.', 'integrate-palace-properties' ),
            'screenshot'  => JESTART_STARTER_DIR_URL . 'public/assets/img/templates/classic.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );

        $this->register( 'modern', [
            'name'        => __( 'Modern', 'integrate-palace-properties' ),
            'description' => __( 'Bold, image-forward design with full-width gallery.', 'integrate-palace-properties' ),
            'screenshot'  => JESTART_STARTER_DIR_URL . 'public/assets/img/templates/modern.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );

        $this->register( 'minimal', [
            'name'        => __( 'Minimal', 'integrate-palace-properties' ),
            'description' => __( 'Typography-focused, elegant professional look.', 'integrate-palace-properties' ),
            'screenshot'  => JESTART_STARTER_DIR_URL . 'public/assets/img/templates/minimal.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );

        $this->register( 'grid-gallery', [
            'name'        => __( 'Grid Gallery', 'integrate-palace-properties' ),
            'description' => __( 'Masonry archive grid with photography emphasis.', 'integrate-palace-properties' ),
            'screenshot'  => JESTART_STARTER_DIR_URL . 'public/assets/img/templates/grid-gallery.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );

        $this->register( 'developer', [
            'name'        => __( 'Developer', 'integrate-palace-properties' ),
            'description' => __( 'Barebone semantic HTML, intended for theme developers to override.', 'integrate-palace-properties' ),
            'screenshot'  => JESTART_STARTER_DIR_URL . 'public/assets/img/templates/developer.png',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );
    }

    /**
     * Register a template.
     *
     * @param string $slug   Unique template slug.
     * @param array  $config {
     *     Template configuration.
     *
     *     @type string   $name        Human-readable name.
     *     @type string   $description Short description.
     *     @type string   $screenshot  URL to a screenshot image.
     *     @type string[] $supports    Contexts the template supports (archive, single, search).
     *     @type bool     $default     Whether this is the default template.
     * }
     * @return void
     */
    public function register( $slug, $config ) {
        $this->templates[ sanitize_key( $slug ) ] = wp_parse_args( $config, [
            'name'        => '',
            'description' => '',
            'screenshot'  => '',
            'supports'    => [ 'archive', 'single', 'search' ],
            'default'     => false,
        ] );
    }

    /**
     * Get a single template config by slug.
     *
     * @param string $slug Template slug.
     * @return array|null Template config or null if not found.
     */
    public function get( $slug ) {
        $slug = sanitize_key( $slug );
        return isset( $this->templates[ $slug ] ) ? $this->templates[ $slug ] : null;
    }

    /**
     * Get all registered templates.
     *
     * @return array Associative array of slug => config.
     */
    public function get_all() {
        return $this->templates;
    }

    /**
     * Get the slug of the currently active template.
     *
     * @return string Active template slug.
     */
    public function get_active() {
        return sanitize_text_field( get_option( 'ipp_select_template', 'palace-property' ) );
    }

    /**
     * Get the full config for the active template.
     *
     * @return array|null Active template config or null if not found.
     */
    public function get_active_config() {
        return $this->get( $this->get_active() );
    }

    /**
     * Check whether a template supports a given context.
     *
     * @param string $slug    Template slug.
     * @param string $context Context to check (archive, single, search).
     * @return bool
     */
    public function supports( $slug, $context ) {
        $template = $this->get( $slug );
        if ( ! $template ) {
            return false;
        }
        return in_array( $context, $template['supports'], true );
    }

    /**
     * Unregister a template (useful for replacing defaults).
     *
     * @param string $slug Template slug.
     * @return void
     */
    public function unregister( $slug ) {
        unset( $this->templates[ sanitize_key( $slug ) ] );
    }
}
