<?php
/**
 * Registers the 'sifency_template' CPT and manages its admin columns.
 *
 * This class is specifically for the Theme Builder template functionality.
 *
 * @package Sifency
 */

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

/**
 * Final Sifency_Template_CPT Class.
 *
 * Encapsulates the registration and admin UI for the 'sifency_template' post type.
 */
final class Sifency_Template_CPT {

	/**
	 * The single instance of the class.
	 *
	 * @var Sifency_Template_CPT|null
	 */
	private static $instance = null;

	/**
	 * Ensures only one instance of the class is loaded.
	 *
	 * @return Sifency_Template_CPT - The main instance.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Private constructor to prevent direct instantiation.
	 */
	private function __construct() {
		add_action( 'init', array( $this, 'register_template_cpt' ) );
		add_action( 'elementor/init', array( $this, 'add_elementor_support' ) );
	}

	/**
	 * Registers the 'sifency_template' Custom Post Type.
	 * It also hooks in the custom admin columns.
	 */
	public function register_template_cpt() {
		$labels = array(
			'name'          => esc_html__( 'Sifency Templates', 'sifency-addons' ),
			'singular_name' => esc_html__( 'Template', 'sifency-addons' ),
			'add_new_item'  => esc_html__( 'Add New Template', 'sifency-addons' ),
			'edit_item'     => esc_html__( 'Edit Template', 'sifency-addons' ),
			'all_items'     => esc_html__( 'All Templates', 'sifency-addons' ),
		);

		$args = array(
			'labels'              => $labels,
			'public'              => true,
			'publicly_queryable'  => current_user_can( 'edit_posts' ),
			'show_in_menu'        => 'sifency-welcome',
			'show_in_nav_menus'   => false,
			'show_in_rest'        => true,
			'supports'            => array( 'title', 'editor', 'elementor' ),
			'menu_icon'           => 'dashicons-layout',
			'has_archive'         => false,
			'exclude_from_search' => true,
		);

		register_post_type( 'sifency_template', $args );
		add_filter( 'manage_sifency_template_posts_columns', array( $this, 'add_template_columns' ) );
		add_action( 'manage_sifency_template_posts_custom_column', array( $this, 'render_template_column' ), 10, 2 );
	}

	/**
	 * Adds Elementor support specifically to the 'sifency_template' CPT.
	 */
	public function add_elementor_support() {
		add_post_type_support( 'sifency_template', 'elementor' );
	}

	/**
	 * Adds a custom 'Template Type' column to the admin list table.
	 */
	public function add_template_columns( $columns ) {
		// Add our column after the title column for a clean layout.
		$new_columns = array();
		foreach ( $columns as $key => $value ) {
			$new_columns[ $key ] = $value;
			if ( $key === 'title' ) {
				$new_columns['template_type'] = esc_html__( 'Template Type', 'sifency-addons' );
			}
		}
		return $new_columns;
	}

	/**
	 * Renders the content for the custom 'template_type' column.
	 * REFACTORED: Now uses sifency_get_field() for correct and fast data retrieval.
	 */
	public function render_template_column( $column, $post_id ) {
		if ( 'template_type' !== $column ) {
			return;
		}

		// Use your modern helper function to get the individual meta value.
		$template_type = sifency_get_field( 'sifency_template_type', $post_id );

		if ( empty( $template_type ) ) {
			echo '—'; // Display a dash if no type is set.
			return;
		}

		// A map for user-friendly labels and colors.
		$type_map = array(
			'header'           => array(
				'label' => 'Header',
				'color' => '#3568B7',
			),
			'footer'           => array(
				'label' => 'Footer',
				'color' => '#1DA851',
			),
			'404'              => array(
				'label' => '404 Page',
				'color' => '#D9534F',
			),
			'coming_soon'      => array(
				'label' => 'Coming Soon',
				'color' => '#8800d6',
			),
			'subheader'        => array(
				'label' => 'Subheader',
				'color' => '#5BC0DE',
			),
			'cta'              => array(
				'label' => 'CTA',
				'color' => '#5CB85C',
			),
			'single_team'      => array(
				'label' => 'Team Single',
				'color' => '#dc4a6c',
			),
			'single_career'    => array(
				'label' => 'Career Single',
				'color' => '#0B6484',
			),
			'single_service'   => array(
				'label' => 'Service Single',
				'color' => '#F0AD4E',
			),
			'single_portfolio' => array(
				'label' => 'Portfolio Single',
				'color' => '#af241f',
			),
			'single_event'     => array(
				'label' => 'Event Single',
				'color' => '#3b0f02',
			),
			'single_product'   => array(
				'label' => 'Product Single',
				'color' => '#873EFF',
			),
		);

		$label = $type_map[ $template_type ]['label'] ?? ucfirst( str_replace( '_', ' ', $template_type ) );
		$color = $type_map[ $template_type ]['color'] ?? '#777777';

		printf(
			'<span class="sifency-template-badge" style="display:inline-block; padding:4px 12px; background-color:%s; color:#fff; border-radius:3px; font-size:12px; font-weight:500;">%s</span>',
			esc_attr( $color ),
			esc_html( $label )
		);
	}
}

// Initialize the class.
Sifency_Template_CPT::instance();
