<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }

/**
 * Base Factory Class for CPT Registration
 *
 * This abstract class provides a reusable foundation for registering custom post types
 * and their associated taxonomies. Child classes only need to define configuration properties.
 *
 * @package Sifency_Addons
 * @subpackage Theme_Builder
 * @abstract
 */
abstract class Sifency_CPT_Factory_Base {

	/**
	 * Post type slug (e.g., 'service', 'portfolio')
	 *
	 * @var string
	 */
	protected $post_type;

	/**
	 * Singular name for the post type (e.g., 'Service')
	 *
	 * @var string
	 */
	protected $singular_name;

	/**
	 * Plural name for the post type (e.g., 'Services')
	 *
	 * @var string
	 */
	protected $plural_name;

	/**
	 * Option key for custom slug (e.g., 'service_slug')
	 *
	 * @var string
	 */
	protected $option_slug_key;

	/**
	 * Default slug if no custom slug is set
	 *
	 * @var string
	 */
	protected $default_slug;

	/**
	 * Dashicons icon for post type menu (e.g., 'dashicons-hammer')
	 *
	 * @var string
	 */
	protected $menu_icon;

	/**
	 * WordPress options key where custom slugs are stored
	 *
	 * @var string
	 */
	protected $option_key = 'sifency-options';

	/**
	 * Minimum capability to manage this post type
	 *
	 * @var string
	 */
	protected $min_capability = 'manage_options';

	/**
	 * Taxonomy slug (leave empty to skip taxonomy registration)
	 *
	 * @var string
	 */
	protected $taxonomy_name = '';

	/**
	 * Singular name for taxonomy
	 *
	 * @var string
	 */
	protected $taxonomy_singular = '';

	/**
	 * Plural name for taxonomy
	 *
	 * @var string
	 */
	protected $taxonomy_plural = '';

	/**
	 * Supported features for this post type
	 *
	 * @var array
	 */
	protected $supports = array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' );

	/**
	 * Initialize CPT registration
	 */
	public function __construct() {
		if ( class_exists( 'Sifency_Addons_Theme_Builder_Master' )
			&& Sifency_Addons_Theme_Builder_Master::sync_environment_state() ) {
			if ( did_action( 'init' ) ) {
				$this->register();
			} else {
				add_action( 'init', array( $this, 'register' ) );
			}
		}
	}

	/**
	 * Register both post type and taxonomy
	 */
	public function register() {
		$this->register_post_type();
		if ( ! empty( $this->taxonomy_name ) ) {
			$this->register_taxonomy();
		}
	}

	/**
	 * Register custom post type
	 */
	protected function register_post_type() {
		$slug   = $this->get_slug();
		$labels = $this->get_post_type_labels();
		$args   = $this->get_post_type_args( $slug, $labels );
		register_post_type( $this->post_type, $args );
	}

	/**
	 * Get post type slug with option override support
	 *
	 * @return string
	 */
	protected function get_slug() {
		$options = get_option( $this->option_key, array() );
		return ! empty( $options[ $this->option_slug_key ] )
			? sanitize_title( $options[ $this->option_slug_key ] )
			: $this->default_slug;
	}

	/**
	 * Get post type labels array
	 *
	 * @return array
	 */
	protected function get_post_type_labels() {
		return array(
			'name'               => $this->plural_name,
			'singular_name'      => $this->singular_name,
			'add_new'            => __( 'Add New', 'sifency-addons' ),
			/* translators: %s: Post type singular name */
			'add_new_item'       => sprintf( __( 'Add New %s', 'sifency-addons' ), $this->singular_name ),
			/* translators: %s: Post type singular name */
			'edit_item'          => sprintf( __( 'Edit %s', 'sifency-addons' ), $this->singular_name ),
			/* translators: %s: Post type singular name */
			'new_item'           => sprintf( __( 'New %s', 'sifency-addons' ), $this->singular_name ),
			/* translators: %s: Post type singular name */
			'view_item'          => sprintf( __( 'View %s', 'sifency-addons' ), $this->singular_name ),
			/* translators: %s: Post type plural name */
			'search_items'       => sprintf( __( 'Search %s', 'sifency-addons' ), $this->plural_name ),
			/* translators: %s: Post type plural name */
			'not_found'          => sprintf( __( 'No %s found', 'sifency-addons' ), strtolower( $this->plural_name ) ),
			/* translators: %s: Post type plural name */
			'not_found_in_trash' => sprintf( __( 'No %s found in trash', 'sifency-addons' ), strtolower( $this->plural_name ) ),
			'menu_name'          => $this->plural_name,
		);
	}

	/**
	 * Get post type registration arguments
	 *
	 * @param string $slug Post type slug
	 * @param array  $labels Post type labels
	 * @return array
	 */
	protected function get_post_type_args( $slug, $labels ) {
		return array(
			'labels'          => $labels,
			'public'          => true,
			'has_archive'     => true,
			'menu_icon'       => $this->menu_icon,
			'show_in_menu'    => true,
			'show_in_rest'    => true,
			'supports'        => $this->supports,
			'rewrite'         => array(
				'slug'       => $slug,
				'with_front' => false,
			),
			'capability_type' => 'post',
			'map_meta_cap'    => true,
		);
	}

	/**
	 * Register taxonomy for this post type
	 */
	protected function register_taxonomy() {
		if ( empty( $this->taxonomy_name ) ) {
			return;
		}

		$labels = array(
			'name'          => $this->taxonomy_plural,
			'singular_name' => $this->taxonomy_singular,
			/* translators: %s: Taxonomy plural name */
			'search_items'  => sprintf( __( 'Search %s', 'sifency-addons' ), $this->taxonomy_plural ),
			/* translators: %s: Taxonomy plural name */
			'all_items'     => sprintf( __( 'All %s', 'sifency-addons' ), $this->taxonomy_plural ),
			/* translators: %s: Taxonomy singular name */
			'edit_item'     => sprintf( __( 'Edit %s', 'sifency-addons' ), $this->taxonomy_singular ),
			/* translators: %s: Taxonomy singular name */
			'update_item'   => sprintf( __( 'Update %s', 'sifency-addons' ), $this->taxonomy_singular ),
			/* translators: %s: Taxonomy singular name */
			'add_new_item'  => sprintf( __( 'Add New %s', 'sifency-addons' ), $this->taxonomy_singular ),
			/* translators: %s: Taxonomy singular name */
			'new_item_name' => sprintf( __( 'New %s Name', 'sifency-addons' ), $this->taxonomy_singular ),
			'menu_name'     => $this->taxonomy_plural,
		);

		register_taxonomy(
			$this->taxonomy_name,
			array( $this->post_type ),
			array(
				'labels'            => $labels,
				'hierarchical'      => true,
				'public'            => true,
				'show_in_rest'      => true,
				'show_admin_column' => true,
			)
		);
	}
}
