<?php
/**
 * Odysseo SEO — self-contained, AI-assisted, SEO-ready module.
 *
 * Everything for this feature lives under /inc/odysseo-seo/ so it can be lifted
 * into a companion plugin later without touching the rest of the theme. When
 * moved to a plugin, only the URL/dir constants and the bootstrap require need
 * adjusting; all hooks are self-registered here.
 *
 * @package    Odysseo
 * @author     Chandrabhan Shekhawat
 * @company    Gigai Kripa Services
 * @link       https://gigaikripaservices.com/
 * @copyright  Copyright (c) 2026, Gigai Kripa Services
 */

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

define( 'ODYSSEO_SEO_VER', '1.0.0' );
define( 'ODYSSEO_SEO_DIR', __DIR__ );
define( 'ODYSSEO_SEO_URL', ODYSSEO_CORE_URL . 'includes/odysseo-seo' );
define( 'ODYSSEO_SEO_OPTION', 'odysseo_seo_settings' );
define( 'ODYSSEO_SEO_META_PREFIX', '_odysseo_seo_' );

/**
 * Default + saved settings, merged.
 *
 * @return array
 */
function odysseo_seo_get_settings() {
	$defaults = array(
		'provider'         => 'openai',
		'api_key'          => '',
		'model_openai'     => 'gpt-4o-mini',
		'model_gemini'     => 'gemini-1.5-flash',
		'gsc_verification' => '',
		// Structured data (schema) toggles — default ON.
		'schema_org'         => '1',
		'schema_website'     => '1',
		'schema_breadcrumb'  => '1',
		'schema_article'     => '1',
		'schema_destination' => '1',
		'schema_package'     => '1',
		'schema_vehicle'     => '1',
		'schema_faq'         => '1',
		'schema_service'     => '1',
		'schema_rating'      => '1',
		// Organization-level schema fields.
		'schema_legal_name'  => '',
		'schema_logo'        => '',
		'schema_founding'    => '',
		'schema_price_range' => '',
	);
	$saved = get_option( ODYSSEO_SEO_OPTION, array() );
	return wp_parse_args( is_array( $saved ) ? $saved : array(), $defaults );
}

/**
 * Whether a given structured-data type is enabled (default ON).
 *
 * @param string $type org|website|breadcrumb|article|destination|package|vehicle|faq|service|rating
 * @return bool
 */
function odysseo_seo_schema_on( $type ) {
	$s = odysseo_seo_get_settings();
	$k = 'schema_' . $type;
	return ! isset( $s[ $k ] ) || '1' === (string) $s[ $k ];
}

/**
 * The active API key. A wp-config constant wins over the DB option, so the most
 * sensitive secret can live outside the database entirely.
 *
 * @return string
 */
function odysseo_seo_get_api_key() {
	if ( defined( 'ODYSSEO_SEO_API_KEY' ) && ODYSSEO_SEO_API_KEY ) {
		return (string) ODYSSEO_SEO_API_KEY;
	}
	$s = odysseo_seo_get_settings();
	return (string) $s['api_key'];
}

/**
 * Whether an API key is configured (controls the "Generate with AI" UI).
 *
 * @return bool
 */
function odysseo_seo_has_api_key() {
	return '' !== trim( odysseo_seo_get_api_key() );
}

/**
 * Whether the key is locked to a wp-config constant (so the UI field is read-only).
 *
 * @return bool
 */
function odysseo_seo_key_is_constant() {
	return defined( 'ODYSSEO_SEO_API_KEY' ) && ODYSSEO_SEO_API_KEY;
}

/**
 * Post types that get the SEO meta box + per-post overrides. Filterable.
 *
 * @return string[]
 */
function odysseo_seo_post_types() {
	return apply_filters( 'odysseo_seo_post_types', array( 'post', 'page', 'destination', 'tour_package', 'fleet' ) );
}

/**
 * Step back if a major SEO plugin is active, so we never double up on output.
 *
 * @return bool
 */
function odysseo_seo_other_plugin_active() {
	return defined( 'WPSEO_VERSION' )       // Yoast SEO.
		|| defined( 'RANK_MATH_VERSION' )   // Rank Math.
		|| defined( 'AIOSEO_VERSION' )      // All in One SEO.
		|| function_exists( 'aioseo' )
		|| defined( 'SEOPRESS_VERSION' );   // SEOPress.
}

/* -------------------------------------------------------------------------- *
 *  Load components.
 * -------------------------------------------------------------------------- */
require_once ODYSSEO_SEO_DIR . '/includes/class-settings.php';
require_once ODYSSEO_SEO_DIR . '/includes/class-metabox.php';
require_once ODYSSEO_SEO_DIR . '/includes/providers/interface.php';
require_once ODYSSEO_SEO_DIR . '/includes/providers/openai.php';
require_once ODYSSEO_SEO_DIR . '/includes/providers/gemini.php';
require_once ODYSSEO_SEO_DIR . '/includes/class-ai.php';
require_once ODYSSEO_SEO_DIR . '/includes/class-head.php';
require_once ODYSSEO_SEO_DIR . '/includes/class-schema.php';
require_once ODYSSEO_SEO_DIR . '/includes/class-sitemap.php';

/**
 * Boot the module.
 */
function odysseo_seo_boot() {
	new Odysseo_SEO_Settings();
	new Odysseo_SEO_Metabox();
	new Odysseo_SEO_AI();
	new Odysseo_SEO_Head();
	new Odysseo_SEO_Schema();
	new Odysseo_SEO_Sitemap();
}
odysseo_seo_boot();

/*
 * The Head and Schema classes each detach the theme's inline head/schema hooks
 * (odysseo_output_seo_meta, odysseo_output_extra_seo_schema) when they load, so
 * head output is produced once and never duplicated.
 */
