<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<?php
$creator_profile       = isset( $creator['profile'] ) && is_array( $creator['profile'] ) ? $creator['profile'] : array();
$creator_rights        = isset( $creator['rights'] ) && is_array( $creator['rights'] ) ? $creator['rights'] : array();
$dashboard_summary     = isset( $dashboard_summary ) && is_array( $dashboard_summary ) ? $dashboard_summary : array();
$dashboard_summary     = wp_parse_args(
	$dashboard_summary,
	array(
		'total'    => 0,
		'pending'  => 0,
		'approved' => 0,
		'rejected' => 0,
	)
);
$recent_uploads        = isset( $recent_uploads ) && is_array( $recent_uploads ) ? $recent_uploads : array();
$upload_history        = isset( $upload_history ) && is_array( $upload_history ) ? $upload_history : array();
$performance           = isset( $performance ) && is_array( $performance ) ? $performance : array();
$creator_name          = ! empty( $creator_profile['display_name'] ) ? (string) $creator_profile['display_name'] : ( ! empty( $creator['display_name'] ) ? (string) $creator['display_name'] : __( 'Creator', 'ugcify' ) );
$joined_at             = ! empty( $creator['joined_at'] ) ? (int) $creator['joined_at'] : 0;
$can_edit_profile      = empty( $creator['access']['is_admin_preview'] ) || ! empty( $can_view_uploads );
$overview_status_items = array(
	array(
		'key'   => 'total',
		'label' => __( 'Total uploads', 'ugcify' ),
		'value' => (string) $dashboard_summary['total'],
		'tone'  => 'neutral',
		'help'  => __( 'Everything you have sent through the creator portal.', 'ugcify' ),
	),
	array(
		'key'   => 'pending',
		'label' => __( 'Pending review', 'ugcify' ),
		'value' => (string) $dashboard_summary['pending'],
		'tone'  => 'pending',
		'help'  => __( 'Waiting for brand moderation.', 'ugcify' ),
	),
	array(
		'key'   => 'approved',
		'label' => __( 'Approved', 'ugcify' ),
		'value' => (string) $dashboard_summary['approved'],
		'tone'  => 'approved',
		'help'  => __( 'Cleared for publication or reuse.', 'ugcify' ),
	),
	array(
		'key'   => 'rejected',
		'label' => __( 'Needs changes', 'ugcify' ),
		'value' => (string) $dashboard_summary['rejected'],
		'tone'  => 'rejected',
		'help'  => __( 'Review history that still needs attention.', 'ugcify' ),
	),
);
?>
<div class="ugcify-portal-dashboard" data-ugcify-portal-dashboard>
	<div class="ugcify-portal-stats" data-ugcify-portal-stats>
		<?php foreach ( $overview_status_items as $stat_item ) : ?>
			<?php
			$stat_key   = $stat_item['key'];
			$stat_label = $stat_item['label'];
			$stat_value = $stat_item['value'];
			$stat_tone  = $stat_item['tone'];
			$stat_help  = $stat_item['help'];
			include UGCIFY_PATH . 'templates/influencer/parts/stat-card.php';
			?>
		<?php endforeach; ?>
	</div>

	<div class="ugcify-portal-tabset" data-ugcify-portal-tabset>
		<input class="ugcify-portal-tabset__control" type="radio" name="ugcify-portal-dashboard-tab" id="ugcify-portal-tab-control-overview" data-ugcify-portal-tab-control="overview" checked>
		<input class="ugcify-portal-tabset__control" type="radio" name="ugcify-portal-dashboard-tab" id="ugcify-portal-tab-control-upload" data-ugcify-portal-tab-control="upload">
		<input class="ugcify-portal-tabset__control" type="radio" name="ugcify-portal-dashboard-tab" id="ugcify-portal-tab-control-history" data-ugcify-portal-tab-control="history">
		<input class="ugcify-portal-tabset__control" type="radio" name="ugcify-portal-dashboard-tab" id="ugcify-portal-tab-control-profile" data-ugcify-portal-tab-control="profile">

		<nav class="ugcify-portal-nav" aria-label="<?php esc_attr_e( 'Creator portal sections', 'ugcify' ); ?>" role="tablist" data-ugcify-portal-tablist>
			<label class="ugcify-portal-nav__link" id="ugcify-portal-tab-overview" role="tab" aria-selected="true" aria-controls="ugcify-portal-overview" tabindex="0" data-ugcify-portal-view="overview" for="ugcify-portal-tab-control-overview"><?php esc_html_e( 'Overview', 'ugcify' ); ?></label>
			<label class="ugcify-portal-nav__link" id="ugcify-portal-tab-upload" role="tab" aria-selected="false" aria-controls="ugcify-portal-upload" tabindex="-1" data-ugcify-portal-view="upload" for="ugcify-portal-tab-control-upload"><?php esc_html_e( 'Upload', 'ugcify' ); ?></label>
			<label class="ugcify-portal-nav__link" id="ugcify-portal-tab-history" role="tab" aria-selected="false" aria-controls="ugcify-portal-history" tabindex="-1" data-ugcify-portal-view="history" for="ugcify-portal-tab-control-history"><?php esc_html_e( 'History', 'ugcify' ); ?></label>
			<label class="ugcify-portal-nav__link" id="ugcify-portal-tab-profile" role="tab" aria-selected="false" aria-controls="ugcify-portal-profile" tabindex="-1" data-ugcify-portal-view="profile" for="ugcify-portal-tab-control-profile"><?php esc_html_e( 'Profile', 'ugcify' ); ?></label>
			<span class="ugcify-portal-nav__indicator" aria-hidden="true" data-ugcify-portal-nav-indicator></span>
		</nav>

		<div class="ugcify-portal-panels">
		<section id="ugcify-portal-overview" class="ugcify-portal-panel" data-ugcify-portal-panel="overview" role="tabpanel" aria-labelledby="ugcify-portal-tab-overview" aria-hidden="false" tabindex="0">
			<div class="ugcify-portal-grid ugcify-portal-grid--two">
				<article class="ugcify-portal-card">
					<div class="ugcify-portal-section__header">
						<div>
							<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'Portal status', 'ugcify' ); ?></h4>
							<p class="ugcify-portal-section__description"><?php esc_html_e( 'Key account details and rights status for this creator profile.', 'ugcify' ); ?></p>
						</div>
					</div>
					<dl class="ugcify-portal-definition-list" data-ugcify-portal-profile-summary>
						<div>
							<dt><?php esc_html_e( 'Email', 'ugcify' ); ?></dt>
							<dd data-ugcify-portal-profile-field="email"><?php echo esc_html( (string) ( $creator['email'] ?? '' ) ); ?></dd>
						</div>
						<div>
							<dt><?php esc_html_e( 'Joined', 'ugcify' ); ?></dt>
							<dd data-ugcify-portal-profile-field="joined_at"><?php echo esc_html( $joined_at ? wp_date( get_option( 'date_format', 'F j, Y' ), $joined_at ) : __( 'Recently', 'ugcify' ) ); ?></dd>
						</div>
						<div>
							<dt><?php esc_html_e( 'Rights version', 'ugcify' ); ?></dt>
							<dd data-ugcify-portal-profile-field="rights_version"><?php echo esc_html( (string) ( $creator_rights['accepted_version'] ?? 0 ) ); ?></dd>
						</div>
						<div>
							<dt><?php esc_html_e( 'Creator status', 'ugcify' ); ?></dt>
							<dd data-ugcify-portal-profile-field="status"><?php echo esc_html( ucfirst( str_replace( '_', ' ', (string) ( $creator['status'] ?? 'inactive' ) ) ) ); ?></dd>
						</div>
					</dl>
				</article>

				<article class="ugcify-portal-card">
					<div class="ugcify-portal-section__header">
						<div>
							<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'What happens next', 'ugcify' ); ?></h4>
							<p class="ugcify-portal-section__description"><?php esc_html_e( 'Every creator submission stays within the existing moderation workflow.', 'ugcify' ); ?></p>
						</div>
					</div>
					<ul class="ugcify-portal-checklist">
						<li><?php esc_html_e( 'Upload images or videos in batches and keep per-item captions organized before submission.', 'ugcify' ); ?></li>
						<li><?php esc_html_e( 'Submitted items enter pending moderation by default and stay visible in your history.', 'ugcify' ); ?></li>
						<li><?php esc_html_e( 'Approved content can later appear alongside your creator profile and attribution settings.', 'ugcify' ); ?></li>
					</ul>
				</article>
			</div>

			<?php if ( ! empty( $performance ) ) : ?>
				<article class="ugcify-portal-card">
					<div class="ugcify-portal-section__header">
						<div>
							<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'Performance snapshot', 'ugcify' ); ?></h4>
							<p class="ugcify-portal-section__description"><?php esc_html_e( 'Only real analytics surfaced by the backend are shown here.', 'ugcify' ); ?></p>
						</div>
					</div>
					<div class="ugcify-portal-stats is-performance">
						<?php foreach ( $performance as $performance_item ) : ?>
							<?php
							$stat_key   = sanitize_key( (string) ( $performance_item['key'] ?? $performance_item['label'] ?? 'metric' ) );
							$stat_label = (string) ( $performance_item['label'] ?? '' );
							$stat_value = (string) ( $performance_item['value'] ?? '' );
							$stat_tone  = isset( $performance_item['tone'] ) ? (string) $performance_item['tone'] : 'neutral';
							$stat_help  = isset( $performance_item['description'] ) ? (string) $performance_item['description'] : '';
							include UGCIFY_PATH . 'templates/influencer/parts/stat-card.php';
							?>
						<?php endforeach; ?>
					</div>
				</article>
			<?php endif; ?>
		</section>

		<section id="ugcify-portal-upload" class="ugcify-portal-panel" data-ugcify-portal-panel="upload" role="tabpanel" aria-labelledby="ugcify-portal-tab-upload" aria-hidden="true" tabindex="-1">
			<?php include UGCIFY_PATH . 'templates/influencer/upload.php'; ?>
		</section>

		<section id="ugcify-portal-history" class="ugcify-portal-panel" data-ugcify-portal-panel="history" role="tabpanel" aria-labelledby="ugcify-portal-tab-history" aria-hidden="true" tabindex="-1">
			<?php include UGCIFY_PATH . 'templates/influencer/uploads-list.php'; ?>
		</section>

		<section id="ugcify-portal-profile" class="ugcify-portal-panel" data-ugcify-portal-panel="profile" role="tabpanel" aria-labelledby="ugcify-portal-tab-profile" aria-hidden="true" tabindex="-1">
			<?php include UGCIFY_PATH . 'templates/influencer/profile.php'; ?>
		</section>
		</div>
	</div>
</div>