<?php
/**
 * Plugin Name: Tilvora - 3D Flooring Visualizer
 * Description: Standalone 3D floor visualizer. Create visualizer projects, embed them anywhere with a shortcode, and receive quote requests with ease.
 * Version: 1.0.0
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: Noor E Alam
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: tilvora-3d-flooring-visualizer
 */

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



/* ── Constants ────────────────────────────────────────────────────── */
define( 'TILVORA_VERSION', '1.0.0' );
define( 'TILVORA_PLUGIN_FILE', __FILE__ );
define( 'TILVORA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'TILVORA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'TILVORA_QUERY_VAR', 'tilvora_configure' );        // value = project ID, renders full-screen visualizer
define( 'TILVORA_CONFIG_QUERY_VAR', 'tilvora_config' );    // value = project ID, serves resolved config.json
define( 'TILVORA_MATERIALS_QUERY_VAR', 'tilvora_materials' ); // presence = serves the Materials Library JSON feed

/* Custom post type for visualizer projects. */
define( 'TILVORA_CPT', 'tilvora_proj' );

/* Per-project meta keys (stored on the tilvora_visualizer post). */
define( 'TILVORA_META_MODEL_ID', '_tilvora_model_id' );      // attachment ID of an uploaded .glb
define( 'TILVORA_META_CONFIG_JSON', '_tilvora_config_json' ); // raw JSON text, project-specific config.json

/* ── Allow .glb uploads through the WordPress media library ──────── */
add_filter( 'upload_mimes', function ( $mimes ) {
	$mimes['glb'] = 'model/gltf-binary';
	return $mimes;
} );

add_filter( 'wp_check_filetype_and_ext', function ( $data, $file, $filename, $mimes ) {
	if ( preg_match( '/\.glb$/i', $filename ) ) {
		$data['ext']             = 'glb';
		$data['type']            = 'model/gltf-binary';
		$data['proper_filename'] = $filename;
	}
	return $data;
}, 10, 4 );

/* ── Includes ─────────────────────────────────────────────────────── */
require_once TILVORA_PLUGIN_DIR . 'includes/quote-db.php';
require_once TILVORA_PLUGIN_DIR . 'includes/project-config.php';
require_once TILVORA_PLUGIN_DIR . 'includes/cpt.php';
require_once TILVORA_PLUGIN_DIR . 'includes/materials-cpt.php';
require_once TILVORA_PLUGIN_DIR . 'includes/materials-endpoint.php';
require_once TILVORA_PLUGIN_DIR . 'includes/admin-ui.php';
require_once TILVORA_PLUGIN_DIR . 'includes/quote-inbox.php';
require_once TILVORA_PLUGIN_DIR . 'includes/configurator-page.php';
require_once TILVORA_PLUGIN_DIR . 'includes/config-endpoint.php';
require_once TILVORA_PLUGIN_DIR . 'includes/ajax-bridge.php';

/* ── Query vars ───────────────────────────────────────────────────── */
add_filter( 'query_vars', function ( $vars ) {
	$vars[] = TILVORA_QUERY_VAR;
	$vars[] = TILVORA_CONFIG_QUERY_VAR;
	$vars[] = TILVORA_MATERIALS_QUERY_VAR;
	return $vars;
} );

/* ── Activation / deactivation ────────────────────────────────────── */
register_activation_hook( __FILE__, function () {
	tilvora_create_quotes_table();
	flush_rewrite_rules();
} );

register_deactivation_hook( __FILE__, function () {
	flush_rewrite_rules();
} );
