-- available bookings - task 3
-- changes:
--   changed BookingDate (Reserved) data type from date to datetime, because I assumed that Little Lemon
--   allows wants bookings per day and table - a table is considered blocked for 2 hours
--   added CustomerID and EmployeeID input parameters
-- noqa: disable=CP03,LT02,RF03
--
create procedure AddValidBooking (
    in Reserved datetime, in TableNumber int, in CustomerID int, in EmployeeID int
)
begin
    start transaction;

    if TableIsFree(Reserved, TableNumber) then
        insert into Bookings (Reserved, TableNumber, CustomerID, EmployeeID) values (
            Reserved, TableNumber, CustomerID, EmployeeID
        );
        commit;
        select concat(
            "Table ", TableNumber,
            " was free - booking accepted (BookingID=", (select last_insert_id()), ')'
        ) as BookingStatus;
    else
        rollback;
        select concat(
            "Table ", TableNumber,
            " is already booked - booking cancelled"
        ) as BookingStatus;
    end if;
end
