<?php
/** Conservative 4.6.2 fallback-package checks. */
$root = dirname( __DIR__ );
$failures = array();
$check = static function ( $condition, $message ) use ( &$failures ) {
    if ( ! $condition ) { $failures[] = $message; }
};
$main  = (string) file_get_contents( $root . '/yatoon-booking-system.php' );
$ajax  = (string) file_get_contents( $root . '/includes/class-yatoon-ajax.php' );
$hours = (string) file_get_contents( $root . '/includes/class-yatoon-business-hours.php' );
$is_pro_package = is_dir( $root . '/includes/pro' );

$check( false !== strpos( $main, "define( 'YATOON_VERSION', '4.6.2' );" ), 'Version constant is not 4.6.2.' );
$check( false !== strpos( $main, ' * Version:     4.6.2' ), 'Plugin header is not 4.6.2.' );
$check( ! is_file( $root . '/includes/class-yatoon-booking-creator.php' ), '4.6.3 booking-service refactor leaked into fallback.' );
$check( ! is_file( $root . '/includes/class-yatoon-schedule-repository.php' ), '4.6.3 schedule repository leaked into fallback.' );

if ( $is_pro_package ) {
    $check( false !== strpos( $main, "define( 'YATOON_PACKAGE_TIER', 'pro' );" ), 'Pro tier marker is wrong.' );
    $check( false !== strpos( $main, "'is_premium'      => true" ), 'Pro Freemius marker is wrong.' );
} else {
    $check( false !== strpos( $main, 'Plugin Name: Yatoon Salon Booking & Appointments' ), 'Free plugin name is wrong.' );
    $check( false !== strpos( $main, "define( 'YATOON_PACKAGE_TIER', 'free' );" ), 'Free tier marker is wrong.' );
    $check( false !== strpos( $main, "'is_premium'      => false" ), 'Free Freemius marker is wrong.' );
    $check( false === strpos( $main, 'Update URI:' ), 'Free package must use the WordPress.org update channel.' );
}

$check( false !== strpos( $ajax, 'public function release_booking_locks( $lock_keys )' ), 'Lock release callback is not public.' );
$check( substr_count( $ajax, "register_shutdown_function( array( \$this, 'release_booking_locks' )" ) === 1, 'Attempt-lock shutdown callback changed.' );
$check( substr_count( $ajax, "register_shutdown_function(array(\$this, 'release_booking_locks')" ) === 1, 'Calendar-lock shutdown callback changed.' );
$check( false !== strpos( $hours, "new DateTime( \$parts[0] . ' 00:00:00', \$tz )" ), 'All-day time off does not use the requested timezone.' );
$check( false !== strpos( $hours, "\$end->modify( '+1 day' )" ), 'All-day time off does not end on the next local calendar day.' );

$iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $root, FilesystemIterator::SKIP_DOTS ) );
foreach ( $iterator as $file ) {
    if ( 'php' !== strtolower( $file->getExtension() ) ) { continue; }
    exec( escapeshellarg( PHP_BINARY ) . ' -l ' . escapeshellarg( $file->getPathname() ) . ' 2>&1', $output, $code );
    if ( 0 !== $code ) { $failures[] = 'PHP syntax failed: ' . $file->getPathname(); }
    $output = array();
}

if ( $failures ) {
    foreach ( $failures as $failure ) { fwrite( STDERR, 'FAIL: ' . $failure . PHP_EOL ); }
    exit( 1 );
}
echo 'PASS: conservative Yatoon 4.6.2 fallback checks completed.' . PHP_EOL;
