<?php

namespace Fotogenic_AI\Compat\WooCommerce;

use Fotogenic_AI\ProductImageSourceAdapter;

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

final class Product_Image_Source_Adapter_WooCommerce implements ProductImageSourceAdapter {

	/** @return array|\WP_Error */
	public function get_image_ids_for_product( int $product_id ) {
		$wc_ok = function_exists( 'Fotogenic_AI\fotogenic_ai_woocommerce_available' ) ? \Fotogenic_AI\fotogenic_ai_woocommerce_available() : false;
		if ( ! $wc_ok ) {
			return new \WP_Error( 'woocommerce_not_active', __( 'WooCommerce is not active.', 'fotogenic-ai' ), [ 'status' => 503 ] );
		}
		$product = wc_get_product( $product_id );
		if ( ! $product ) {
			return new \WP_Error( 'fotogenic_ai_invalid_product', __( 'Product not found.', 'fotogenic-ai' ), [ 'status' => 404 ] );
		}
		$is_variation = $product instanceof \WC_Product_Variation || $product->is_type( 'variation' );
		$featured_id = (int) $product->get_image_id( 'edit' );
		$gallery_ids = $is_variation ? [] : $product->get_gallery_image_ids( 'edit' );
		$gallery     = is_array( $gallery_ids ) ? array_map( 'absint', $gallery_ids ) : [];
		return [
			'featured' => $featured_id,
			'gallery'  => $gallery,
		];
	}
}
