<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$is_pro      = function_exists( 'ybs_is_pro' ) ? ybs_is_pro() : false;
$upgrade_url = defined( 'YBS_UPGRADE_URL' ) ? YBS_UPGRADE_URL : 'https://checkout.freemius.com/plugin/24668/plan/40958/';

$pro_addons = array(
	array(
		'title' => __( 'Square Appointments Sync', 'yatoon-booking-system' ),
		'desc'  => __( 'Sync website bookings with the Square workflow your salon already uses.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-update',
		'page'  => 'yatoon-booking-system',
	),
	array(
		'title' => __( 'Square Online Payments', 'yatoon-booking-system' ),
		'desc'  => __( 'Collect deposits or prepayment with Card, Apple Pay, Google Pay, Cash App Pay, and Square Gift Card.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-cart',
		'page'  => 'yatoon-deposit-pro',
	),
	array(
		'title' => __( 'SMS Notifications', 'yatoon-booking-system' ),
		'desc'  => __( 'Text confirmations, reminders, and review requests via Twilio.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-email-alt',
		'page'  => 'sbs-sms-pro',
	),
	array(
		'title' => __( 'Google Calendar Sync', 'yatoon-booking-system' ),
		'desc'  => __( 'Two-way sync so every booking shows up on your staff Google calendars.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-google',
		'page'  => 'sbs-gcal-pro',
	),
	array(
		'title' => __( 'Deposit & Prepay', 'yatoon-booking-system' ),
		'desc'  => __( 'Collect a deposit or full payment at booking time to cut no-shows.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-money-alt',
		'page'  => 'yatoon-deposit-pro',
	),
	array(
		'title' => __( 'No-show Tracking', 'yatoon-booking-system' ),
		'desc'  => __( 'Flag repeat no-shows automatically and see them on the client profile.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-warning',
		'page'  => 'yatoon-noshow-pro',
	),
	array(
		'title' => __( 'Waitlist Auto-notify', 'yatoon-booking-system' ),
		'desc'  => __( 'Automatically text/email waitlisted clients the moment a slot opens up.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-megaphone',
		'page'  => 'yatoon-waitlist-pro',
	),
	array(
		'title' => __( 'Service Packages', 'yatoon-booking-system' ),
		'desc'  => __( 'Sell prepaid bundles clients can redeem across visits.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-archive',
		'page'  => 'yatoon-packages',
	),
	array(
		'title' => __( 'Brand & Colors', 'yatoon-booking-system' ),
		'desc'  => __( 'Full control of fonts, colors, and button styling on the booking widget.', 'yatoon-booking-system' ),
		'icon'  => 'dashicons-admin-customizer',
		'page'  => 'sbs-brand',
	),
);

$toggle_addons = array(
	array(
		'title'  => __( 'Progressive Web App', 'yatoon-booking-system' ),
		'desc'   => __( 'Let staff add the Staff Portal to their phone home screen like a native app.', 'yatoon-booking-system' ),
		'icon'   => 'dashicons-smartphone',
		'option' => 'yatoon_pwa_enabled',
	),
	array(
		'title'  => __( 'AI Reply Drafts', 'yatoon-booking-system' ),
		'desc'   => __( 'Draft suggested replies to client messages and reviews for staff to review and send.', 'yatoon-booking-system' ),
		'icon'   => 'dashicons-superhero-alt',
		'option' => 'yatoon_ai_reply_drafts_enabled',
	),
);
?>
<div class="wrap yatoon-addons-wrap" id="yatoon-dashboard-wrap">
	<h1 style="margin-bottom:4px;"><?php esc_html_e( 'Add-ons', 'yatoon-booking-system' ); ?></h1>
	<p class="description" style="margin-bottom:24px;"><?php esc_html_e( 'Everything you can switch on for Yatoon Booking, in one place - no hunting through menus.', 'yatoon-booking-system' ); ?></p>

	<?php if ( ! $is_pro ) : ?>
	<div class="sbs-card yatoon-addon-hero">
		<div>
			<strong><?php esc_html_e( 'Free is ready for local booking. Pro connects the salon workflow.', 'yatoon-booking-system' ); ?></strong>
			<p class="description" style="margin:4px 0 0;"><?php esc_html_e( 'Upgrade when you want Square sync, Square online payments, SMS reminders, Google Calendar, deposits, packages, no-show workflows, and priority support.', 'yatoon-booking-system' ); ?></p>
		</div>
		<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-primary" style="white-space:nowrap;"><?php esc_html_e( 'Upgrade to Pro', 'yatoon-booking-system' ); ?></a>
	</div>
	<?php else : ?>
	<div class="sbs-card yatoon-addon-hero is-pro">
		<div>
			<strong><?php esc_html_e( 'Pro is active.', 'yatoon-booking-system' ); ?></strong>
			<p class="description" style="margin:4px 0 0;"><?php esc_html_e( 'Use this page as a control center for Square, payments, reminders, growth tools, and staff workflow features.', 'yatoon-booking-system' ); ?></p>
		</div>
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=yatoon-setup-wizard' ) ); ?>" class="button" style="white-space:nowrap;"><?php esc_html_e( 'Open Setup Wizard', 'yatoon-booking-system' ); ?></a>
	</div>
	<?php endif; ?>

	<h2 style="font-size:13px;text-transform:uppercase;letter-spacing:.05em;color:#697386;margin-bottom:10px;"><?php esc_html_e( 'Pro Add-ons', 'yatoon-booking-system' ); ?></h2>
	<div class="yatoon-addon-grid">
		<?php foreach ( $pro_addons as $addon ) : ?>
			<div class="yatoon-addon-card">
				<div class="yatoon-addon-icon"><span class="dashicons <?php echo esc_attr( $addon['icon'] ); ?>"></span></div>
				<div class="yatoon-addon-body">
					<h3><?php echo esc_html( $addon['title'] ); ?></h3>
					<p><?php echo esc_html( $addon['desc'] ); ?></p>
				</div>
				<div class="yatoon-addon-action">
					<?php if ( $is_pro ) : ?>
						<span class="yatoon-addon-pill is-active"><?php esc_html_e( 'Activated', 'yatoon-booking-system' ); ?></span>
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . $addon['page'] ) ); ?>" class="button button-small"><?php esc_html_e( 'Configure', 'yatoon-booking-system' ); ?></a>
					<?php else : ?>
						<span class="yatoon-addon-pill"><?php esc_html_e( 'Pro', 'yatoon-booking-system' ); ?></span>
						<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-small button-primary"><?php esc_html_e( 'Unlock', 'yatoon-booking-system' ); ?></a>
					<?php endif; ?>
				</div>
			</div>
		<?php endforeach; ?>
	</div>

	<h2 style="font-size:13px;text-transform:uppercase;letter-spacing:.05em;color:#697386;margin:28px 0 10px;"><?php esc_html_e( 'Included Add-ons', 'yatoon-booking-system' ); ?></h2>
	<div class="yatoon-addon-grid">
		<?php foreach ( $toggle_addons as $addon ) :
			$enabled = get_option( $addon['option'], '0' ) === '1';
			?>
			<div class="yatoon-addon-card">
				<div class="yatoon-addon-icon"><span class="dashicons <?php echo esc_attr( $addon['icon'] ); ?>"></span></div>
				<div class="yatoon-addon-body">
					<h3><?php echo esc_html( $addon['title'] ); ?></h3>
					<p><?php echo esc_html( $addon['desc'] ); ?></p>
				</div>
				<div class="yatoon-addon-action">
					<label class="yatoon-addon-switch">
						<input type="checkbox" class="yatoon-addon-toggle" data-option="<?php echo esc_attr( $addon['option'] ); ?>" <?php checked( $enabled ); ?> />
						<span class="yatoon-addon-switch-track"></span>
					</label>
				</div>
			</div>
		<?php endforeach; ?>
	</div>
</div>

<style>
.yatoon-addon-hero{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:18px 20px;margin-bottom:24px;border:1px solid #fde68a;background:#fffbeb;}
.yatoon-addon-hero.is-pro{border-color:#bbf7d0;background:#f0fdf4;}
.yatoon-addon-hero strong{font-size:14px;color:#111827;}
.yatoon-addon-grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(280px,1fr));gap:14px;}
.yatoon-addon-card{background:#fff;border:1px solid rgba(203,213,225,.78);border-radius:12px;padding:16px;display:flex;flex-direction:column;gap:10px;box-shadow:0 12px 32px rgba(15,23,42,.045);}
.yatoon-addon-icon{width:36px;height:36px;border-radius:9px;background:#f2f4f8;display:flex;align-items:center;justify-content:center;}
.yatoon-addon-icon .dashicons{color:#111827;}
.yatoon-addon-body h3{font-size:14px;margin:0 0 4px;}
.yatoon-addon-body p{font-size:12.5px;color:#697386;margin:0;line-height:1.5;}
.yatoon-addon-action{margin-top:auto;display:flex;align-items:center;gap:8px;}
.yatoon-addon-pill{font-size:10px;font-weight:700;letter-spacing:.04em;text-transform:uppercase;background:#f2f4f8;color:#6b7280;padding:3px 8px;border-radius:999px;}
.yatoon-addon-pill.is-active{background:#dcfce7;color:#15803d;}
.yatoon-addon-switch{position:relative;display:inline-block;width:38px;height:22px;}
.yatoon-addon-switch input{opacity:0;width:0;height:0;}
.yatoon-addon-switch-track{position:absolute;inset:0;background:#e5e7eb;border-radius:999px;transition:.15s;cursor:pointer;}
.yatoon-addon-switch-track::before{content:"";position:absolute;width:16px;height:16px;left:3px;top:3px;background:#fff;border-radius:50%;transition:.15s;box-shadow:0 1px 3px rgba(0,0,0,.2);}
.yatoon-addon-switch input:checked + .yatoon-addon-switch-track{background:#111827;}
.yatoon-addon-switch input:checked + .yatoon-addon-switch-track::before{transform:translateX(16px);}
@media(max-width:720px){.yatoon-addon-hero{display:block}.yatoon-addon-hero .button{margin-top:12px}}
</style>
<script>
jQuery(function($){
	$('.yatoon-addon-toggle').on('change', function(){
		var $cb = $(this);
		$.post(ajaxurl, {
			action: 'yatoon_toggle_addon_setting',
			nonce: (window.sbsAdmin && sbsAdmin.nonce) ? sbsAdmin.nonce : '',
			option: $cb.data('option'),
			value: $cb.is(':checked') ? '1' : '0'
		}).fail(function(){
			$cb.prop('checked', !$cb.is(':checked'));
		});
	});
});
</script>
