<?php 
require_once "../../../../dream/server/config.php";

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

$rooms = json_decode('{
	"Rooms": {
		"s_Meal": {
			"cafe": {
				"value": "vegetarian",
				"bookings": [
					1,
					2,
					3,
					4,
					6,
					7,
					8,
					9,
					10,
					11,
					12,
					13,
					14,
					15,
					16,
					17,
					18
				],
				"capacity": 17,
				"optional": "",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 0,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 0,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 0,
						"nbookings": 0
					}
				},
				"nbookings": 17,
				"expression": true,
				"overbooking": 0,
				"seatreservation": false
			}
		},
		"s_Session": {
			"zoom": {
				"value": "remote",
				"bookings": [
					20,
					22,
					23,
					26
				],
				"capacity": 20,
				"optional": "",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 16,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [
							25
						],
						"available": 15,
						"nbookings": 1
					},
					"2025-09-28": {
						"bookings": [],
						"available": 16,
						"nbookings": 0
					},
					"2025-09-29": {
						"bookings": [],
						"available": 16,
						"nbookings": 0
					}
				},
				"nbookings": 4,
				"expression": true,
				"overbooking": 0,
				"seatreservation": false
			},
			"temple": {
				"value": "local",
				"bookings": [
					1,
					2,
					3,
					4,
					5,
					6,
					7,
					8,
					9,
					10,
					11,
					12,
					13,
					14,
					15,
					16,
					17,
					18,
					19,
					21,
					24,
					27
				],
				"capacity": 30,
				"optional": "",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 8,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 8,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 8,
						"nbookings": 0
					},
					"2025-09-29": {
						"bookings": [],
						"available": 8,
						"nbookings": 0
					}
				},
				"nbookings": 22,
				"expression": true,
				"overbooking": 0,
				"seatreservation": false
			}
		},
		"s_Translation": {
			"remote": {
				"value": "pt",
				"bookings": [
					20,
					23,
					26
				],
				"capacity": 16,
				"optional": "(!isset($bookingjson[\"s_Session\"]) ? true : ($bookingjson[\"s_Session\"]==\"remote\"))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-29": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					}
				},
				"nbookings": 3,
				"expression": "$bookingjson[\"s_Session\"]==\"remote\"",
				"overbooking": 0,
				"seatreservation": false
			},
			"temple": {
				"value": "pt",
				"bookings": [
					18,
					27
				],
				"capacity": 15,
				"optional": "(!isset($bookingjson[\"s_Session\"]) ? true : ($bookingjson[\"s_Session\"]==\"local\"))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					},
					"2025-09-29": {
						"bookings": [],
						"available": 13,
						"nbookings": 0
					}
				},
				"nbookings": 2,
				"expression": "$bookingjson[\"s_Session\"]==\"local\"",
				"overbooking": 0,
				"seatreservation": false
			}
		},
		"s_Accommodation": {
			"s1": {
				"value": "single",
				"bookings": [],
				"capacity": 1,
				"optional": "((!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"dF1": {
				"value": "double",
				"bookings": [],
				"capacity": 2,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"dF2": {
				"value": "double",
				"bookings": [],
				"capacity": 2,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 2,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"sd1": {
				"type": "valueXOR",
				"value": [
					"double",
					"double",
					"single"
				],
				"bookings": [
					27
				],
				"capacity": [
					2,
					2,
					1
				],
				"optional": [
					"((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
					"((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"male\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
					"((!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))"
				],
				"quantity": 1,
				"selected": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 1,
						"nbookings": 0
					}
				},
				"nbookings": 1,
				"expression": [
					"($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
					"($bookingjson[\"gender\"]==\"male\" AND $bookingjson[\"age\"]==\"adult\")",
					"($bookingjson[\"age\"]==\"adult\")"
				],
				"overbooking": [
					0,
					0,
					0
				],
				"seatreservation": true,
				"allowreconfiguration": false
			},
			"sd2": {
				"type": "valueXOR",
				"value": [
					"double",
					"double",
					"single"
				],
				"bookings": [],
				"capacity": [
					2,
					2,
					1
				],
				"optional": [
					"((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
					"((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"male\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
					"((!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))"
				],
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": [
					"($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
					"($bookingjson[\"gender\"]==\"male\" AND $bookingjson[\"age\"]==\"adult\")",
					"($bookingjson[\"age\"]==\"adult\")"
				],
				"overbooking": [
					0,
					0,
					0
				],
				"seatreservation": true,
				"allowreconfiguration": false
			},
			"quadF1": {
				"value": "dormitory",
				"bookings": [],
				"capacity": 4,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"quadF2": {
				"value": "dormitory",
				"bookings": [],
				"capacity": 4,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"female\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"gender\"]==\"female\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"quadM1": {
				"value": "dormitory",
				"bookings": [
					1
				],
				"capacity": 4,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"male\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 3,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 3,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 3,
						"nbookings": 0
					}
				},
				"nbookings": 1,
				"expression": "($bookingjson[\"gender\"]==\"male\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"quadM2": {
				"value": "dormitory",
				"bookings": [],
				"capacity": 4,
				"optional": "((!isset($bookingjson[\"gender\"]) ? true : ($bookingjson[\"gender\"]==\"male\")) AND (!isset($bookingjson[\"age\"]) ? true : ($bookingjson[\"age\"]==\"adult\")))",
				"quantity": 1,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 4,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": "($bookingjson[\"gender\"]==\"male\" AND $bookingjson[\"age\"]==\"adult\")",
				"overbooking": 0,
				"seatreservation": false
			},
			"camping": {
				"value": "camping",
				"bookings": [],
				"capacity": 1,
				"optional": "",
				"quantity": 5,
				"intervals": {
					"2025-09-26": {
						"bookings": [],
						"available": 5,
						"nbookings": 0
					},
					"2025-09-27": {
						"bookings": [],
						"available": 5,
						"nbookings": 0
					},
					"2025-09-28": {
						"bookings": [],
						"available": 5,
						"nbookings": 0
					}
				},
				"nbookings": 0,
				"expression": true,
				"overbooking": 0,
				"seatreservation": false
			}
		}
	}
}',true);

$newrooms = $rooms["Rooms"];
removeBookingNumberFromRooms($newrooms,6);

exit(json_encode($newrooms));

function removeBookingNumberFromRooms(&$rooms,$number) {
    foreach( $rooms as &$roomtype ) {
        foreach( $roomtype as &$room ) {
            // search booking number in bookings list for all intervals
			$key=array_search($number, $room["bookings"]);
            if( $key!==false ) {
                // found, so remove it from list and increase available for all intervals by one
                unset($room["bookings"][$key]);
				$room["bookings"] = array_values($room["bookings"]);
                $room["nbookings"]--;
                foreach( $room["intervals"] as &$interval ) {
                    $interval["available"]++;
                }
            } else {
                // search each interval
                foreach( $room["intervals"] as &$interval ) {
					$key=array_search($number, $interval["bookings"]);
                    if( $key!==false ) {
                        // found, so remove it from list and increase available for all intervals by one
                        unset($interval["bookings"][$key]);
						$interval["bookings"] = array_values($interval["bookings"]);
                        $interval["nbookings"]--;
                        $interval["available"]++;
                    }
                }
				unset($interval);
            }
        }
		unset($room);
    }
	unset($roomtype);
}