import Task from "../models/task.model.js"
import { verifyToken } from "../token/token.js"
import express from "express"

const taskRouter = express.Router()

// add

taskRouter.post("/add", verifyToken, async(req, res) =>{
    try{
        const { title, completed } = req.body
        if(!title || !completed){
            return res.status(400).json({ message: "all fields are required please"})
        }
        const task = await Task.findOne({ title })
        if(task){
            return res.status(400).json({ message: "task already exists"})
        }

        const createdTask = await Task.create({ title, completed, owner: req.id })
        return res.status(201).json({ message: "task added successfully", task: createdTask })
    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "server error"})
    }
})

// deelete 

taskRouter.delete("/delete/:id", verifyToken, async(req, res) => {
    try {
        const { id } = req.params
        const task = await Task.findById(id)
        if(!task){
            return res.status(400).json({ message: "task not found"})
        }
        // res.send({ owner: task.owner, id: req.id })
        if(task.owner != req.id){
            return res.status(400).json({ message: "you are not the owner of this task"})
        }
        await Task.deleteOne({ _id: id })
        return res.status(200).json({ message: "task deleted successfully"})
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "server error"})
    }
})

// update

taskRouter.put("/update/:id", verifyToken, async(req, res) => {
    try{
        const { id } = req.params
        const { title, completed } = req.body
        const fieldsToUpdate = {}
        if(title !== undefined) fieldsToUpdate.title = title
        if(completed !== undefined) fieldsToUpdate.completed = completed

        if(Object.keys(fieldsToUpdate).length === 0){
            return res.status(400).json({ message: "all fields are required please"})
        }

        const task = await Task.findById(id)
        if(!task){
            return res.status(400).json({ message: "task not found"})
        }
        if(task.owner != req.id){
            return res.status(400).json({ message: "you are not the owner of this task"})
        }
        await Task.findByIdAndUpdate(id, { $set: fieldsToUpdate })
        return res.status(200).json({ message: "task updated successfully"})
    }catch(error){
        console.log(error)
        return res.status(500).json({ message: "server error"})
    }
})

// get all 

taskRouter.get("/", verifyToken, async(req, res) => {
    try {
        const tasks = await Task.find({ owner: req.id })
        return res.status(200).json({ tasks })
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "server error"})
    }
})

// get by id

taskRouter.get("/:id", verifyToken, async(req, res) => {
    try {
        const { id } = req.params
        const task = await Task.findById(id)
        if(!task){
            return res.status(400).json({ message: "task not found"})
        }
        if(task.owner !== req.id){
            return res.status(400).json({ message: "you are not the owner of this task"})
        }
        return res.status(200).json({ task })
    } catch (error) {
        console.log(error)
        return res.status(500).json({ message: "server error"})
    }
})

export default taskRouter