<?php
/**
 * Cron compatibility bridge for Noesia Smart Waiter.
 *
 * Cron ownership for completed request cleanup has moved to a module-local
 * owner. This bridge remains only so existing bootstrap / activation /
 * deactivation code that references Noesia_SWA_Cron does not fatal.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_SWA_Cron' ) ) {
    /**
     * Legacy cron bridge.
     */
    class Noesia_SWA_Cron {

        /**
         * Module cron instance.
         *
         * @var object|null
         */
        private $module_cron = null;

        /**
         * Constructor kept for backward compatibility.
         */
        public function __construct() {
            $this->boot_module_cron();
        }

        /**
         * Load and instantiate the module-local cron owner.
         *
         * @return void
         */
        private function boot_module_cron() {
            $class_name = $this->get_cron_owner_class();

            if ( is_object( $this->module_cron ) && $this->module_cron instanceof $class_name ) {
                return;
            }

            if ( ! class_exists( $class_name ) ) {
                $module_file = $this->get_cron_owner_file();

                if ( '' !== $module_file && file_exists( $module_file ) ) {
                    require_once $module_file;
                }
            }

            if ( class_exists( $class_name ) ) {
                $this->module_cron = new $class_name();
            }
        }

        /**
         * Backward-compatible scheduler proxy.
         *
         * @return void
         */
        public static function schedule_event() {
            self::call_owner_method( 'schedule_event' );
        }

        /**
         * Backward-compatible unscheduler proxy.
         *
         * @return void
         */
        public static function clear_scheduled_event() {
            self::call_owner_method( 'clear_scheduled_event' );
        }

        /**
         * Backward-compatible cleanup proxy.
         *
         * @return void
         */
        public function daily_database_cleanup() {
            $this->boot_module_cron();

            if ( is_object( $this->module_cron ) && method_exists( $this->module_cron, 'daily_database_cleanup' ) ) {
                $this->module_cron->daily_database_cleanup();
            }
        }

        /**
         * Resolve the cron owner file.
         *
         * @return string
         */
        private function get_cron_owner_file() {
            $default = NOESIA_SWA_PLUGIN_DIR . 'modules/free/design/includes/class-noesia-swa-module-design-cron.php';

            /**
             * Filter the module-local cron owner file used by the legacy bridge.
             *
             * @param string           $default Default owner file.
             * @param Noesia_SWA_Cron  $this    Cron bridge instance.
             */
            return (string) apply_filters( 'noesia_swa_cron_owner_file', $default, $this );
        }

        /**
         * Resolve the cron owner class.
         *
         * @return string
         */
        private function get_cron_owner_class() {
            $default = 'Noesia_SWA_Module_Design_Cron';

            /**
             * Filter the module-local cron owner class used by the legacy bridge.
             *
             * @param string           $default Default owner class.
             * @param Noesia_SWA_Cron  $this    Cron bridge instance.
             */
            return (string) apply_filters( 'noesia_swa_cron_owner_class', $default, $this );
        }

        /**
         * Resolve the cron owner file statically.
         *
         * @return string
         */
        private static function resolve_cron_owner_file() {
            $default = NOESIA_SWA_PLUGIN_DIR . 'modules/free/design/includes/class-noesia-swa-module-design-cron.php';

            return (string) apply_filters( 'noesia_swa_cron_owner_file', $default, null );
        }

        /**
         * Resolve the cron owner class statically.
         *
         * @return string
         */
        private static function resolve_cron_owner_class() {
            $default = 'Noesia_SWA_Module_Design_Cron';

            return (string) apply_filters( 'noesia_swa_cron_owner_class', $default, null );
        }

        /**
         * Call a static owner method when available.
         *
         * @param string $method Method name.
         *
         * @return void
         */
        private static function call_owner_method( $method ) {
            $class_name = self::resolve_cron_owner_class();
            $module_file = self::resolve_cron_owner_file();

            if ( ! class_exists( $class_name ) && '' !== $module_file && file_exists( $module_file ) ) {
                require_once $module_file;
            }

            if ( class_exists( $class_name ) && method_exists( $class_name, $method ) ) {
                call_user_func( array( $class_name, $method ) );
            }
        }
    }
}
