<?php
$context           = ru_ufpe_theme_get_unit_page_context();
$fruits_illustration = ru_ufpe_theme_asset_uri('assets/images/illustrations/frutas.svg');
get_header();
?>

<main class="min-h-screen">
	<div class="mx-auto max-w-ru px-4 py-10 md:py-14 space-y-8">

		<?php if (!$context): ?>
			<div class="notice notice-error"><i data-lucide="circle-x"></i><span>Esta página de unidade não está vinculada corretamente.</span></div>
		<?php else:
			$unit_post       = $context['unit_post'];
			$unit_meta       = $context['unit_meta'];
			$map_provider    = ru_ufpe_theme_get_unit_map_provider($unit_meta);
			$map_label       = ru_ufpe_theme_get_map_provider_label($map_provider);
			$page_links      = $context['page_links'];
			$external_links  = ru_ufpe_theme_get_unit_external_links($unit_meta);
			$maps_url        = !empty($unit_meta['google_maps_url']) ? $unit_meta['google_maps_url'] : '';
			$maps_embed_url  = !empty($unit_meta['google_maps_embed_url']) && ru_ufpe_theme_is_valid_google_maps_embed_url($unit_meta['google_maps_embed_url']) ? $unit_meta['google_maps_embed_url'] : '';
			$featured_notice = ru_ufpe_theme_get_unit_notices_query($unit_post->ID, 3);
		?>

		<!-- Page header -->
		<header class="space-y-4">
			<div class="flex flex-wrap gap-2">
				<span class="tag tag-outline-secondary tag-lg">Unidade</span>
				<?php if ($unit_meta['cidade']): ?>
					<span class="tag tag-outline tag-lg"><?php echo esc_html($unit_meta['cidade']); ?></span>
				<?php endif; ?>
				<span class="tag tag-success tag-lg"><?php echo esc_html($unit_meta['status'] ?: 'Funcionamento regular'); ?></span>
			</div>
			<h1 class="text-4xl font-semibold leading-tight tracking-tight text-base-content md:text-5xl">
				<?php echo esc_html(get_the_title($unit_post)); ?>
			</h1>
			<p class="max-w-2xl text-lg leading-8 text-base-muted">
				<?php echo esc_html($unit_post->post_excerpt ?: wp_trim_words(wp_strip_all_tags($unit_post->post_content), 26)); ?>
			</p>
			<div class="flex flex-wrap gap-2">
				<a class="btn btn-primary" href="<?php echo esc_url($page_links['overview']); ?>">Visão geral</a>
				<a class="btn btn-outline" href="<?php echo esc_url($page_links['cardapio']); ?>">Cardápio</a>
				<a class="btn btn-outline" href="<?php echo esc_url($page_links['avisos']); ?>">Avisos</a>
			</div>
		</header>

		<!-- Unit overview -->
		<div class="grid gap-5 lg:grid-cols-3">
			<article class="card">
				<div class="card-body gap-5">
					<?php if (has_post_thumbnail($unit_post)): ?>
						<figure class="overflow-hidden rounded-md">
							<?php echo get_the_post_thumbnail($unit_post, 'large', ['class' => 'max-h-80 w-full object-cover']); ?>
						</figure>
					<?php endif; ?>
					<div class="prose-ru"><?php echo apply_filters('the_content', $unit_post->post_content); ?></div>
				</div>
			</article>

			<div class="card">
				<div class="card-body gap-3">
					<h2 class="text-xs font-semibold uppercase tracking-widest text-base-muted">Dados da unidade</h2>
					<ul class="space-y-3 text-sm leading-7">
						<?php if ($unit_meta['cidade']): ?><li><strong>Cidade:</strong> <?php echo esc_html($unit_meta['cidade']); ?></li><?php endif; ?>
						<?php if ($unit_meta['endereco']): ?><li><strong>Endereço:</strong> <?php echo esc_html($unit_meta['endereco']); ?></li><?php endif; ?>
						<?php if ($unit_meta['horario_funcionamento']): ?><li><strong>Horário:</strong> <?php echo esc_html($unit_meta['horario_funcionamento']); ?></li><?php endif; ?>
						<?php if ($unit_meta['contato']): ?><li><strong>Contato:</strong> <?php echo esc_html($unit_meta['contato']); ?></li><?php endif; ?>
					</ul>
				</div>
			</div>

			<div class="card">
				<div class="card-body gap-2">
					<h2 class="text-xs font-semibold uppercase tracking-widest text-base-muted">Acessos rápidos</h2>
					<a class="btn btn-primary btn-wide" href="<?php echo esc_url($page_links['cardapio']); ?>"><i data-lucide="utensils"></i> Cardápio semanal</a>
					<a class="btn btn-outline btn-wide" href="<?php echo esc_url($page_links['avisos']); ?>"><i data-lucide="bell"></i> Avisos da unidade</a>
					<?php if (!empty($external_links)): ?>
						<div class="divider"></div>
						<?php foreach ($external_links as $link):
							$is_tel = str_starts_with($link['url'], 'tel:'); ?>
							<a class="btn btn-outline justify-start gap-2" href="<?php echo esc_url($link['url']); ?>" <?php echo $is_tel ? '' : 'target="_blank" rel="noopener noreferrer"'; ?>>
								<?php echo wp_kses_post(ru_ufpe_theme_get_icon_markup($link['icon'])); ?>
								<span class="truncate"><?php echo esc_html($link['label']); ?></span>
								<?php if (!$is_tel): ?><i data-lucide="external-link" class="ml-auto text-base-muted shrink-0"></i><?php endif; ?>
							</a>
						<?php endforeach; ?>
					<?php endif; ?>
				</div>
			</div>

		</div>

		<?php if ($maps_embed_url || $maps_url): ?>
			<section class="unit-map-panel-bleed" aria-labelledby="unit-map-title">
				<div class="unit-map-panel">
					<div class="unit-map-panel-heading">
						<div>
							<p class="text-xs font-semibold uppercase tracking-normal text-base-muted">Localização da unidade</p>
							<h2 id="unit-map-title" class="text-2xl font-semibold text-base-content">Como chegar</h2>
						</div>
						<?php if ($maps_url): ?>
							<a class="btn btn-outline gap-2" href="<?php echo esc_url($maps_url); ?>" target="_blank" rel="noopener noreferrer">
								<i data-lucide="map"></i> Abrir no <?php echo esc_html($map_label); ?>
								<i data-lucide="external-link" class="text-base-muted"></i>
							</a>
						<?php endif; ?>
					</div>
					<?php if ($maps_embed_url): ?>
						<div class="unit-map-frame-wrap">
							<iframe class="unit-map-frame" src="<?php echo esc_url($maps_embed_url); ?>" loading="lazy" allowfullscreen referrerpolicy="no-referrer-when-downgrade" title="<?php echo esc_attr('Mapa ' . get_the_title($unit_post)); ?>"></iframe>
						</div>
					<?php endif; ?>
				</div>
			</section>
		<?php endif; ?>

		<!-- Latest notices -->
		<section class="space-y-5">
			<div class="flex flex-wrap items-center justify-between gap-3">
				<div class="space-y-1">
					<span class="tag tag-outline-accent">Avisos</span>
					<h2 class="text-2xl font-semibold tracking-tight">Últimas atualizações de <?php echo esc_html(get_the_title($unit_post)); ?></h2>
				</div>
				<a class="btn btn-outline" href="<?php echo esc_url($page_links['avisos']); ?>">Ver todos</a>
			</div>
			<?php if ($featured_notice->have_posts()): ?>
				<div class="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
					<?php while ($featured_notice->have_posts()): $featured_notice->the_post(); ?>
						<article <?php post_class('card'); ?>>
							<div class="card-body gap-2">
								<span class="tag tag-outline text-base-muted text-xs w-fit"><?php echo esc_html(get_the_date('d/m/Y')); ?></span>
								<h3 class="card-title text-base"><a class="hover:underline no-underline" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
								<p class="text-sm leading-6 text-base-muted line-clamp-3"><?php echo esc_html(wp_trim_words(get_the_excerpt(), 18)); ?></p>
							</div>
						</article>
					<?php endwhile; wp_reset_postdata(); ?>
				</div>
			<?php else: ?>
				<div class="notice notice-info"><i data-lucide="info"></i><span>Nenhum aviso vinculado a esta unidade foi publicado.</span></div>
			<?php endif; ?>
		</section>

		<?php endif; ?>
	</div>
</main>

<?php get_footer(); ?>
