<?php

class EveAI_Sync {
    private $api;
    private $post_handler;
    private $admin;

    public function init() {
        $this->load_dependencies();
        $this->setup_actions();
    }

    private function load_dependencies() {
        require_once EVEAI_SYNC_PLUGIN_DIR . 'includes/class-eveai-api.php';
        require_once EVEAI_SYNC_PLUGIN_DIR . 'includes/class-eveai-post-handler.php';
        require_once EVEAI_SYNC_PLUGIN_DIR . 'includes/class-eveai-admin.php';
        require_once EVEAI_SYNC_PLUGIN_DIR . 'includes/class-eveai-bulk-sync.php';

        $this->api = new EveAI_API();
        $this->post_handler = new EveAI_Post_Handler($this->api);
        $this->admin = new EveAI_Admin($this->api);
    }

    private function setup_actions() {
        add_action('save_post', array($this->post_handler, 'handle_post_save'), 10, 3);
        add_action('before_delete_post', array($this->post_handler, 'handle_post_delete'));
        add_action('admin_init', array($this->admin, 'register_settings'));
        add_action('admin_menu', array($this->admin, 'add_admin_menu'));
        add_action('add_meta_boxes', array($this->admin, 'add_sync_meta_box'));
        add_action('eveai_sync_post', array($this->post_handler, 'sync_post'), 10, 2);
        add_action('wp_ajax_eveai_bulk_sync', array($this->admin, 'handle_bulk_sync_ajax'));
        // Additional delete hooks
        add_action('trashed_post', array($this->post_handler, 'handle_post_delete'));
        add_action('wp_trash_post', array($this->post_handler, 'handle_post_delete'));

        // Add debug logging for delete actions
        add_action('before_delete_post', function($post_id) {
            error_log("EveAI Debug: before_delete_post triggered for post {$post_id}");
        });
        add_action('trashed_post', function($post_id) {
            error_log("EveAI Debug: trashed_post triggered for post {$post_id}");
        });
        add_action('wp_trash_post', function($post_id) {
            error_log("EveAI Debug: wp_trash_post triggered for post {$post_id}");
        });
    }
}
