<?php
/**
 * Registers the visualizer project custom post type and the
 * [tilvora id="<id>"] shortcode for embedding on any page.
 */

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

add_action( 'init', 'tilvora_register_cpt' );

function tilvora_register_cpt() {
	$labels = [
		'name'               => __( 'Visualizers',                           'tilvora-3d-flooring-visualizer' ),
		'singular_name'      => __( 'Visualizer',                            'tilvora-3d-flooring-visualizer' ),
		'add_new'            => __( 'Add New',                               'tilvora-3d-flooring-visualizer' ),
		'add_new_item'       => __( 'Add New Visualizer',                    'tilvora-3d-flooring-visualizer' ),
		'edit_item'          => __( 'Edit Visualizer',                       'tilvora-3d-flooring-visualizer' ),
		'new_item'           => __( 'New Visualizer',                        'tilvora-3d-flooring-visualizer' ),
		'view_item'          => __( 'View Visualizer',                       'tilvora-3d-flooring-visualizer' ),
		'search_items'       => __( 'Search Visualizers',                    'tilvora-3d-flooring-visualizer' ),
		'not_found'          => __( 'No visualizers found.',                 'tilvora-3d-flooring-visualizer' ),
		'not_found_in_trash' => __( 'No visualizers found in trash.',        'tilvora-3d-flooring-visualizer' ),
		'menu_name'          => __( 'Tilvora',                            'tilvora-3d-flooring-visualizer' ),
	];

	register_post_type( TILVORA_CPT, [
		'labels'              => $labels,
		'public'              => false,
		'publicly_queryable'  => false,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => false,
		'show_in_admin_bar'   => true,
		'query_var'           => false,
		'rewrite'             => false,
		'capability_type'     => 'post',
		'has_archive'         => false,
		'hierarchical'        => false,
		'menu_position'       => 56,
		'menu_icon'           => 'dashicons-grid-view',
		'supports'            => [ 'title' ],
	] );
}

/* ── [tilvora id="<id>" label="Design Your Floor"] shortcode ──── */
add_shortcode( 'tilvora', 'tilvora_shortcode' );

function tilvora_shortcode( $atts ) {
	$atts = shortcode_atts(
		[
			'id'    => 0,
			'label' => __( 'Design Your Floor', 'tilvora-3d-flooring-visualizer' ),
		],
		$atts,
		'tilvora'
	);

	$project_id = absint( $atts['id'] );
	if ( ! $project_id || ! tilvora_is_enabled_for_project( $project_id ) ) {
		return '';
	}

	$url = tilvora_get_configurator_url( $project_id );

	wp_register_style( 'tilvora-shortcode-btn', false, [], TILVORA_VERSION );
	wp_enqueue_style( 'tilvora-shortcode-btn' );
	wp_add_inline_style(
		'tilvora-shortcode-btn',
		'.tilvora-embed-cta{margin:14px 0}'
		. '.tilvora-embed-btn{display:inline-flex;align-items:center;gap:8px;background:#111111;color:#ffffff !important;padding:10px 22px;border-radius:8px;font-weight:600;text-decoration:none;transition:opacity 0.15s ease;font-size:15px}'
		. '.tilvora-embed-btn:hover{opacity:0.85}'
		. '.tilvora-embed-btn svg{flex-shrink:0}'
	);

	$label = esc_html( $atts['label'] );
	$href  = esc_url( $url );

	return '<div class="tilvora-embed-cta">'
		. '<a href="' . $href . '" class="tilvora-embed-btn">'
		. '<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">'
		. '<rect x="1" y="1" width="6" height="6" rx="1" stroke="currentColor" stroke-width="1.5"/>'
		. '<rect x="9" y="1" width="6" height="6" rx="1" stroke="currentColor" stroke-width="1.5"/>'
		. '<rect x="1" y="9" width="6" height="6" rx="1" stroke="currentColor" stroke-width="1.5"/>'
		. '<rect x="9" y="9" width="6" height="6" rx="1" stroke="currentColor" stroke-width="1.5"/>'
		. '</svg>'
		. $label
		. '</a>'
		. '</div>';
}
