<?php

namespace Fotogenic_AI;

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

/**
 * Owns compat layer: WooCommerce adapters and cache invalidation registration.
 * Adapters are constructed lazily; cache invalidation is registered during Plugin init when WC is available.
 */
final class Compat_Manager {
	private Platform_Capabilities $capabilities;

	private ?ProductQueryAdapter $product_query_adapter = null;
	private ?ProductImageSourceAdapter $product_image_source_adapter = null;
	private ?ProductMediaWriteAdapter $product_media_write_adapter = null;
	private bool $adapters_created = false;

	private ?MediaQueryAdapter $media_query_adapter = null;
	private bool $media_adapters_created = false;

	private ?PostQueryAdapter $post_query_adapter = null;
	private ?PageQueryAdapter $page_query_adapter = null;
	private bool $wp_content_adapters_created = false;

	public function __construct( ?Platform_Capabilities $capabilities = null ) {
		$this->capabilities = $capabilities ?? Platform_Capabilities::instance();
	}

	/**
	 * Register Studio product cache invalidation hooks. Call from Plugin::init() when WooCommerce is available.
	 */
	public function register_cache_invalidation(): void {
		if ( ! $this->is_woocommerce_active() ) {
			return;
		}
		fotogenic_ai_load_compat();
		$invalidator = new Studio_Cache_Invalidator();
		$cache_invalidation = new Compat\WooCommerce\WooCommerce_Studio_Cache_Invalidation( $invalidator );
		$cache_invalidation->register();
		$bulk_cache_invalidation = new Compat\WooCommerce\WooCommerce_Bulk_Cache_Invalidation();
		$bulk_cache_invalidation->register();
	}

	/**
	 * Lazy-create WooCommerce adapters and return the product query adapter.
	 * When WooCommerce is not available, adapters are still constructed; they return empty/error from their methods.
	 */
	public function get_product_query_adapter(): ?ProductQueryAdapter {
		$this->ensure_adapters();
		return $this->product_query_adapter;
	}

	/**
	 * Lazy-create WooCommerce adapters and return the product image source adapter.
	 */
	public function get_product_image_source_adapter(): ?ProductImageSourceAdapter {
		$this->ensure_adapters();
		return $this->product_image_source_adapter;
	}

	/**
	 * Lazy-create adapters and return the product media write adapter.
	 */
	public function get_product_media_write_adapter(): ?ProductMediaWriteAdapter {
		$this->ensure_adapters();
		return $this->product_media_write_adapter;
	}

	/**
	 * Whether the WooCommerce product backend is available. Used by Studio for localized data only.
	 * Adapter selection is the single place availability is decided; callers use adapters and handle WP_Error.
	 */
	public function is_woocommerce_active(): bool {
		return $this->woocommerce_status()['available'];
	}

	/** @return array{available: bool, code: string, message: string, version: string, missingApis: array<int, string>} */
	public function woocommerce_status(): array {
		return $this->capabilities->woocommerce_status();
	}

	/**
	 * Lazy-create WP media adapters and return the media query adapter. Always non-null (WP media).
	 */
	public function get_media_query_adapter(): MediaQueryAdapter {
		$this->ensure_media_adapters();
		return $this->media_query_adapter;
	}

	/**
	 * Return the post query adapter. Always non-null (WP posts).
	 */
	public function get_post_query_adapter(): PostQueryAdapter {
		$this->ensure_wp_content_adapters();
		return $this->post_query_adapter;
	}

	/**
	 * Return the page query adapter. Always non-null (WP pages).
	 */
	public function get_page_query_adapter(): PageQueryAdapter {
		$this->ensure_wp_content_adapters();
		return $this->page_query_adapter;
	}

	private function ensure_adapters(): void {
		if ( $this->adapters_created ) {
			return;
		}
		if ( $this->is_woocommerce_active() ) {
			fotogenic_ai_load_compat();
			$this->product_query_adapter         = new Compat\WooCommerce\Product_Query_Adapter_WooCommerce();
			$this->product_image_source_adapter  = new Compat\WooCommerce\Product_Image_Source_Adapter_WooCommerce();
			$this->product_media_write_adapter  = new Compat\WooCommerce\Product_Media_Write_Adapter_WooCommerce();
		} else {
			$this->product_query_adapter         = new Product_Query_Adapter_Fallback();
			$this->product_image_source_adapter  = new Product_Image_Source_Adapter_Fallback();
			$this->product_media_write_adapter   = new Product_Media_Write_Adapter_Fallback();
		}
		$this->adapters_created = true;
	}

	private function ensure_media_adapters(): void {
		if ( $this->media_adapters_created ) {
			return;
		}
		fotogenic_ai_load_compat();
		$this->media_query_adapter         = new Compat\Media\Media_Query_Adapter_Wp();
		$this->media_adapters_created = true;
	}

	private function ensure_wp_content_adapters(): void {
		if ( $this->wp_content_adapters_created ) {
			return;
		}
		fotogenic_ai_load_compat();
		$this->post_query_adapter = new Compat\Wp\Post_Query_Adapter_Wp();
		$this->page_query_adapter = new Compat\Wp\Page_Query_Adapter_Wp();
		$this->wp_content_adapters_created = true;
	}
}
