<?php
/**
 * Página de perfil del usuario en el panel.
 *
 * @package Logistica
 */

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

require_once LOGISTICA_PLUGIN_DIR . 'admin/views/partials.php';

$context = logistica_is_tabbed_shell() ? 'frontend' : 'admin';
$user    = wp_get_current_user();

if ( ! $user || ! $user->ID ) {
	echo '<p class="logistica-user-panel__empty">' . esc_html__( 'No hay sesión activa.', 'logistica' ) . '</p>';
	return;
}

$info         = class_exists( 'Logistica_User_Panel', false )
	? Logistica_User_Panel::get_user_info( (int) $user->ID )
	: array();
$display_name = (string) ( $info['display_name'] ?? $user->display_name );
$email        = (string) ( $info['email'] ?? $user->user_email );
$role_label   = (string) ( $info['role'] ?? '' );
$login        = (string) ( $info['login'] ?? $user->user_login );
$registered   = (string) ( $info['registered'] ?? $user->user_registered );
$avatar_url   = (string) ( $info['avatar_url'] ?? get_avatar_url( $user->ID, array( 'size' => 128 ) ) );
$inicio_url   = logistica_tab_url( 'logistica', $context );

$registered_label = '';
if ( '' !== $registered ) {
	$registered_ts = strtotime( $registered );
	if ( $registered_ts ) {
		$registered_label = wp_date( 'j \d\e F \d\e Y', $registered_ts );
	}
}

if ( ! logistica_is_tabbed_shell() ) {
	logistica_admin_header( __( 'Mi perfil', 'logistica' ) );
}
?>
<section class="logistica-perfil" data-logistica-perfil>
	<header class="logistica-perfil__hero">
		<div class="logistica-perfil__avatar">
			<?php if ( $avatar_url ) : ?>
				<img src="<?php echo esc_url( $avatar_url ); ?>" alt="" width="96" height="96" loading="lazy" decoding="async">
			<?php else : ?>
				<span class="dashicons dashicons-admin-users" aria-hidden="true"></span>
			<?php endif; ?>
		</div>
		<div class="logistica-perfil__hero-text">
			<p class="logistica-perfil__eyebrow"><?php esc_html_e( 'Cuenta', 'logistica' ); ?></p>
			<h2 class="logistica-perfil__title"><?php echo esc_html( $display_name ?: __( 'Usuario', 'logistica' ) ); ?></h2>
			<?php if ( $role_label ) : ?>
				<span class="logistica-perfil__role"><?php echo esc_html( $role_label ); ?></span>
			<?php endif; ?>
		</div>
	</header>

	<div class="logistica-perfil__grid">
		<article class="logistica-perfil__card">
			<h3><?php esc_html_e( 'Datos de la cuenta', 'logistica' ); ?></h3>
			<dl class="logistica-perfil__details">
				<div>
					<dt><?php esc_html_e( 'Nombre', 'logistica' ); ?></dt>
					<dd><?php echo esc_html( $display_name ?: '—' ); ?></dd>
				</div>
				<div>
					<dt><?php esc_html_e( 'Usuario', 'logistica' ); ?></dt>
					<dd><?php echo esc_html( $login ?: '—' ); ?></dd>
				</div>
				<div>
					<dt><?php esc_html_e( 'Correo', 'logistica' ); ?></dt>
					<dd><?php echo esc_html( $email ?: '—' ); ?></dd>
				</div>
				<div>
					<dt><?php esc_html_e( 'Rol', 'logistica' ); ?></dt>
					<dd><?php echo esc_html( $role_label ?: '—' ); ?></dd>
				</div>
				<?php if ( $registered_label ) : ?>
					<div>
						<dt><?php esc_html_e( 'Miembro desde', 'logistica' ); ?></dt>
						<dd><?php echo esc_html( $registered_label ); ?></dd>
					</div>
				<?php endif; ?>
			</dl>
		</article>

		<article class="logistica-perfil__card logistica-perfil__card--actions">
			<h3><?php esc_html_e( 'Acciones', 'logistica' ); ?></h3>
			<p class="logistica-perfil__help"><?php esc_html_e( 'Vuelve al inicio o cierra tu sesión de forma segura.', 'logistica' ); ?></p>
			<div class="logistica-perfil__actions">
				<a class="logistica-perfil__btn logistica-perfil__btn--primary" href="<?php echo esc_url( $inicio_url ); ?>">
					<span class="dashicons dashicons-admin-home" aria-hidden="true"></span>
					<?php esc_html_e( 'Ir a Inicio', 'logistica' ); ?>
				</a>
				<button type="button" class="logistica-perfil__btn logistica-perfil__btn--danger" data-logistica-panel-logout>
					<span class="dashicons dashicons-migrate" aria-hidden="true"></span>
					<?php esc_html_e( 'Cerrar sesión', 'logistica' ); ?>
				</button>
			</div>
		</article>
	</div>
</section>
