<?php
/**
 * Card de obra — padrão dos wireframes (home e, na etapa 4, listagens e
 * mapa): foto, badge de status, carimbo "ENTREGUE!" animado se entregue,
 * eixo, título, bairro + região, descritivo breve e barra de progresso
 * quando em andamento. O card inteiro leva pra ficha da obra.
 *
 * Uso: get_template_part( 'template-parts/card-obra', null, array( 'obra' => $post ) ).
 */

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

$mfm_obra = $args['obra'] ?? null;
if ( ! $mfm_obra instanceof WP_Post ) {
	return;
}

$mfm_status        = (string) get_post_meta( $mfm_obra->ID, 'status', true );
$mfm_status_rotulo = mfm_status_rotulos()[ $mfm_status ] ?? '';
$mfm_entregue      = 'entregue' === $mfm_status;
$mfm_progresso     = min( 100, max( 0, (int) get_post_meta( $mfm_obra->ID, 'progresso', true ) ) );
$mfm_resumo        = (string) get_post_meta( $mfm_obra->ID, 'descritivo_breve', true );
$mfm_bairro        = mfm_termo_da_obra( $mfm_obra->ID, 'bairro' );
$mfm_regiao        = mfm_termo_da_obra( $mfm_obra->ID, 'regiao' );
$mfm_eixo          = mfm_termo_da_obra( $mfm_obra->ID, 'eixo' );
$mfm_local         = implode( ' · ', array_filter( array( $mfm_bairro['nome'], $mfm_regiao['nome'] ) ) );
?>

<article class="mfm-card<?php echo $mfm_entregue ? ' mfm-card--entregue' : ''; ?>">
	<div class="mfm-card__midia">
		<?php if ( has_post_thumbnail( $mfm_obra ) ) : ?>
			<?php
			echo get_the_post_thumbnail(
				$mfm_obra,
				'medium_large',
				array(
					'class'   => 'mfm-card__foto',
					'loading' => 'lazy',
					'sizes'   => '(min-width: 1100px) 25vw, (min-width: 640px) 50vw, 100vw',
				)
			);
			?>
		<?php else : ?>
			<span class="mfm-card__foto mfm-card__foto--vazia" aria-hidden="true">{&nbsp;}</span>
		<?php endif; ?>

		<?php if ( $mfm_status_rotulo ) : ?>
			<span class="mfm-badge mfm-badge--<?php echo esc_attr( $mfm_status ); ?>"><?php echo esc_html( $mfm_status_rotulo ); ?></span>
		<?php endif; ?>

		<?php if ( $mfm_entregue ) : ?>
			<!-- Decorativo (aria-hidden): o badge acima já conta o status. -->
			<span class="mfm-carimbo" aria-hidden="true">Entregue!</span>
		<?php endif; ?>
	</div>

	<div class="mfm-card__corpo">
		<?php if ( $mfm_eixo['nome'] ) : ?>
			<p class="mfm-card__eixo"><?php echo esc_html( $mfm_eixo['nome'] ); ?></p>
		<?php endif; ?>

		<h3 class="mfm-card__titulo">
			<a href="<?php echo esc_url( get_permalink( $mfm_obra ) ); ?>"><?php echo esc_html( get_the_title( $mfm_obra ) ); ?></a>
		</h3>

		<?php if ( $mfm_local ) : ?>
			<p class="mfm-card__local"><?php echo esc_html( $mfm_local ); ?></p>
		<?php endif; ?>

		<?php if ( $mfm_resumo ) : ?>
			<p class="mfm-card__resumo"><?php echo esc_html( $mfm_resumo ); ?></p>
		<?php endif; ?>

		<?php if ( 'andamento' === $mfm_status ) : ?>
			<div class="mfm-card__progresso">
				<span
					class="mfm-card__barra"
					role="progressbar"
					aria-label="Progresso da obra"
					aria-valuemin="0"
					aria-valuemax="100"
					aria-valuenow="<?php echo esc_attr( $mfm_progresso ); ?>"
				>
					<span class="mfm-card__barra-fill" style="width:<?php echo esc_attr( $mfm_progresso ); ?>%"></span>
				</span>
				<?php echo esc_html( $mfm_progresso ); ?>%
			</div>
		<?php endif; ?>
	</div>
</article>
