<?php
require_once '../../../../wp-load.php';

header('Content-Type: application/json');

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['error' => 'Invalid request method']);
    exit;
}

// Decode the input JSON
$postData = json_decode(file_get_contents('php://input'), true);
$action = $postData['action'] ?? 'getTemplates';
$url = '';
$request_method = 'GET';
$data = [];

switch ($action) {
    case 'getTemplates':
        $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_theme_templates';
        $request_method = 'POST';

        $data = [
            'per_page' => !empty($postData['per_page']) ? (int) $postData['per_page'] : 1,
            'page' => !empty($postData['page']) ? (int) $postData['page'] : 1
        ];

        if (!empty($postData['product_cat'])) {
            $data['product_cat'] = $postData['product_cat'];
        }

        if (!empty($postData['search'])) {
            $data['search'] = $postData['search'];
        }

        if (!empty($postData['free_theme_domain'])) {
            $data['free_theme_domain'] = $postData['free_theme_domain'];
        }

        if (!empty($postData['free_pro'])) {
            $allowed = ['Free', 'Pro'];
            $free_pro = $postData['free_pro'];
            if (in_array($free_pro, $allowed, true)) {
                $data['free_pro'] = $free_pro;
            }
        }

        break;

    case 'getCategories':
        $url = FLEXIMP_JSON_DATA_URL . 'flex_theme_get_product_categories';
        $request_method = 'GET';
        break;

    default:
        echo json_encode(['error' => 'Invalid action']);
        exit;
}

$args = [
    'method' => $request_method,
    'headers' => [
        'Content-Type' => 'application/json',
    ],
];

if ($request_method === 'POST') {
    $args['body'] = json_encode($data);
}

$response = ($request_method === 'POST') ? wp_remote_post($url, $args) : wp_remote_get($url, $args);

if (is_wp_error($response)) {
    echo json_encode(['error' => 'Request failed: ' . $response->get_error_message()]);
    exit;
}

$http_code = wp_remote_retrieve_response_code($response);
if ($http_code !== 200) {
    echo json_encode(['error' => 'HTTP error: ' . $http_code]);
    exit;
}

$response_body = wp_remote_retrieve_body($response);
$data = json_decode($response_body, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    echo json_encode(['error' => 'Invalid JSON format received from the external API']);
    exit;
}

// Filter free_template based on free_pro parameter for getTemplates action
if ($action === 'getTemplates' && !empty($postData['free_pro'])) {
    $requested_type = $postData['free_pro']; // 'Free' or 'Pro'
    
    // Check if free_template exists in the response
    if (isset($data['data']['free_template']) && is_array($data['data']['free_template'])) {
        $free_template_type = $data['data']['free_template']['metafields']['free_pro'] ?? '';
        
        // If free_template doesn't match requested type, remove it
        if ($free_template_type !== $requested_type) {
            unset($data['data']['free_template']);
        }
    }
    
    // Also check nested structure
    if (isset($data['data']['data']['free_template']) && is_array($data['data']['data']['free_template'])) {
        $free_template_type = $data['data']['data']['free_template']['metafields']['free_pro'] ?? '';
        
        // If free_template doesn't match requested type, remove it
        if ($free_template_type !== $requested_type) {
            unset($data['data']['data']['free_template']);
        }
    }
}

echo json_encode(['success' => true, 'data' => $data]);




