<?php

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

use AC_SYNC\Includes\Classes\Api\Action_Wear_Api_Route;
use AC_SYNC\Includes\Classes\Api\Action_Wear_API_Response as ApiResponse;
use AC_SYNC\Includes\Classes\Order\Action_Wear_Order as Order;

$route = new Action_Wear_Api_Route("/get_actionwear_orders");
$route->setMethods("GET");
$route->setCallback(function () {
    $response = new ApiResponse();

    $page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
    $per_page = isset($_GET['per_page']) ? min(100, max(1, intval($_GET['per_page']))) : 20;
    $sync_status = isset($_GET['sync_status']) && !empty($_GET['sync_status']) ? sanitize_text_field($_GET['sync_status']) : null;

    $result = Order::getAllActionWearOrders($page, $per_page, $sync_status);

    $response->success([
        'orders' => $result['orders'],
        'total' => $result['total'],
        'page' => $page,
        'per_page' => $per_page,
        'total_pages' => ceil($result['total'] / $per_page)
    ]);

    return $response->getResponse();
});
$route->registerRoute();
