<?php

namespace Fotogenic_AI;

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

final class Api_Endpoints {
    private function __construct() {}

    public static function get_base_urls(): array {
        return [
            'production' => 'https://api.fotogenic.ai',
        ];
    }

    public static function get_base_path(): string {
        return '/v2';
    }

    public static function get_default_endpoints(): array {
        return [
            'configuration_generator' => '/generateConfiguration',
            'images_generator'        => '/generateImages',
            'campaign_set_generator'  => '/generateCampaignSet',
            'enhance_images'           => '/enhanceImages',
            'requests_reconcile'       => '/requests/reconcile',
            'jobs_batch'              => '/jobs',
            'job'                     => '/job/{jobId}',
            'configuration'           => '/configuration',
            'credits_balance'         => '/credits/balance',
            'price'                   => '/price',
        ];
    }

	/**
	 * Returns immutable query parameters that identify a workflow operation.
	 *
	 * @return array<string,string>
	 */
	public static function get_fixed_query_params( string $endpoint_key ): array {
		if ( 'enhance_images' === $endpoint_key ) {
			return [ 'type' => 'increaseResolution' ];
		}

		return [];
	}

    public static function get_default_config(): array {
        return [
            'base_urls'       => self::get_base_urls(),
            'base_path'       => self::get_base_path(),
            'endpoints'       => self::get_default_endpoints(),
            'auth_header'     => 'authorization',
            'post_content_type' => 'application/json',
        ];
    }

    public static function normalize_base_path( string $path ): string {
        $normalized = '/' . trim( $path, " \t\n\r\0\x0B/" );
        return self::get_base_path() === $normalized ? $normalized : self::get_base_path();
    }

    public static function normalize_endpoints( array $endpoints ): array {
        $defaults   = self::get_default_endpoints();
        $normalized = [];
        foreach ( $defaults as $key => $default ) {
            $path = trim( (string) ( $endpoints[ $key ] ?? $default ) );
            $path = '' === $path ? '' : '/' . ltrim( $path, '/' );
            if ( '' === $path || preg_match( '#^/(?:https?:|v\d+/)#i', $path ) ) {
                $path = $default;
            }
            $normalized[ $key ] = $path;
        }

        return $normalized;
    }
}
