<?php
/**
 * Subscriber list admin view.
 *
 * Displays filterable, paginated subscriber table with
 * delete actions and PRO export/bulk features.
 *
 * @package Waitlytics
 */

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

$wtly_subscriber_model = new WTLY_Subscriber();

// Get filter values. These read-only $_GET params drive list filtering and
// pagination display; they never mutate data, so nonce verification is not required.
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$wtly_current_status = isset( $_GET['status'] ) ? sanitize_key( $_GET['status'] ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$wtly_current_search = isset( $_GET['s'] ) ? sanitize_text_field( wp_unslash( $_GET['s'] ) ) : '';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$wtly_current_page   = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
$wtly_per_page       = 20;

$wtly_args = array(
	'status'   => $wtly_current_status,
	'search'   => $wtly_current_search,
	'page'     => $wtly_current_page,
	'per_page' => $wtly_per_page,
);

$wtly_result      = $wtly_subscriber_model->get_all( $wtly_args );
$wtly_subscribers = $wtly_result['items'];
$wtly_total       = $wtly_result['total'];
$wtly_total_pages = $wtly_result['pages'];

$wtly_base_url = admin_url( 'admin.php?page=waitlytics&tab=subscribers' );
?>

<div class="wtly-subscribers">

	<!-- Filters -->
	<div class="wtly-card wtly-filter-bar">
		<form method="get" action="<?php echo esc_url( admin_url( 'admin.php' ) ); ?>">
			<input type="hidden" name="page" value="waitlytics">
			<input type="hidden" name="tab" value="subscribers">

			<label for="wtly-filter-status"><?php esc_html_e( 'Status:', 'waitlytics' ); ?></label>
			<select id="wtly-filter-status" name="status">
				<option value=""><?php esc_html_e( 'All', 'waitlytics' ); ?></option>
				<option value="active" <?php selected( $wtly_current_status, 'active' ); ?>><?php esc_html_e( 'Active', 'waitlytics' ); ?></option>
				<option value="notified" <?php selected( $wtly_current_status, 'notified' ); ?>><?php esc_html_e( 'Notified', 'waitlytics' ); ?></option>
				<option value="unsubscribed" <?php selected( $wtly_current_status, 'unsubscribed' ); ?>><?php esc_html_e( 'Unsubscribed', 'waitlytics' ); ?></option>
			</select>

			<label for="wtly-filter-search" class="screen-reader-text"><?php esc_html_e( 'Search', 'waitlytics' ); ?></label>
			<input type="search" id="wtly-filter-search" name="s" value="<?php echo esc_attr( $wtly_current_search ); ?>" placeholder="<?php esc_attr_e( 'Search by email...', 'waitlytics' ); ?>">

			<button type="submit" class="button"><?php esc_html_e( 'Filter', 'waitlytics' ); ?></button>

			<?php
			/**
			 * Fires after the subscriber filter controls. The Pro build uses
			 * this to render the CSV export button.
			 */
			do_action( 'wtly_subscribers_actions' );
			?>
		</form>
	</div>

	<!-- Subscriber Table -->
	<div class="wtly-card">
		<?php if ( ! empty( $wtly_subscribers ) ) : ?>
			<table class="wp-list-table widefat fixed striped wtly-subscriber-table">
				<thead>
					<tr>
						<th class="wtly-col-email"><?php esc_html_e( 'Email', 'waitlytics' ); ?></th>
						<th class="wtly-col-product"><?php esc_html_e( 'Product', 'waitlytics' ); ?></th>
						<th class="wtly-col-status"><?php esc_html_e( 'Status', 'waitlytics' ); ?></th>
						<th class="wtly-col-date"><?php esc_html_e( 'Date', 'waitlytics' ); ?></th>
						<th class="wtly-col-actions"><?php esc_html_e( 'Actions', 'waitlytics' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $wtly_subscribers as $wtly_sub ) :
						$wtly_product = wc_get_product( $wtly_sub->product_id );
						$wtly_product_name = $wtly_product ? $wtly_product->get_name() : __( '(Deleted)', 'waitlytics' );

						if ( $wtly_sub->variation_id > 0 ) {
							$wtly_variation = wc_get_product( $wtly_sub->variation_id );
							if ( $wtly_variation ) {
								$wtly_product_name = $wtly_variation->get_name();
							}
						}

						$wtly_status_labels = array(
							'active'       => __( 'Active', 'waitlytics' ),
							'notified'     => __( 'Notified', 'waitlytics' ),
							'unsubscribed' => __( 'Unsubscribed', 'waitlytics' ),
							'bounced'      => __( 'Bounced', 'waitlytics' ),
						);
						$wtly_status_label = isset( $wtly_status_labels[ $wtly_sub->status ] ) ? $wtly_status_labels[ $wtly_sub->status ] : $wtly_sub->status;
						?>
						<tr data-subscriber-id="<?php echo esc_attr( $wtly_sub->id ); ?>">
							<td class="wtly-col-email"><?php echo esc_html( $wtly_sub->email ); ?></td>
							<td class="wtly-col-product"><?php echo esc_html( $wtly_product_name ); ?></td>
							<td class="wtly-col-status">
								<span class="wtly-status wtly-status-<?php echo esc_attr( $wtly_sub->status ); ?>">
									<?php echo esc_html( $wtly_status_label ); ?>
								</span>
							</td>
							<td class="wtly-col-date">
								<?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $wtly_sub->subscribed_at ) ) ); ?>
							</td>
							<td class="wtly-col-actions">
								<button type="button"
										class="button button-small wtly-delete-subscriber"
										data-id="<?php echo esc_attr( $wtly_sub->id ); ?>"
										title="<?php esc_attr_e( 'Delete', 'waitlytics' ); ?>">
									&times;
								</button>
							</td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>

			<!-- Pagination -->
			<?php if ( $wtly_total_pages > 1 ) : ?>
				<div class="wtly-pagination tablenav bottom">
					<div class="tablenav-pages">
						<span class="displaying-num">
							<?php
							printf(
								/* translators: %s: total number of subscribers */
								esc_html__( '%s items', 'waitlytics' ),
								esc_html( number_format_i18n( $wtly_total ) )
							);
							?>
						</span>
						<span class="pagination-links">
							<?php if ( $wtly_current_page > 1 ) : ?>
								<a class="prev-page button" href="<?php echo esc_url( add_query_arg( 'paged', $wtly_current_page - 1, $wtly_base_url ) ); ?>">
									&lsaquo;
								</a>
							<?php else : ?>
								<span class="tablenav-pages-navspan button disabled">&lsaquo;</span>
							<?php endif; ?>

							<span class="paging-input">
								<?php echo esc_html( $wtly_current_page ); ?>
								<?php esc_html_e( 'of', 'waitlytics' ); ?>
								<span class="total-pages"><?php echo esc_html( $wtly_total_pages ); ?></span>
							</span>

							<?php if ( $wtly_current_page < $wtly_total_pages ) : ?>
								<a class="next-page button" href="<?php echo esc_url( add_query_arg( 'paged', $wtly_current_page + 1, $wtly_base_url ) ); ?>">
									&rsaquo;
								</a>
							<?php else : ?>
								<span class="tablenav-pages-navspan button disabled">&rsaquo;</span>
							<?php endif; ?>
						</span>
					</div>
				</div>
			<?php endif; ?>

		<?php else : ?>
			<p class="wtly-empty-state">
				<?php esc_html_e( 'No subscribers found.', 'waitlytics' ); ?>
			</p>
		<?php endif; ?>
	</div>

</div>
