<?php
/**
 * Event Event Subscriber
 *
 * Handles event-related events
 *
 * @package NemtlyBooking
 * @since   PBOOKING_SINCE
 */

namespace NemtlyBooking\Subscribers;

use NemtlyBooking\Interfaces\Event_Subscriber_Interface;

/**
 * Event Event Subscriber class
 *
 * @since PBOOKING_SINCE
 */
class Event_Event_Subscriber implements Event_Subscriber_Interface {

    /**
     * Get subscribed events
     *
     * @since PBOOKING_SINCE
     *
     * @return array Array of event names and their corresponding methods.
     */
    public function get_subscribed_events() {
        return [
            'event.created'     => 'on_event_created',
            'event.updated'     => 'on_event_updated',
            'event.deleted'     => 'on_event_deleted',
            'event.published'   => 'on_event_published',
            'event.unpublished' => 'on_event_unpublished',
        ];
    }

    /**
     * Handle event created event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_event_created( $data ) {
        // Clear related caches
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_clear_event_cache', $data['event_id'] );
        }

        // Update event statistics
        do_action( 'nemtly_booking_update_event_stats' );

        return $data;
    }

    /**
     * Handle event updated event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_event_updated( $data ) {
        // Clear related caches
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_clear_event_cache', $data['event_id'] );
        }

        // Notify existing bookings if significant changes
        if ( isset( $data['event_id'] ) && isset( $data['changes'] ) ) {
            $significant_changes     = [ 'event_date', 'event_time', 'location', 'capacity' ];
            $has_significant_changes = ! empty( array_intersect( $significant_changes, array_keys( $data['changes'] ) ) );

            if ( $has_significant_changes ) {
                do_action( 'nemtly_booking_notify_booking_changes', $data['event_id'], $data['changes'] );
            }
        }

        return $data;
    }

    /**
     * Handle event deleted event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_event_deleted( $data ) {
        // Clear related caches
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_clear_event_cache', $data['event_id'] );
        }

        // Cancel all bookings for this event
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_cancel_event_bookings', $data['event_id'] );
        }

        // Update event statistics
        do_action( 'nemtly_booking_update_event_stats' );

        return $data;
    }

    /**
     * Handle event published event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_event_published( $data ) {
        // Clear related caches
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_clear_event_cache', $data['event_id'] );
        }

        // Update event statistics
        do_action( 'nemtly_booking_update_event_stats' );

        return $data;
    }

    /**
     * Handle event unpublished event
     *
     * @since PBOOKING_SINCE
     *
     * @param array $data Event data.
     *
     * @return array Modified event data.
     */
    public function on_event_unpublished( $data ) {
        // Clear related caches
        if ( isset( $data['event_id'] ) ) {
            do_action( 'nemtly_booking_clear_event_cache', $data['event_id'] );
        }

        // Update event statistics
        do_action( 'nemtly_booking_update_event_stats' );

        return $data;
    }
}
