<?php
/**
 * Alt Text Generator facade.
 *
 * @package AIAltTextGenerator
 * @since 1.0.0
 */

namespace AIAltTextGenerator;

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

/**
 * Backward-compatible facade over provider-based generation.
 *
 * @since 1.0.0
 */
class AltTextGenerator {

	/** @var list<string> */
	private const SUPPORTED_MIMES = [
		'image/jpeg',
		'image/jpg',
		'image/png',
		'image/gif',
		'image/webp',
		'image/avif',
		'image/bmp',
		'image/tiff',
		'image/x-icon',
		'image/vnd.microsoft.icon',
	];

	private const SVG_MIME = 'image/svg+xml';

	/** @var array<string, list<string>> */
	private const EXTENSION_MIMES = [
		'jpg'   => [ 'image/jpeg', 'image/jpg' ],
		'jpeg'  => [ 'image/jpeg', 'image/jpg' ],
		'png'   => [ 'image/png' ],
		'gif'   => [ 'image/gif' ],
		'webp'  => [ 'image/webp' ],
		'avif'  => [ 'image/avif' ],
		'bmp'   => [ 'image/bmp' ],
		'tiff'  => [ 'image/tiff' ],
		'tif'   => [ 'image/tiff' ],
		'svg'   => [ 'image/svg+xml' ],
		'ico'   => [ 'image/x-icon', 'image/vnd.microsoft.icon' ],
	];

	/**
	 * Generate alt text for an image.
	 *
	 * @param string               $file_path Absolute path to the image file.
	 * @param string               $api_key   OpenAI API key.
	 * @param array<string, mixed> $options   Generation options.
	 * @return string|\WP_Error
	 */
	public static function generate( string $file_path, string $api_key, array $options = [] ): string|\WP_Error {
		$options['api_key'] = $api_key;
		$result = GenerationService::provider()->generate( $file_path, $options );

		if ( is_wp_error( $result ) ) {
			return $result;
		}

		return (string) ( $result['alt_text'] ?? '' );
	}

	/**
	 * @return list<string>
	 */
	public static function getSupportedMimes(): array {
		return array_values( array_unique( array_merge( self::SUPPORTED_MIMES, [ self::SVG_MIME ] ) ) );
	}

	/**
	 * Effective MIME allowlist based on module settings.
	 *
	 * @return list<string>
	 */
	public static function getAllowedMimes( ?array $settings = null ): array {
		$settings   = $settings ?? Settings::get();
		$extensions = self::expandExtensionAliases(
			self::parseAllowedExtensions( (string) ( $settings['allowed_extensions'] ?? '' ) )
		);

		if ( $extensions === [] ) {
			return self::getSupportedMimes();
		}

		$mimes = [];
		foreach ( $extensions as $extension ) {
			foreach ( self::EXTENSION_MIMES[ $extension ] ?? [] as $mime ) {
				$mimes[] = self::normalizeMime( $mime );
			}
		}

		return array_values( array_unique( $mimes ) );
	}

	/**
	 * @param list<string> $extensions
	 * @return list<string>
	 */
	public static function expandExtensionAliases( array $extensions ): array {
		$aliases = [
			'jpg'  => [ 'jpeg' ],
			'jpeg' => [ 'jpg' ],
			'tif'  => [ 'tiff' ],
			'tiff' => [ 'tif' ],
			'svg'  => [ 'svgz' ],
			'svgz' => [ 'svg' ],
		];

		$expanded = $extensions;
		foreach ( $extensions as $extension ) {
			foreach ( $aliases[ $extension ] ?? [] as $alias ) {
				$expanded[] = $alias;
			}
		}

		return array_values( array_unique( $expanded ) );
	}

	public static function normalizeMime( string $mime ): string {
		return Providers\VisionHelper::normalizeMime( $mime );
	}

	public static function isSupportedImage( int $attachment_id, ?array $settings = null ): bool {
		$mime = get_post_mime_type( $attachment_id );
		if ( ! $mime ) {
			return false;
		}

		return in_array( self::normalizeMime( $mime ), self::getAllowedMimes( $settings ), true );
	}

	/**
	 * @return list<string>
	 */
	public static function parseAllowedExtensions( string $raw ): array {
		$normalized = Settings::sanitizeAllowedExtensions( $raw );

		if ( $normalized === '' ) {
			return [];
		}

		return array_values( array_filter( explode( ',', $normalized ) ) );
	}

	/**
	 * Test API connectivity.
	 *
	 * @return true|\WP_Error
	 */
	public static function testConnection( string $api_key = '' ): bool|\WP_Error {
		$settings = Settings::get();
		$key      = $api_key !== '' ? $api_key : Settings::getApiKey( $settings );
		return ProviderRegistry::get()->testConnection( [ 'api_key' => $key ] );
	}
}
