<?php
/**
 * AYSA Permalink Handler
 * 
 * Handles AJAX requests to get real permalinks for entities
 */

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

class Aysa_Permalink_Handler {
    
    public function __construct() {
        add_action('wp_ajax_aysa_get_permalink', array($this, 'get_permalink'));
    }
    
    /**
     * Get permalink for an entity via AJAX
     */
    public function get_permalink() {
        if (!current_user_can('edit_posts')) {
            wp_send_json_error('Insufficient permissions', 403);
            return;
        }

        // Verify nonce
        $nonce = isset($_POST['nonce']) ? sanitize_text_field((string) wp_unslash($_POST['nonce'])) : '';
        if ($nonce === '' || !wp_verify_nonce($nonce, 'aysa_get_permalink_nonce')) {
            wp_send_json_error('Security check failed');
            return;
        }
        
        $entity_type = isset($_POST['entity_type']) ? sanitize_text_field((string) wp_unslash($_POST['entity_type'])) : '';
        $entity_id = isset($_POST['entity_id']) ? intval(wp_unslash($_POST['entity_id'])) : 0;
        
        if (!$entity_id || $entity_id <= 0) {
            wp_send_json_error('Invalid entity ID');
            return;
        }
        
        $permalink = $this->generate_permalink($entity_type, $entity_id);
        
        if ($permalink) {
            wp_send_json_success(array(
                'permalink' => $permalink,
                'entity_type' => $entity_type,
                'entity_id' => $entity_id
            ));
        } else {
            wp_send_json_error('Could not generate permalink');
        }
    }
    
    /**
     * Generate permalink based on entity type and ID
     */
    private function generate_permalink($entity_type, $entity_id) {
        switch (strtolower($entity_type)) {
            case 'post':
                $post = get_post($entity_id);
                if ($post && $post->post_status === 'publish') {
                    return get_permalink($entity_id);
                }
                break;
                
            case 'page':
                $page = get_post($entity_id);
                if ($page && $page->post_type === 'page' && $page->post_status === 'publish') {
                    return get_permalink($entity_id);
                }
                break;
                
            case 'product':
                // Check if WooCommerce is active
                if (class_exists('WooCommerce')) {
                    $product = wc_get_product($entity_id);
                    if ($product && $product->get_status() === 'publish') {
                        return get_permalink($entity_id);
                    }
                } else {
                    // Fallback for custom product post type
                    $post = get_post($entity_id);
                    if ($post && $post->post_type === 'product' && $post->post_status === 'publish') {
                        return get_permalink($entity_id);
                    }
                }
                break;
                
            case 'category':
            case 'product_cat':
                $term = get_term($entity_id, $entity_type === 'category' ? 'category' : 'product_cat');
                if ($term && !is_wp_error($term)) {
                    return get_term_link($term);
                }
                break;
                
            case 'tag':
            case 'product_tag':
                $term = get_term($entity_id, $entity_type === 'tag' ? 'post_tag' : 'product_tag');
                if ($term && !is_wp_error($term)) {
                    return get_term_link($term);
                }
                break;
                
            default:
                // Try generic post lookup
                $post = get_post($entity_id);
                if ($post && $post->post_status === 'publish') {
                    return get_permalink($entity_id);
                }
        }
        
        return false;
    }
}

// Initialize the handler
new Aysa_Permalink_Handler();
