<?php
/**
 * Design / Live Requests cron bootstrap.
 *
 * Transitional module-local owner for the daily cleanup of completed requests.
 * Keeps the same cleanup behavior while moving ownership out of the central
 * kernel.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_SWA_Module_Design_Cron' ) ) {
    /**
     * Handles scheduled cleanup tasks for the Design module.
     */
    class Noesia_SWA_Module_Design_Cron {

        /**
         * Cron hook name.
         */
        const CRON_HOOK = 'noesia_swa_daily_cleanup';

        /**
         * Register cron callback.
         */
        public function __construct() {
            add_action( self::CRON_HOOK, array( $this, 'daily_database_cleanup' ) );
        }

        /**
         * Ensure the daily cleanup event is scheduled.
         *
         * @return void
         */
        public static function schedule_event() {
            if ( ! wp_next_scheduled( self::CRON_HOOK ) ) {
                wp_schedule_event( time(), 'daily', self::CRON_HOOK );
            }
        }

        /**
         * Remove the scheduled cleanup event.
         *
         * @return void
         */
        public static function clear_scheduled_event() {
            $timestamp = wp_next_scheduled( self::CRON_HOOK );

            while ( false !== $timestamp ) {
                wp_unschedule_event( $timestamp, self::CRON_HOOK );
                $timestamp = wp_next_scheduled( self::CRON_HOOK );
            }
        }

        /**
         * Delete completed request posts older than the retention window.
         *
         * @return void
         */
        public function daily_database_cleanup() {
            $days_to_keep = 15;
            $cutoff_time  = time() - ( $days_to_keep * DAY_IN_SECONDS );

            $old_posts = new WP_Query(
                array(
                    'post_type'      => 'noesia_request',
                    'post_status'    => 'publish',
                    'posts_per_page' => -1,
                    'fields'         => 'ids',
                    // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Active/completed request state is stored in post meta for this module and the current CPT volume is expected to stay small.
                    'meta_query'     => array(
                        array(
                            'key'     => 'noesia_status',
                            'value'   => 'completed',
                            'compare' => '=',
                        ),
                    ),
                    'date_query'     => array(
                        array(
                            'before'    => gmdate( 'Y-m-d H:i:s', $cutoff_time ),
                            'inclusive' => true,
                        ),
                    ),
                )
            );

            if ( empty( $old_posts->posts ) ) {
                return;
            }

            foreach ( $old_posts->posts as $post_id ) {
                wp_delete_post( (int) $post_id, true );
            }
        }
    }
}
