<?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    exit(0);
}

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../models/RentalPrice.php';

$database = new Database();
$db = $database->getConnection();

$rentalPrice = new RentalPrice($db);

$request_method = $_SERVER['REQUEST_METHOD'];

switch($request_method) {
    case 'GET':
        if(isset($_GET['id'])) {
            $rentalPrice->id = $_GET['id'];
            $rentalPrice->readOne();
            
            if($rentalPrice->item_id != null) {
                $rental_arr = array(
                    "id" => $rentalPrice->id,
                    "item_id" => $rentalPrice->item_id,
                    "client_id" => $rentalPrice->client_id,
                    "start_date" => $rentalPrice->start_date,
                    "end_date" => $rentalPrice->end_date,
                    "daily_price" => $rentalPrice->daily_price,
                    "created_at" => $rentalPrice->created_at,
                    "updated_at" => $rentalPrice->updated_at
                );
                
                http_response_code(200);
                echo json_encode($rental_arr);
            } else {
                http_response_code(404);
                echo json_encode(array("message" => "Rental price not found."));
            }
        } elseif(isset($_GET['item_id'])) {
            $rentalPrice->item_id = $_GET['item_id'];
            $stmt = $rentalPrice->read();
            $num = $stmt->rowCount();
            
            if($num > 0) {
                $rentals_arr = array();
                $rentals_arr["records"] = array();
                
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
                    extract($row);
                    
                    $rental_item = array(
                        "id" => $id,
                        "item_id" => $item_id,
                        "client_id" => $client_id,
                        "start_date" => $start_date,
                        "end_date" => $end_date,
                        "daily_price" => $daily_price,
                        "created_at" => $created_at,
                        "updated_at" => $updated_at
                    );
                    
                    array_push($rentals_arr["records"], $rental_item);
                }
                
                http_response_code(200);
                echo json_encode($rentals_arr);
            } else {
                http_response_code(200);
                echo json_encode(array("records" => array()));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Missing item_id parameter."));
        }
        break;
        
    case 'POST':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->item_id) && !empty($data->start_date) && !empty($data->end_date) && !empty($data->daily_price)) {
            $rentalPrice->item_id = $data->item_id;
            $rentalPrice->client_id = $data->client_id ?? null;
            $rentalPrice->start_date = $data->start_date;
            $rentalPrice->end_date = $data->end_date;
            $rentalPrice->daily_price = $data->daily_price;
            
            if($rentalPrice->create()) {
                http_response_code(201);
                echo json_encode(array("message" => "Rental price was created."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to create rental price."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to create rental price. Data is incomplete."));
        }
        break;
        
    case 'PUT':
        $data = json_decode(file_get_contents("php://input"));
        
        if(!empty($data->id) && !empty($data->item_id) && !empty($data->start_date) && !empty($data->end_date) && !empty($data->daily_price)) {
            $rentalPrice->id = $data->id;
            $rentalPrice->item_id = $data->item_id;
            $rentalPrice->client_id = $data->client_id ?? null;
            $rentalPrice->start_date = $data->start_date;
            $rentalPrice->end_date = $data->end_date;
            $rentalPrice->daily_price = $data->daily_price;
            
            if($rentalPrice->update()) {
                http_response_code(200);
                echo json_encode(array("message" => "Rental price was updated."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to update rental price."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to update rental price. Data is incomplete."));
        }
        break;
        
    case 'DELETE':
        if(isset($_GET['id'])) {
            $rentalPrice->id = $_GET['id'];
            
            if($rentalPrice->delete()) {
                http_response_code(200);
                echo json_encode(array("message" => "Rental price was deleted."));
            } else {
                http_response_code(503);
                echo json_encode(array("message" => "Unable to delete rental price."));
            }
        } else {
            http_response_code(400);
            echo json_encode(array("message" => "Unable to delete rental price. ID is missing."));
        }
        break;
        
    default:
        http_response_code(405);
        echo json_encode(array("message" => "Method not allowed."));
        break;
}
?>
