<?php if ( ! defined( 'ABSPATH' ) ) { exit; } ?>
<?php
$upload_disabled_message = ! empty( $creator['access']['is_admin_preview'] ) && empty( $can_view_uploads )
	? __( 'Admin preview mode cannot upload creator content until the account is linked to a creator profile.', 'ugcify' )
	: __( 'Your account cannot upload new creator content right now. If rights or account access changed, review the latest portal state first.', 'ugcify' );
$active_invitation            = isset( $active_invitation ) && is_array( $active_invitation ) ? $active_invitation : array();
$available_invitations        = isset( $available_invitations ) && is_array( $available_invitations ) ? array_values( $available_invitations ) : array();
$requires_invitation_selection = ! empty( $requires_invitation_selection );
$active_invitation_products   = isset( $active_invitation['products'] ) && is_array( $active_invitation['products'] ) ? array_values( $active_invitation['products'] ) : array();
$active_has_product_scope     = ! empty( $active_invitation['has_product_scope'] );
$scoped_invitations           = array_values(
	array_filter(
		$available_invitations,
		static function ( $invitation ) {
			return is_array( $invitation ) && ! empty( $invitation['has_product_scope'] );
		}
	)
);
?>
<div class="ugcify-portal-upload" data-ugcify-portal-upload data-can-upload="<?php echo $can_upload ? '1' : '0'; ?>">
	<div class="ugcify-form-alert" data-ugcify-portal-upload-notice hidden></div>

	<?php if ( ! $can_upload ) : ?>
		<div class="ugcify-portal-card">
			<div class="ugcify-portal-empty-state is-inline">
				<p><?php echo esc_html( $upload_disabled_message ); ?></p>
			</div>
		</div>
	<?php elseif ( $requires_invitation_selection ) : ?>
		<article class="ugcify-portal-card ugcify-portal-card--stacked ugcify-portal-invitation-brief">
			<div class="ugcify-portal-section__header">
				<div>
					<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'Choose a creator brief', 'ugcify' ); ?></h4>
					<p class="ugcify-portal-section__description"><?php esc_html_e( 'Your account has product-specific invitation tasks. Open one of the invitations below before uploading so each file is submitted against the correct WooCommerce product scope.', 'ugcify' ); ?></p>
				</div>
			</div>

			<div class="ugcify-portal-invitation-task-list">
				<?php foreach ( $scoped_invitations as $invitation_task ) : ?>
					<?php
					$task_products = isset( $invitation_task['products'] ) && is_array( $invitation_task['products'] ) ? array_values( $invitation_task['products'] ) : array();
					$task_url      = isset( $invitation_task['portal_url'] ) ? (string) $invitation_task['portal_url'] : '';
					$task_url      = '' !== $task_url ? preg_replace( '/#.*/', '', $task_url ) . '#ugcify-portal-upload' : '';
					?>
					<article class="ugcify-portal-invitation-task">
						<div class="ugcify-portal-invitation-task__copy">
							<div class="ugcify-portal-invitation-task__header">
								<strong><?php echo esc_html( ! empty( $invitation_task['name'] ) ? (string) $invitation_task['name'] : (string) $invitation_task['email'] ); ?></strong>
								<span class="ugcify-portal-status-badge is-<?php echo esc_attr( sanitize_html_class( (string) ( $invitation_task['status'] ?? 'pending' ) ) ); ?>"><?php echo esc_html( ucfirst( str_replace( '_', ' ', (string) ( $invitation_task['status'] ?? 'pending' ) ) ) ); ?></span>
							</div>
							<p class="ugcify-portal-section__description"><?php echo esc_html( sprintf( __( 'Expires %s', 'ugcify' ), wp_date( get_option( 'date_format', 'F j, Y' ), strtotime( (string) $invitation_task['expires_at'] ) ) ) ); ?></p>
							<?php if ( ! empty( $invitation_task['custom_message'] ) ) : ?>
								<p class="ugcify-portal-invitation-task__message"><?php echo esc_html( (string) $invitation_task['custom_message'] ); ?></p>
							<?php endif; ?>
							<div class="ugcify-portal-invitation-products">
								<?php if ( ! empty( $task_products ) ) : ?>
									<?php foreach ( $task_products as $product ) : ?>
										<span class="ugcify-portal-product-pill"><?php echo esc_html( (string) ( $product['title'] ?? __( 'Product', 'ugcify' ) ) ); ?></span>
									<?php endforeach; ?>
								<?php else : ?>
									<span class="ugcify-portal-product-pill"><?php esc_html_e( 'No product scope attached', 'ugcify' ); ?></span>
								<?php endif; ?>
							</div>
						</div>
						<?php if ( '' !== $task_url ) : ?>
							<a class="ugcify-portal-button is-primary" href="<?php echo esc_url( $task_url ); ?>"><?php esc_html_e( 'Open Upload Brief', 'ugcify' ); ?></a>
						<?php endif; ?>
					</article>
				<?php endforeach; ?>
			</div>
		</article>
	<?php else : ?>
		<?php if ( ! empty( $active_invitation['id'] ) ) : ?>
			<article class="ugcify-portal-card ugcify-portal-card--stacked ugcify-portal-invitation-brief">
				<div class="ugcify-portal-section__header">
					<div>
						<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'Active creator brief', 'ugcify' ); ?></h4>
						<p class="ugcify-portal-section__description"><?php echo esc_html( $active_has_product_scope ? __( 'Each queued item in this batch must be assigned to one of the products requested in this invitation.', 'ugcify' ) : __( 'This upload session is linked to the invitation you opened. Any new submission will stay tied to that creator brief.', 'ugcify' ) ); ?></p>
					</div>
					<?php if ( ! empty( $active_invitation['expires_at'] ) ) : ?>
						<span class="ugcify-portal-section__count"><?php echo esc_html( sprintf( __( 'Expires %s', 'ugcify' ), wp_date( get_option( 'date_format', 'F j, Y' ), strtotime( (string) $active_invitation['expires_at'] ) ) ) ); ?></span>
					<?php endif; ?>
				</div>

				<?php if ( ! empty( $active_invitation['custom_message'] ) ) : ?>
					<div class="ugcify-review-form-message">
						<strong><?php esc_html_e( 'Brief note', 'ugcify' ); ?></strong><br>
						<?php echo esc_html( (string) $active_invitation['custom_message'] ); ?>
					</div>
				<?php endif; ?>

				<?php if ( $active_has_product_scope ) : ?>
					<div class="ugcify-portal-invitation-products">
						<?php foreach ( $active_invitation_products as $product ) : ?>
							<span class="ugcify-portal-product-pill"><?php echo esc_html( (string) ( $product['title'] ?? __( 'Product', 'ugcify' ) ) ); ?></span>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</article>
		<?php endif; ?>

		<article class="ugcify-portal-card ugcify-portal-card--stacked ugcify-portal-upload-workspace">
			<div class="ugcify-portal-section__header">
				<div>
					<h4 class="ugcify-portal-section__title"><?php esc_html_e( 'Add your reviews', 'ugcify' ); ?></h4>
					<p class="ugcify-portal-section__description"><?php echo esc_html( $active_has_product_scope ? __( 'Click "Add your review" on any product below to share your experience with the product.', 'ugcify' ) : __( 'Share your experience by adding reviews for the products.', 'ugcify' ) ); ?></p>
				</div>
			</div>

			<?php if ( $active_has_product_scope && ! empty( $active_invitation_products ) ) : ?>
				<div class="ugcify-portal-product-cards">
					<?php foreach ( $active_invitation_products as $product_data ) : ?>
						<?php
						$product_id = isset( $product_data['id'] ) ? intval( $product_data['id'] ) : 0;
						$product_title = isset( $product_data['title'] ) ? (string) $product_data['title'] : __( 'Product', 'ugcify' );
						$product_image = isset( $product_data['image'] ) ? (string) $product_data['image'] : '';
						
						// Get WooCommerce product for additional details
						$wc_product = function_exists( 'wc_get_product' ) && $product_id ? wc_get_product( $product_id ) : null;
						$product_price = $wc_product ? $wc_product->get_price_html() : '';
						
						// Use placeholder if no image
						if ( empty( $product_image ) && $wc_product ) {
							$product_image = wp_get_attachment_url( $wc_product->get_image_id() );
						}
						if ( empty( $product_image ) ) {
							$product_image = UGCIFY_URL . 'assets/images/default-image.35c6a344.png';
						}
						?>
						<div class="ugcify-portal-product-card">
							<div class="ugcify-portal-product-card__image">
								<img src="<?php echo esc_url( $product_image ); ?>" alt="<?php echo esc_attr( $product_title ); ?>" loading="lazy">
							</div>
							<div class="ugcify-portal-product-card__content">
								<h5 class="ugcify-portal-product-card__title"><?php echo esc_html( $product_title ); ?></h5>
							</div>
							<div class="ugcify-portal-product-card__footer">
								<button 
									type="button" 
									class="ugcify-portal-button is-primary ugcify-review-form-button" 
									data-product-id="<?php echo esc_attr( $product_id ); ?>"
									data-from-portal="1"
								>
									<?php esc_html_e( 'Add your review', 'ugcify' ); ?>
								</button>
							</div>
						</div>
					<?php endforeach; ?>
				</div>
			<?php else : ?>
				<div class="ugcify-portal-empty-state">
					<p><?php esc_html_e( 'No products available for review at this time.', 'ugcify' ); ?></p>
				</div>
			<?php endif; ?>

			<style>
				.ugcify-portal-product-cards {
					display: grid;
					grid-template-columns: repeat(4, 1fr);
					gap: 1.5em;
					margin-top: 1.5em;
				}
				
				.ugcify-portal-product-card {
					display: flex;
					flex-direction: column;
					background: rgba(0, 0, 0, 0.02);
					border: 1px solid rgba(0, 0, 0, 0.1);
					border-radius: 0.5em;
					overflow: hidden;
					transition: all 0.2s ease;
				}
				
				.ugcify-portal-product-card:hover {
					border-color: rgba(0, 0, 0, 0.2);
					box-shadow: 0 0.25em 0.75em rgba(0, 0, 0, 0.08);
					transform: translateY(-0.125em);
				}
				
				.ugcify-portal-product-card__image {
					position: relative;
					width: 100%;
					padding-top: 100%;
					background: rgba(0, 0, 0, 0.03);
					overflow: hidden;
				}
				
				.ugcify-portal-product-card__image img {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					object-fit: cover;
				}
				
				.ugcify-portal-product-card__content {
					padding: 1em;
					flex: 1;
				}
				
				.ugcify-portal-product-card__title {
					margin: 0 0 0.5em 0;
					font-size: 0.95em;
					font-weight: 600;
					line-height: 1.4;
					color: inherit;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
					overflow: hidden;
				}
				
				.ugcify-portal-product-card__footer {
					padding: 0 1em 1em 1em;
				}
				
				.ugcify-portal-product-card__footer .ugcify-portal-button {
					width: 100%;
					text-align: center;
					padding: 0.75em 1em;
					font-size: 0.9em;
				}
				
				@media (max-width: 1024px) {
					.ugcify-portal-product-cards {
						grid-template-columns: repeat(3, 1fr);
					}
				}
				
				@media (max-width: 768px) {
					.ugcify-portal-product-cards {
						grid-template-columns: repeat(2, 1fr);
						gap: 1em;
					}
				}
				
				@media (max-width: 480px) {
					.ugcify-portal-product-cards {
						grid-template-columns: repeat(2, 1fr);
					}
				}
			</style>
		</article>
	<?php endif; ?>
</div>