<?php
/**
 * Event Author Fixer
 *
 * Ensures events created through Community Events are properly assigned to the creating user
 *
 * @package HVAC_Community_Events
 */

namespace HVAC_Community_Events;

/**
 * Class Event_Author_Fixer
 * 
 * Fixes event author assignment for Community Events
 */
class Event_Author_Fixer {
    
    /**
     * Constructor
     */
    public function __construct() {
        // Hook into Community Events submission process
        add_filter('tec_events_community_before_save_submission', array($this, 'ensure_author_is_set'), 10, 1);
        
        // Hook into event creation to ensure author is current user
        add_action('tribe_community_event_created', array($this, 'fix_event_author'), 10, 1);
        
        // Also hook into WordPress post data to ensure author is set
        add_filter('wp_insert_post_data', array($this, 'set_post_author'), 10, 2);
    }
    
    /**
     * Ensure author is set in submission data before saving
     *
     * @param array $submission The submission data
     * @return array Modified submission data
     */
    public function ensure_author_is_set($submission) {
        // If post_author is not set or is 0, set it to current user
        if (empty($submission['post_author']) || $submission['post_author'] == 0) {
            $current_user_id = get_current_user_id();
            if ($current_user_id > 0) {
                $submission['post_author'] = $current_user_id;
                HVAC_Logger::info('Setting event author to current user: ' . $current_user_id, 'EventAuthor');
            }
        }
        
        return $submission;
    }
    
    /**
     * Fix event author after creation
     *
     * @param int $event_id The event ID
     */
    public function fix_event_author($event_id) {
        $event = get_post($event_id);
        
        if ($event && $event->post_type === 'tribe_events') {
            $current_user_id = get_current_user_id();
            
            // If the event has no author or author is 0, set it to current user
            if (($event->post_author == 0 || empty($event->post_author)) && $current_user_id > 0) {
                wp_update_post(array(
                    'ID' => $event_id,
                    'post_author' => $current_user_id
                ));
                
                HVAC_Logger::info('Fixed event author for event ' . $event_id . ' to user ' . $current_user_id, 'EventAuthor');
            }
        }
    }
    
    /**
     * Set post author when inserting tribe_events post
     *
     * @param array $data    The post data
     * @param array $postarr The post array
     * @return array Modified post data
     */
    public function set_post_author($data, $postarr) {
        // Only handle tribe_events posts
        if ($data['post_type'] === 'tribe_events') {
            $current_user_id = get_current_user_id();
            
            // If no author is set and we have a logged-in user, set the author
            if ((empty($data['post_author']) || $data['post_author'] == 0) && $current_user_id > 0) {
                $data['post_author'] = $current_user_id;
                HVAC_Logger::info('Setting tribe_events post author to: ' . $current_user_id, 'EventAuthor');
            }
        }
        
        return $data;
    }
}