<?php
/**
 * Contentosapp Studio Standalone — Compat Layer
 *
 * Emulates the ContentOS Core API functions by mapping them to
 * the standalone equivalents. This allows the Contentosapp Studio engine
 * (copied from the Core) to run without modifications.
 *
 * @since 1.0.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Emulate costudio_settings()
 * Returns the standalone settings instance.
 */
if ( ! function_exists( 'costudio_settings' ) ) {
	function costudio_settings() {
		$core_settings = 'contentos_' . 'settings';
		if ( function_exists( $core_settings ) && defined( 'CONTENTOS_VERSION' ) ) {
			return $core_settings();
		}
		if ( ! class_exists( 'Costudio_Standalone_Settings' ) ) {
			return null;
		}
		return Costudio_Standalone_Settings::get_instance();
	}
}

/**
 * Emulate costudio_sites()
 * Returns the standalone sites instance.
 */
if ( ! function_exists( 'costudio_sites' ) ) {
	function costudio_sites() {
		$core_sites = 'contentos_' . 'sites';
		if ( function_exists( $core_sites ) && defined( 'CONTENTOS_VERSION' ) ) {
			return $core_sites();
		}
		if ( ! class_exists( 'Costudio_Standalone_Sites' ) ) {
			return null;
		}
		return Costudio_Standalone_Sites::get_instance();
	}
}

/**
 * Emulate costudio_events()
 * Standalone ships a no-op bus so engine code can call ->emit() safely.
 */
if ( ! class_exists( 'Costudio_Standalone_Null_Events_Bus' ) ) {
	class Costudio_Standalone_Null_Events_Bus {
		public function emit( string $event, array $payload = array() ): void {
			// Intentionally no-op in standalone mode.
		}
	}
}

if ( ! function_exists( 'costudio_events' ) ) {
	function costudio_events(): object {
		$core_events = 'contentos_' . 'events';
		if ( function_exists( $core_events ) && defined( 'CONTENTOS_VERSION' ) ) {
			return $core_events();
		}
		static $bus = null;
		if ( null === $bus ) {
			$bus = new Costudio_Standalone_Null_Events_Bus();
		}
		return $bus;
	}
}

/**
 * Emulate costudio_can()
 * Uses the standalone capability map.
 */
if ( ! function_exists( 'costudio_can' ) ) {
	function costudio_can( string $capability ): bool {
		$core_can = 'contentos_' . 'can';
		if ( function_exists( $core_can ) && defined( 'CONTENTOS_VERSION' ) ) {
			return (bool) $core_can( $capability );
		}
		if ( ! class_exists( 'Costudio_Standalone_License' ) ) {
			// Fail safe: only admins can do things if the capability map is missing.
			return current_user_can( 'manage_options' );
		}
		return Costudio_Standalone_License::can( $capability );
	}
}

/**
 * Emulate costudio_license()
 * Returns the standalone license instance.
 */
if ( ! function_exists( 'costudio_license' ) ) {
	function costudio_license(): ?Costudio_Standalone_License {
		if ( ! class_exists( 'Costudio_Standalone_License' ) ) {
			return null;
		}
		return Costudio_Standalone_License::get_instance();
	}
}

/**
 * Emulate Costudio_Sites_Crypto
 * Redirects to the standalone crypto implementation.
 */
if ( ! class_exists( 'Costudio_Sites_Crypto' ) ) {
	class Costudio_Sites_Crypto {
		public static function encrypt( string $plain ): string {
			return Costudio_Standalone_Crypto::encrypt( $plain );
		}
		public static function decrypt( string $value ): string {
			return Costudio_Standalone_Crypto::decrypt( $value );
		}
	}
}

/**
 * Human-readable site title for UI lists and selects.
 * Standalone storage uses `label`; ContentOS Core uses `name`.
 *
 * @param array  $cfg Site row from contentos_sites / costudio_sites.
 * @param string $key Option array key (slug) when no display name is stored.
 */
if ( ! function_exists( 'costudio_site_display_name' ) ) {
	function costudio_site_display_name( array $cfg, string $key ): string {
		$name  = isset( $cfg['name'] ) ? trim( (string) $cfg['name'] ) : '';
		$label = isset( $cfg['label'] ) ? trim( (string) $cfg['label'] ) : '';
		if ( '' !== $name ) {
			return $name;
		}
		if ( '' !== $label ) {
			return $label;
		}
		return $key;
	}
}

/**
 * Build the CSS variable set used by site identity badges.
 *
 * @param array $cfg Site row from contentos_sites / costudio_sites.
 * @return array{accent:string,soft:string,border:string,style:string}
 */
if ( ! function_exists( 'costudio_site_color_vars' ) ) {
	function costudio_site_color_vars( array $cfg ): array {
		$accent = sanitize_hex_color( $cfg['color'] ?? '' ) ?: '#6c47ff';
		$soft   = class_exists( 'Costudio_Pipeline_Input_Mapper' )
			? Costudio_Pipeline_Input_Mapper::get_soft_color( $accent )
			: '#f0edff';

		return array(
			'accent' => $accent,
			'soft'   => $soft,
			'border' => $soft,
			'style'  => sprintf(
				'--fos-site-accent:%1$s;--fos-site-soft:%2$s;--fos-site-border:%3$s;',
				esc_attr( $accent ),
				esc_attr( $soft ),
				esc_attr( $soft )
			),
		);
	}
}
