text: const express = require("express");
  const cors = require("cors");
  
  const { uuid, isUuid } = require("uuidv4");
  
  const app = express();
  
  app.use(express.json());
  app.use(cors());
  
  const repositories = [];
  
  function logRequests(request, response, next){
    const { method, url } = request;
  
    const logLabel = `[${method.toUpperCase()}] ${url}`;
    console.time(logLabel);
    
    next();// PROXIMO MIDDLEWARE
    console.timeEnd(logLabel);
  }
  
  function validateProjectId(request, response, next){
    const { id } = request.params;
    
    if(!isUuid(id)){
        return response.status(400).json({ error: 'Invalid repository ID.' });
    }
  
    return next();
  }
  
  app.use('/repositories/:id', validateProjectId);
  
  app.use(logRequests);
  
  app.get("/repositories", (request, response) => {
    return response.json(repositories);
  });
  
  app.post("/repositories", (request, response) => {
    const { title, url, techs } = request.body;
  
    const repository = {
      id: uuid(),
      title,
      url,
      techs,
      likes:0,
    }
  
    repositories.push(repository);
  
    return response.json(repository);
  
  });
  
  app.put("/repositories/:id", (request, response) => {
    const { id } = request.params;
    const { title, url, techs } = request.body;
   
    const repoIndex = repositories.findIndex(repository => repository.id === id);
    
    if(repoIndex < 0 ){
      return response.status(400).json({error: "Repository not found"});
    }
  
    repositories[repoIndex].title = title;
    repositories[repoIndex].url  = url;
    repositories[repoIndex].techs  = techs ;
  
    return response.json(repositories[repoIndex]);
  
  });
  
  app.delete("/repositories/:id", (request, response) => {
     const { id } = request.params;
  
     const repoIndex = repositories.findIndex(repository => repository.id === id);
        
     if(repoIndex  < 0){f
       return response.status(400).json({error: "Repository not found"});
     }
  
     repositories.splice(repoIndex, 1);
  
     return response.status(204).send();
  
  
  });
  
  app.post("/repositories/:id/like", (request, response) => {
     const { id } = request.params;
  
     const repository = repositories.find(repository => repository.id === id);
    
     if(!repository){
       return response.status(400).json({error: "Repository not found"});
     }
     repository.likes += 1;
     return response.json(repository);
  
  });
  
  module.exports = app;
  