<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access to the script.
}

use Gigneo\Notifications;

$profile_id    = ! empty( $args['profile_id'] ) ? $args['profile_id'] : '';
$profile_type  = ! empty( $args['profile_type'] ) ? $args['profile_type'] : '';
$platform_type = ! empty( $args['platform_type'] ) ? $args['platform_type'] : '';
$credit_system = ! empty( $args['credit_system'] ) ? $args['credit_system'] : '';

$notification_class         = Notifications::instance();
$notification_data          = $notification_class::get_notifications( 4, 1, '', 'order' );
$notifications              = $notification_data['notifications'] ?? array();
$notification_dashboard_url = gigneo_dashboard_url( 'notification', 'all' );
?>
<div id="gn-overview-recent-activity" class="gn-dashboard-section gn-overview-recent-activity">
	<div class="gn-head">
		<h2 class="gn-title">
			<?php echo esc_html__( 'Recent order activity', 'gigneo' ); ?>
		</h2>
	</div>
	<div class="gn-body">
		<div class="gn-notification-list">
			<?php
			if ( ! empty( $notifications ) ) {
				foreach ( $notifications as $notification ) {
					$notification_id         = $notification['id'] ?? '';
					$notification_title      = $notification['title'] ?? '';
					$notification_content    = $notification['content'] ?? '';
					$notification_category   = $notification['category'] ?? '';
					$notification_type       = $notification['type'] ?? '';
					$notification_status     = $notification['status'] ?? '';
					$notification_meta       = $notification['meta'] ?? '';
					$notification_profile_id = $notification['profile_id'] ?? '';
					$notification_post_id    = $notification['post_id'] ?? '';
					$notification_created_at = $notification['created_at_gmt'] ?? '';

					$notification_content = html_entity_decode( $notification_content, ENT_QUOTES, 'UTF-8' );
					$notification_content = preg_replace( '/"([^"]+)"/', '<span class="gn-highlight">$1</span>', $notification_content );

					$notification_date   = $notification_class::date( $notification_created_at, $notification_profile_id );
					$notification_button = $notification_class::link( $notification_category, $notification_type, $notification_profile_id, $notification_post_id );

					$anchor_attr = 'div';
					if ( ! empty( $notification_button['link'] ) ) {
						$anchor_attr  = 'a';
						$anchor_attr .= ' href="' . esc_url( $notification_button['link'] ) . '"';
					}
					?>
					<<?php echo wp_kses_post( $anchor_attr ); ?>
					id="gn-notification-<?php echo esc_attr( $notification['id'] ); ?>" class="gn-notification-item
					gn-type-<?php echo esc_attr( $notification_type ); ?> gn-<?php echo esc_attr( $notification_status ); ?>">
					<?php
					//Avatar
					$participant_profile_thumbnail_id = $notification_class::avatar( $notification_category, $notification_type, $profile_id, $notification_post_id );
					if ( false !== $participant_profile_thumbnail_id ) {
						?>
						<figure class="gn-avatar"><?php gigneo_attachment_image_src( $participant_profile_thumbnail_id, 'thumbnail', 'profile' ); ?></figure>
						<?php
					} else {
						$icon_data  = $notification_class::icon( $notification_type );
						$icon       = $icon_data['icon'] ?? 'hi hi-exclamation-circle';
						$icon_class = $icon_data['status'] ?? 'general';
						?>
						<span class="gn-icon gn-status-<?php echo esc_attr( $icon_class ); ?>">
							<i class="<?php echo esc_html( $icon ); ?>" aria-hidden="true"></i>
						</span>
					<?php } ?>
					<div class="gn-info">
						<p class="gn-description"><?php echo wp_kses_post( $notification_content ); ?></p>
						<span class="gn-date"><?php echo esc_html( $notification_date ); ?></span>
					</div>
					</<?php echo wp_kses_post( $anchor_attr ); ?>>
					<?php
				}
			} else {
				$not_found_args = array(
					'title'       => esc_html__( 'No order activity', 'gigneo' ),
					'description' => esc_html__( 'This area will show recent order activity as they happen.', 'gigneo' ),
				);
				gigneo_not_found( 'account_overview_notifications', $not_found_args );
			}
			?>
		</div>
		<?php if ( ! empty( $notifications ) && count( $notifications ) > 3 ) { ?>
			<a href="<?php echo esc_url( $notification_dashboard_url ); ?>" class="gn-btn gn-btn-md gn-btn-outline-grey gn-btn-block">
				<?php echo esc_html__( 'View all activity', 'gigneo' ); ?>
				<i class="hi hi-arrow-up-right" aria-hidden="true"></i>
			</a>
		<?php } ?>
	</div>
</div>
