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

$toggle_addons = array(
	array(
		'title'  => 'Progressive Web App',
		'desc'   => 'Let staff add the Staff Portal to their phone home screen like a native app.',
		'icon'   => 'dashicons-smartphone',
		'option' => 'yatoon_pwa_enabled',
	),
	array(
		'title'  => 'AI Reply Drafts',
		'desc'   => 'Draft suggested replies to client messages and reviews for staff to review and send.',
		'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;">Add-ons</h1>
	<p class="description" style="margin-bottom:24px;">Everything you can switch on for Yatoon Booking, in one place - no hunting through menus.</p>

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

	<h2 style="font-size:13px;text-transform:uppercase;letter-spacing:.05em;color:#697386;margin-bottom:10px;">Pro Add-ons</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">Activated</span>
						<a href="<?php echo esc_url( admin_url( 'admin.php?page=' . $addon['page'] ) ); ?>" class="button button-small">Configure</a>
					<?php else : ?>
						<span class="yatoon-addon-pill">Pro</span>
						<a href="<?php echo esc_url( $upgrade_url ); ?>" class="button button-small button-primary">Unlock</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;">Included Add-ons</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>
