require("dotenv").config();
const express = require('express');
const mongoose = require("mongoose")
const cors = require('cors');

const port = 3001;
const app = express();

app.use(express.json());
app.use(cors());

(async () => {
  try {
    mongoose.set("autoIndex", false);

    await mongoose.connect(process.env.MONGO_URI);
    console.log("✅ MongoDB Connected");

    await Task.syncIndexes();
    console.log("✅ Indexes created");

    app.listen(port, () => {
      console.log(`✅ To Do App listening on port ${port}`);
    });
  } catch (error) {
    console.error(`Startup Error! ${error}`);
  }
})();

// Task Schema
const taskSchema = new mongoose.Schema({
  title: { type: String, required: true },
  completed: { type: Boolean, required: true, default: false },
  description: { type: String, required: true },
  dueDate: { type: Date, required: true },
  dateCreated: { type: Date, required: true, default: new Date() }
})

taskSchema.index({ dueDate: 1 })
taskSchema.index({ dateCreated: 1 })

const Task = mongoose.model("Task", taskSchema);

// this api route gets all the tasks from our database.
// if the request included a query string, use that for our sorting.
app.get('/api/tasks', async (req, res) => {
  try {
    const { sortBy } = req.query; // destructs the sortBy from the query string. e.g. ?sortBy=dueDate or ?sortBy=dateCreated
    let sortOption = {}; // create an empty sort object for the database query

    if (sortBy === 'dueDate') {
      sortOption = { dueDate: 1 }; // if the sortBy query string is dueDate, set the sortOption to dueDate and set the order to ascending (1)
    } else if (sortBy === 'dateCreated') {
      sortOption = { dateCreated: 1 }; // if the sortBy query string is dateCreated, set the sortOption to dateCreated and set the order to ascending (1)
    }

    const tasks = await Task.find({}).sort(sortOption); // fetch all task documents and sort them using sortOption

    if (!tasks) {
      // return the following if the query failed to return a value
      return res.status(404).json({ message: "Tasks not found!" });
    }

    res.json(tasks); // send a json response with the task objects
  } catch (error) {
    // if there are any errors, do the following
    console.error("Error:", error);
    res.status(500).json({ message: "Error grabbing tasks!" });

  }
});

// this api route takes post requests to save a new MongoDB document, aka, our task
app.post('/api/tasks/todo', async (req, res) => {
  try {
    const { title, description, dueDate } = req.body; //destructures the request body
    const taskData = { title, description, dueDate }; // creating the object to be used for creating the task
    const createTask = new Task(taskData); // instantiates a new Task document/model instance using the taskData object values
    const newTask = await createTask.save(); // this saves the new document to MongoDB using the Task model/schema.

    res.json({ task: newTask, message: "New task created successfully!" }); // if there were no error above, send a response where the body is an JSON object.

  } catch (error) {
    // if there are any errors, do the following
    console.error("Error:", error);
    res.status(500).json({ message: "Error creating the task!" });
  }

});

// this api route marks a task as complete using its id.

app.patch('/api/tasks/complete/:id', async (req, res) => {
  try {
    const { completed } = req.body; // expected boolean value coming from the frontend
    const taskId = req.params.id; // grab the task id from the URL params
    const completedTask = await Task.findByIdAndUpdate(taskId, { completed }, { new: true }); // update completed status and return updated document

    if (!completedTask) {
      // return 404 if no task matches the provided id
      return res.status(404).json({ message: "Task not found!" });

    }
    // return updated task document and confirmation message
    res.json({ task: completedTask, message: "Task set to 'Complete'" });

  } catch (error) {
    // return 500 if database update fails
    console.error("Error:", error);
    res.status(500).json({ message: "Error completing the task!" });

  }
});

// this api route marks a task as not complete using its id.

app.patch('/api/tasks/notComplete/:id', async (req, res) => {

  try {
    const { completed } = req.body; // expected boolean value coming from the frontend
    const taskId = req.params.id; // grab the task id from the URL params
    const taskNotComplete = await Task.findByIdAndUpdate(taskId, { completed }, { new: true }); // update completed status and return updated document

    if (!taskNotComplete) {
      // return 404 if no task matches the provided id
      return res.status(404).json({ message: "Task not found!" });
    }
    // return updated task document and confirmation message
    res.json({ task: taskNotComplete, message: "Task set to 'Not Complete'" });
  } catch (error) {
    // return 500 if database update fails
    console.error("Error:", error);
    res.status(500).json({ message: "Error making the task NOT complete!" });
  }
});

// this api route deletes a task by id.
app.delete('/api/tasks/delete/:id', async (req, res) => {
  try {
    const taskId = req.params.id; // grab the task id from the URL params
    const deletedTask = await Task.findByIdAndDelete(taskId); // delete the matching task document

    if (!deletedTask) {
      // return 404 if no task matches the provided id
      return res.status(404).json({ message: "Task not found!" });
    }

    // return deleted task document and confirmation message
    res.json({ task: deletedTask, message: "Task deleted successfully!" });

  } catch (error) {
    // return 500 if deletion fails
    console.error("Error:", error);
    res.status(500).json({ message: "Error deleting the task!" });

  }
});

// this api route updates a task's editable fields by id.

app.put('/api/tasks/update/:id', async (req, res) => {

  try {
    const taskId = req.params.id; // grab the task id from the URL params
    const { title, description, dueDate } = req.body;  // extract updated fields from request body
    const taskData = { title, description, dueDate }; // build the update object
    const updatedTask = await Task.findByIdAndUpdate(taskId, taskData, { new: true }); // apply update and return updated document

    if (!updatedTask) {
      // return 404 if no task matches the provided id
      return res.status(404).json({ message: "Task not found!" });
    }

    // return updated task document and confirmation message
    res.json({ task: updatedTask, message: "Task updated successfully!" });

  } catch (error) {
    // return 500 if update fails
    console.error('Error:', error);
    res.status(500).json({ message: "Error updating the task!" });

  }

});
