<?php
include_once '../controllers/BannersController.php';

$banners = new BannersController();

try {
    if (isset($_POST['action'])) {
        $action = $_POST['action'];
    } else {
        throw new Exception('No se especificó ninguna acción');
    }

    switch ($action) {
        case 'getBanners':
            $banners->getBanners();
            break;
        case 'getBanner':
            $id = $_POST['id'];
            $banners->getBanner($id);
            break;
        case 'uploadBanner':
            $file = $_FILES['file'];
            $response = $banners->uploadBanner($file);
            echo json_encode($response);
            break;
        case 'updateOrder':
            $order = $_POST['order'];
            $response = $banners->updateOrder($order);
            echo json_encode($response);
            break;
        case 'deleteBanner':
            $id = $_POST['id'];
            $banners->deleteBanner($id);
            break;
        case 'updateBannerWithImage':
            $id = $_POST['id'];
            $file = isset($_FILES['file']) ? $_FILES['file'] : null; // Validar si el archivo existe
            $response = $banners->updateBannerWithImage($id, $file);
            echo json_encode($response);
            break;
        default:
            throw new Exception('Acción no válida');
    }
} catch (Exception $e) {
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}
