const Joi = require("joi");
const validate = require('../middleware/validate');
const {
    Rental
} = require("../models/rental");
const {
    Movie
} = require("../models/movie");
const auth = require("../middleware/auth");
const express = require("express");
const router = express.Router();

//GET
router.post("/", [auth, validate(validate)], async (req, res) => {

    const rental = await Rental.lookup(req.body.customerId, req.body.movieId);
    if (!rental) return res.status(404).send("rental not found!");

    if (rental.dateReturned)
        return res.status(400).send("Return already processed");

    rental.return();
    await rental.save();
    await Movie.update({
        _id: rental.movie._id
    }, {
        $inc: {
            numberInStock: 1
        }
    });

    return res.send(rental);
});

function validateReturn(req) {
    const schema = {
        customerId: Joi.objectId().required(),
        movieId: Joi.objectId().required()
    };
    return Joi.validate(genre, schema);
}

module.exports = router;