<?php
/**
 * Image eligibility filtering for generation.
 *
 * @package AIAltTextGenerator
 * @since 2.4.1
 */

namespace AIAltTextGenerator;

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

/**
 * Applies extension, post-type, and file-presence filters from settings.
 *
 * @since 2.4.1
 */
class ImageFilter {

	/**
	 * @return array<string, \WP_Post_Type>
	 */
	public static function availablePostTypes(): array {
		$types = [];

		foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $name => $object ) {
			if ( $name === 'attachment' || ! $object instanceof \WP_Post_Type ) {
				continue;
			}

			$types[ $name ] = $object;
		}

		uasort(
			$types,
			static function ( \WP_Post_Type $a, \WP_Post_Type $b ): int {
				$label_a = $a->labels->singular_name ?? $a->label ?? $a->name;
				$label_b = $b->labels->singular_name ?? $b->label ?? $b->name;

				return strcasecmp( (string) $label_a, (string) $label_b );
			}
		);

		return $types;
	}

	/**
	 * @return list<string>
	 */
	public static function excludedPostTypes( ?array $settings = null ): array {
		$settings = $settings ?? Settings::get();
		$raw      = (string) ( $settings['excluded_post_types'] ?? '' );

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

		return array_values(
			array_filter(
				array_map(
					static fn( string $type ): string => sanitize_key( trim( $type ) ),
					explode( ',', $raw )
				),
				static fn( string $type ): bool => $type !== ''
			)
		);
	}

	public static function isExcludedByPostType( int $attachment_id, ?array $settings = null ): bool {
		$excluded = self::excludedPostTypes( $settings );
		if ( $excluded === [] ) {
			return false;
		}

		$parent_id = (int) wp_get_post_parent_id( $attachment_id );
		if ( $parent_id <= 0 ) {
			return false;
		}

		$parent_type = get_post_type( $parent_id );

		return is_string( $parent_type ) && in_array( $parent_type, $excluded, true );
	}

	public static function attachmentFileExists( int $attachment_id ): bool {
		$file = get_attached_file( $attachment_id, true );

		return is_string( $file ) && $file !== '' && is_file( $file );
	}

	/**
	 * @return array{status: string, message: string}|null Null when the image may be processed.
	 */
	public static function blockingReason( int $attachment_id, ?array $settings = null ): ?array {
		$settings = $settings ?? Settings::get();

		if ( ! AltTextGenerator::isSupportedImage( $attachment_id, $settings ) ) {
			return [
				'status'  => 'skipped',
				'message' => __( 'File type is not in your allowed extensions.', 'wppowersuite-lite' ),
			];
		}

		if ( self::isExcludedByPostType( $attachment_id, $settings ) ) {
			return [
				'status'  => 'skipped',
				'message' => __( 'Image is attached to an excluded post type.', 'wppowersuite-lite' ),
			];
		}

		if ( ! self::attachmentFileExists( $attachment_id ) ) {
			$status = ! empty( $settings['skip_missing_files'] ) ? 'skipped' : 'failed';

			return [
				'status'  => $status,
				'message' => __( 'Image file not found on the server.', 'wppowersuite-lite' ),
			];
		}

		return null;
	}
}
