<?php

namespace Fotogenic_AI;

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

/**
 * Validate configurable workflow recipes against a freshly confirmed account catalog.
 */
final class Workflow_Configuration_Authority {
	private Service_Configuration $configuration_service;

	public function __construct( ?Service_Configuration $configuration_service = null ) {
		$this->configuration_service = $configuration_service ?? new Service_Configuration();
	}

	/**
	 * @param array $draft         Normalized workflow draft.
	 * @param mixed $configuration Current upstream configuration payload.
	 * @return true|\WP_Error
	 */
	public function validate( array $draft, $configuration ) {
		$workflow = (string) ( $draft['workflow'] ?? '' );
		if ( ! in_array( $workflow, [ 'product_images', 'campaign_set', 'increase_resolution' ], true ) ) {
			return true;
		}
		if ( ! is_array( $configuration ) || ! $this->configuration_service->is_configuration_complete( $configuration ) ) {
			return $this->configuration_unavailable();
		}
		if ( 'increase_resolution' === $workflow ) {
			return true;
		}

		$available = $this->configuration_service->get_selection_ids( $configuration );
		$style_ids = array_column( $this->configuration_service->get_style_options( $configuration ), 'id' );
		if ( 'campaign_set' === $workflow ) {
			$quality_ids = array_column( $this->configuration_service->get_campaign_set_quality_options( $configuration ), 'id' );
			$groups = $draft['sourcePolicy']['groups'] ?? null;
			if ( ! is_array( $groups ) || empty( $groups ) ) {
				return $this->campaign_option_unavailable();
			}
			foreach ( $groups as $group ) {
				$recipe = is_array( $group['recipe'] ?? null ) ? $group['recipe'] : [];
				$quality = is_string( $recipe['quality'] ?? null ) ? strtoupper( trim( $recipe['quality'] ) ) : '';
				$style = is_string( $recipe['style'] ?? null ) ? trim( $recipe['style'] ) : '';
				$aspect_ratio = is_string( $recipe['aspectRatio'] ?? null ) ? trim( $recipe['aspectRatio'] ) : '';
				if ( '' === $quality || ! in_array( $quality, $quality_ids, true ) ) {
					return $this->quality_unavailable();
				}
				if ( '' !== $style && ! in_array( $style, $style_ids, true ) ) {
					return $this->campaign_option_unavailable();
				}
				if ( '' !== $aspect_ratio && ( empty( $available['aspectRatio'] ) || ! in_array( $aspect_ratio, $available['aspectRatio'], true ) ) ) {
					return $this->campaign_option_unavailable();
				}
			}

			return true;
		}

		$quality_options = $this->configuration_service->get_image_generation_quality_options( $configuration );
		$quality = is_string( $draft['recipe']['quality'] ?? null )
			? strtoupper( trim( $draft['recipe']['quality'] ) )
			: '';
		$quality_ids = array_column( $quality_options, 'id' );
		if ( '' === $quality || ! in_array( $quality, $quality_ids, true ) ) {
			return $this->quality_unavailable();
		}
		$style = is_string( $draft['recipe']['style'] ?? null )
			? trim( $draft['recipe']['style'] )
			: '';
		if ( '' !== $style && ! in_array( $style, $style_ids, true ) ) {
			return $this->selection_unavailable();
		}
		$selections = $draft['recipe']['selections'] ?? [];
		if ( ! is_array( $selections ) ) {
			return $this->selection_unavailable();
		}
		foreach ( $selections as $key => $selection ) {
			$id = is_array( $selection ) && is_string( $selection['id'] ?? null )
				? $selection['id']
				: '';
			if ( '' === $id || empty( $available[ $key ] ) || ! in_array( $id, $available[ $key ], true ) ) {
				return $this->selection_unavailable();
			}
		}

		return true;
	}

	private function configuration_unavailable(): \WP_Error {
		return new \WP_Error(
			'fotogenic_ai_configuration_unavailable',
			__( 'Current Fotogenic AI configuration is unavailable. Please refresh and try again.', 'fotogenic-ai' ),
			[ 'status' => 503 ]
		);
	}

	private function selection_unavailable(): \WP_Error {
		return new \WP_Error(
			'fotogenic_ai_recipe_option_unavailable',
			__( 'A selected Generate option is no longer available. Return to Configure and choose again.', 'fotogenic-ai' ),
			[ 'status' => 409 ]
		);
	}

	private function quality_unavailable(): \WP_Error {
		return new \WP_Error(
			'fotogenic_ai_quality_unavailable',
			__( 'The selected Generate quality is unavailable for this account. Return to Configure and choose again.', 'fotogenic-ai' ),
			[ 'status' => 409 ]
		);
	}

	private function campaign_option_unavailable(): \WP_Error {
		return new \WP_Error(
			'fotogenic_ai_campaign_option_unavailable',
			__( 'A selected Campaign option is no longer available. Return to Configure and choose again.', 'fotogenic-ai' ),
			[ 'status' => 409 ]
		);
	}
}
