<?php
/**
 * Plans list view.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 *
 * @var LUCRSURE_Settings $settings Settings.
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

if ( ! current_user_can( lucrsure_admin_capability() ) ) {
	wp_die( esc_html__( 'You are not allowed to access this page.', 'lucrativeit-subscription-recurring-billing' ) );
}

$repo        = new LUCRSURE_Plan_Repository();
$cat_repo    = new LUCRSURE_Plan_Category_Repository();
$category_id = 0;
$interval    = '';
$intervals   = lucrsure_billing_intervals();

if ( isset( $_GET['_wpnonce'] )
	&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'lucrsure_filter_plans' )
) {
	$category_id = isset( $_GET['category'] ) ? absint( wp_unslash( $_GET['category'] ) ) : 0;
	$interval    = isset( $_GET['interval'] ) ? sanitize_key( wp_unslash( $_GET['interval'] ) ) : '';
}

if ( '' !== $interval && ! isset( $intervals[ $interval ] ) ) {
	$interval = '';
}
$plans         = $repo->all_by_category( $category_id, 'price', 'ASC', $interval );
$categories    = $cat_repo->options( false );
$category_list = $cat_repo->all( 'name', 'ASC' );
$has_filter    = $category_id > 0 || '' !== $interval;

global $wpdb;
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Subscription Plans', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Manage pricing tiers, features, and billing intervals.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'plans', array( 'action' => 'new' ) ) ); ?>">+ <?php esc_html_e( 'Add Plan', 'lucrativeit-subscription-recurring-billing' ); ?></a>
</div>

<?php if ( $settings->is_woocommerce_mode() ) : ?>
	<div class="lucrsure-info-bar"><?php esc_html_e( 'WooCommerce mode is active. Plans are synced to WooCommerce products automatically.', 'lucrativeit-subscription-recurring-billing' ); ?></div>
<?php endif; ?>

<form class="lucrsure-card lucrsure-toolbar" method="get">
	<input type="hidden" name="page" value="lucrsure-plans" />
	<?php wp_nonce_field( 'lucrsure_filter_plans', '_wpnonce', false ); ?>
	<select name="category" class="lucrsure-auto-submit">
		<option value="0"><?php esc_html_e( 'Category: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<?php foreach ( $category_list as $category ) : ?>
			<option value="<?php echo esc_attr( $category['id'] ); ?>" <?php selected( $category_id, (int) $category['id'] ); ?>><?php echo esc_html( $category['name'] ); ?></option>
		<?php endforeach; ?>
	</select>
	<select name="interval" class="lucrsure-auto-submit">
		<option value=""><?php esc_html_e( 'Interval: All', 'lucrativeit-subscription-recurring-billing' ); ?></option>
		<?php foreach ( $intervals as $interval_key => $interval_label ) : ?>
			<option value="<?php echo esc_attr( $interval_key ); ?>" <?php selected( $interval, $interval_key ); ?>><?php echo esc_html( $interval_label ); ?></option>
		<?php endforeach; ?>
	</select>
	<?php if ( $has_filter ) : ?>
		<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_url( 'plans' ) ); ?>"><?php esc_html_e( 'Clear filters', 'lucrativeit-subscription-recurring-billing' ); ?></a>
	<?php endif; ?>
</form>

<?php if ( empty( $plans ) ) : ?>
	<div class="lucrsure-card lucrsure-empty-state">
		<p>
			<?php
			if ( $has_filter ) {
				esc_html_e( 'No plans match your filters.', 'lucrativeit-subscription-recurring-billing' );
			} else {
				esc_html_e( 'No plans yet. Create your first pricing tier.', 'lucrativeit-subscription-recurring-billing' );
			}
			?>
		</p>
	</div>
<?php else : ?>
	<div class="lucrsure-cards-grid">
		<?php
		foreach ( $plans as $plan ) :
			$sub_count = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter
				$wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE plan_id = %d', lucrsure_table( 'subscriptions' ), (int) $plan['id'] )
			);
			$interval_key  = isset( $intervals[ $plan['billing_interval'] ] ) ? strtolower( $plan['billing_interval'] ) : $plan['billing_interval'];
			$category_name = ! empty( $plan['category_id'] ) && isset( $categories[ (int) $plan['category_id'] ] )
				? $categories[ (int) $plan['category_id'] ]
				: '';
			$thumbnail_url = LUCRSURE_Plan_Repository::thumbnail_url( $plan );
			?>
			<div class="lucrsure-card lucrsure-plan-card">
				<?php if ( $thumbnail_url ) : ?>
					<div class="lucrsure-plan-thumb">
						<img src="<?php echo esc_url( $thumbnail_url ); ?>" alt="<?php echo esc_attr( $plan['name'] ); ?>" />
					</div>
				<?php endif; ?>
				<div class="lucrsure-plan-top">
					<strong><?php echo esc_html( $plan['name'] ); ?></strong>
					<span class="lucrsure-muted"><?php echo esc_html( sprintf( /* translators: %d count */ _n( '%d sub', '%d subs', $sub_count, 'lucrativeit-subscription-recurring-billing' ), $sub_count ) ); ?></span>
				</div>
				<?php if ( ! empty( $plan['is_popular'] ) ) : ?>
					<p class="lucrsure-plan-popular-badge"><span class="lucrsure-pill lucrsure-pill-gold"><?php esc_html_e( 'Most popular', 'lucrativeit-subscription-recurring-billing' ); ?></span></p>
				<?php endif; ?>
				<?php if ( $category_name ) : ?>
					<p class="lucrsure-plan-category"><span class="lucrsure-pill lucrsure-pill-blue"><?php echo esc_html( $category_name ); ?></span></p>
				<?php endif; ?>
				<div class="lucrsure-plan-price">
					<?php
					$price_display = lucrsure_plan_format_price_display( $plan );
					if ( $price_display['has_special'] ) :
						?>
						<span class="lucrsure-price-regular"><?php echo esc_html( $price_display['regular'] ); ?></span>
					<?php endif; ?>
					<?php echo esc_html( $price_display['effective'] ); ?>
					<span>/<?php echo esc_html( $interval_key ); ?></span>
				</div>
				<p class="lucrsure-muted"><?php echo esc_html( $plan['description'] ); ?></p>
				<?php if ( (int) $plan['trial_days'] > 0 ) : ?>
					<p class="lucrsure-plan-trial"><?php echo esc_html( sprintf( /* translators: %d days */ __( '%d-day free trial', 'lucrativeit-subscription-recurring-billing' ), (int) $plan['trial_days'] ) ); ?></p>
				<?php endif; ?>
				<div class="lucrsure-plan-foot">
					<a class="lucrsure-btn lucrsure-btn-light" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'plans', array( 'action' => 'edit', 'id' => $plan['id'] ) ) ); ?>"><?php esc_html_e( 'Edit', 'lucrativeit-subscription-recurring-billing' ); ?></a>
					<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'subscriptions', array( 'action' => 'new', 'plan' => $plan['id'] ) ) ); ?>"><?php esc_html_e( 'Subscribe', 'lucrativeit-subscription-recurring-billing' ); ?></a>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
<?php endif; ?>
