<?php

namespace Fotogenic_AI;

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

/**
 * Immutable safety ceilings shared by every billable workflow owner.
 */
final class Workflow_Operational_Limits {
	public const MAX_RUN_ITEMS         = 12;
	public const MAX_OUTPUTS_PER_ITEM  = 6;
	public const MAX_RUN_OUTPUTS       = self::MAX_RUN_ITEMS * self::MAX_OUTPUTS_PER_ITEM;
	public const MAX_CONCURRENT_STARTS = 10;
	public const MAX_ACTIVE_BULK_ITEMS = 10;
	public const MAX_FILE_BYTES        = 26214400;
	public const MAX_MULTIPART_BYTES   = 31457280;

	public const BILLABLE_HTTP_TIMEOUT_SECONDS       = 120;
	public const BILLABLE_HTTP_MAX_RETRIES           = 2;
	public const RETRYING_HTTP_TIMEOUT_SECONDS       = 20;
	public const RETRYING_HTTP_MAX_RETRIES           = 2;
	public const HTTP_MAX_RETRY_AFTER_SECONDS        = 120;
	public const AUTH_REPLAY_LIMIT                   = 1;
	public const AUTH_HTTP_TIMEOUT_SECONDS           = 20;
	public const BILLABLE_DISPATCH_SAFETY_SECONDS    = 30;
	public const RECONCILIATION_LOCK_SAFETY_SECONDS  = 60;
	public const RECONCILIATION_WINDOW_SECONDS       = 15 * 60;

	/**
	 * Maximum wall-clock envelope for one keyed paid start, including a single
	 * serialized authentication refresh and replay of the identical request.
	 */
	public static function billable_transport_envelope_seconds(): int {
		$single_transport_envelope =
			( 1 + self::BILLABLE_HTTP_MAX_RETRIES ) * self::BILLABLE_HTTP_TIMEOUT_SECONDS
			+ self::BILLABLE_HTTP_MAX_RETRIES * self::HTTP_MAX_RETRY_AFTER_SECONDS;

		return ( 1 + self::AUTH_REPLAY_LIMIT ) * $single_transport_envelope
			+ self::AUTH_REPLAY_LIMIT * self::AUTH_HTTP_TIMEOUT_SECONDS
			+ self::BILLABLE_DISPATCH_SAFETY_SECONDS;
	}

	/**
	 * Maximum wall-clock envelope for one authenticated reconciliation lookup.
	 * The owning mutex must remain valid through all retries, Retry-After waits,
	 * one serialized authentication replay, and the following local mutation.
	 */
	public static function reconciliation_transport_envelope_seconds(): int {
		$single_transport_envelope =
			( 1 + self::RETRYING_HTTP_MAX_RETRIES ) * self::RETRYING_HTTP_TIMEOUT_SECONDS
			+ self::RETRYING_HTTP_MAX_RETRIES * self::HTTP_MAX_RETRY_AFTER_SECONDS;

		return ( 1 + self::AUTH_REPLAY_LIMIT ) * $single_transport_envelope
			+ self::AUTH_REPLAY_LIMIT * self::AUTH_HTTP_TIMEOUT_SECONDS
			+ self::RECONCILIATION_LOCK_SAFETY_SECONDS;
	}

	private function __construct() {}
}
