<?php
/**
 * Attachment alt text status helpers.
 *
 * @package AIAltTextGenerator
 * @since 1.5.0
 */

namespace AIAltTextGenerator;

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

/**
 * Resolves display status for attachments.
 *
 * @since 1.5.0
 */
class AttachmentStatus {

	/**
	 * @return array{slug: string, label: string, score: string}
	 */
	public static function resolve( int $attachment_id ): array {
		if ( AttachmentMeta::isDecorative( $attachment_id ) ) {
			return [
				'slug'  => 'decorative',
				'label' => QualityScorer::label( 'decorative' ),
				'score' => 'decorative',
			];
		}

		$alt      = (string) get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
		$settings = Settings::get();
		$max      = (int) ( $settings['max_chars'] ?? 125 );

		if ( $alt === '' ) {
			$ai_status = AttachmentMeta::getStatus( $attachment_id );
			if ( in_array( $ai_status, [ 'generated', 'needs_review' ], true ) ) {
				return [
					'slug'  => 'needs_review',
					'label' => QualityScorer::label( 'needs_review' ),
					'score' => 'needs_review',
				];
			}

			return [
				'slug'  => 'missing',
				'label' => QualityScorer::label( 'missing' ),
				'score' => 'missing',
			];
		}

		$score = QualityScorer::score( $alt, $max );

		return [
			'slug'  => $score,
			'label' => QualityScorer::label( $score ),
			'score' => $score,
		];
	}

	public static function badgeClass( string $slug ): string {
		$map = [
			'missing'       => 'is-missing',
			'weak'          => 'is-weak',
			'too_short'     => 'is-weak',
			'keyword_stuffed' => 'is-weak',
			'too_long'      => 'is-warning',
			'good'          => 'is-good',
			'decorative'    => 'is-neutral',
			'needs_review'  => 'is-review',
			'generated'     => 'is-review',
		];

		return $map[ $slug ] ?? 'is-neutral';
	}
}
