const Router = require('express')
const routers = new Router();
const authMiddlewere = require('../middleware/auth')
const Project = require('../models/Project')
const User = require("../models/User");


routers.post(
    '/save',
    async (req,res)=>{
        try{
            const{title,codeHTML,codeCSS,codeJS,user,email}= req.body
            const candidate = await Project.findOne({title})
            if(candidate){
                return res.status(400).json({message:"this project already exists"})
            }
            const project = new Project({title,codeHTML,codeCSS,codeJS,user,email})
            await project.save()
            return res.json({message:"Project created"})
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)
routers.post(
    '/reSave',
    async (req,res)=>{
        try{
            const{id,title,codeHTML,codeCSS,codeJS}=req.body
            const proj = await Project.findById({_id: id})
            proj.updateOne({title},{title},function(err, result){ if(err) return console.log(err);})
            proj.updateOne({codeHTML},{codeHTML},function(err, result){ if(err) return console.log(err);})
            proj.updateOne({codeCSS},{codeCSS},function(err, result){ if(err) return console.log(err);})
            proj.updateOne({codeJS},{codeJS},function(err, result){ if(err) return console.log(err);})
            return res.json({message:"Project RESAVE"})
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)
routers.post(
    '/userProject',
    async (req,res)=>{
        try{
            const{email}=req.body
            const proj = await Project.find({email: email})
            let project = new Array
            for(let i = 0, l = proj.length; i < l; i++) {
                let prId = proj[i]._id
                let prTitle = proj[i].title
                project.push({prId,prTitle})

            }
            return res.json({
                project : project
            })
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)

routers.post(
    '/findProject',
    async (req,res)=>{
        try{
            const{id}=req.body
            const proj = await Project.findById({_id: id})
            return res.json({
                title : proj.title,
                codeHTML : proj.codeHTML,
                codeCSS : proj.codeCSS,
                codeJS : proj.codeJS,
                user : proj.user
            })
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)
routers.post(
    '/allProject',
    async (req,res)=>{
        try{
            const proj = await Project.find()
            let project = new Array
            for(let i = 0, l = proj.length; i < l; i++) {
                let prId = proj[i]._id
                let prTitle = proj[i].title
                let email = proj[i].email
                project.push({prId,prTitle,email})
            }
            return res.json({
                project : project 
            })
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)
routers.post(
    '/deleteProject',
    async (req,res)=>{
        try{
            const{id}=req.body
            const proj = await Project.deleteOne({_id: id})
            return res.json({message:"Project deleted"})
        }catch(e){
            console.log(e)
            res.send("error blyad")
        }
    }
)
module.exports = routers