<?php
/**
 * Admin Abandoned Carts API
 * Abandoned carts report and management
 */

require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../config/constants.php';
require_once __DIR__ . '/../../includes/Database.php';
require_once __DIR__ . '/../../includes/Auth.php';
require_once __DIR__ . '/../../includes/Cart.php';

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

// Check admin authentication
$auth = new Auth();
if (!$auth->isAdmin()) {
    http_response_code(401);
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

$cart = new Cart();
$db = Database::getInstance();
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? '';

try {
    switch ($method) {
        case 'GET':
            if ($action === 'list') {
                $hours = $_GET['hours'] ?? CART_ABANDONED_HOURS;
                $page = $_GET['page'] ?? 1;
                $perPage = $_GET['per_page'] ?? 20;
                
                $abandonedCarts = $cart->getAbandonedCarts($hours);
                
                // Get detailed info for each cart
                $detailedCarts = [];
                foreach ($abandonedCarts as $abandonedCart) {
                    $cartItems = $cart->getCartItems($abandonedCart['id']);
                    $cartTotal = $cart->getCartTotal($abandonedCart['id'], $abandonedCart['user_id']);
                    
                    $detailedCarts[] = [
                        'id' => $abandonedCart['id'],
                        'user_id' => $abandonedCart['user_id'],
                        'email' => $abandonedCart['email'],
                        'first_name' => $abandonedCart['first_name'],
                        'last_name' => $abandonedCart['last_name'],
                        'phone' => $abandonedCart['phone'],
                        'items_count' => $abandonedCart['items_count'],
                        'items' => $cartItems,
                        'total' => $cartTotal,
                        'created_at' => $abandonedCart['created_at'],
                        'updated_at' => $abandonedCart['updated_at'],
                        'last_email_sent' => $abandonedCart['last_email_sent'],
                        'email_count' => $abandonedCart['email_count']
                    ];
                }
                
                // Pagination
                $total = count($detailedCarts);
                $offset = ($page - 1) * $perPage;
                $paginatedCarts = array_slice($detailedCarts, $offset, $perPage);
                
                echo json_encode([
                    'success' => true,
                    'carts' => $paginatedCarts,
                    'total' => $total,
                    'page' => $page,
                    'per_page' => $perPage
                ]);
            }
            break;

        default:
            http_response_code(405);
            echo json_encode(['success' => false, 'message' => 'Method not allowed']);
    }
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['success' => false, 'message' => $e->getMessage()]);
}
?>

