const sgMail = require('@sendgrid/mail')
const { SENDGRID_API_KEY, BASE_URL, portalName } = require("../constants/constant")
const { sendPartnerMail, activateProfile } = require("../constants/emailTemplates");
const Admin = require ('../models/admin')
const moment = require("moment");
const sequelize = require("../config/dbconnection");
const { empty, arrayDiffAssoc } = require('../Helpers/Common');
const Driver = require('../models/driver')
const Vehicle = require('../models/vehicle')
let currentDate = Math.round(moment().valueOf() / 1000);


const AddVehicles=async(req,res)=>{
    try {
        const {userId,userType,values} = req.body
        if(!empty(values)){
            let data ={}
            data['name'] = values.name
            data['model'] = values.model
            data['vehicle_number'] = values.vehicle_number
            // data['type_of_vehicle'] = values.type_of_vehicle?.name
            data['assigned_driver'] = values.assigned_driver?.id
            data['vehicle_image'] = values.vehicle_image
            data['status'] = 1
            data['added_by'] = userId
            data['timestamp'] = currentDate
            let result = await Vehicle.AddVehicle(data)
            if(!empty(result)){
                res.send({
                    status:1,message:'Vehicle added successfully'
                })
            }else{
                res.send({
                    status:0,message:'Failed to add a vehicle'
                }) 
            }
        }
    } catch (error) {
        console.log(error,'error in adding a vehicle')
    }
}
const getAllVehicles=async(req,res)=>{
    const {userId,userType,value} = req.body
    let result = []
    if(!empty(value)){
        result = await Vehicle.getAllVehicles(value)
    }else{
       result = await Vehicle.getAllVehicles()
    }
    if(!empty(result)){
        res.send({
            status:1,result
        })
    }else{
        res.send({
            status:0
        })
    }
}
const getVehicleById=async(req,res)=>{
    try {
        const {id,userId,userType} = req.body
        let data = await Vehicle.getVehicleById(id)
        if(!empty(data)){
            res.send({
                status:1,data
            })
        }else{
            res.send({
                status:0
            })
        }
    } catch (error) {
        console.log(error)
    }
}
const updateVehicle=async(req,res)=>{
    try {
       const {id,values,userId} = req.body 
       if(!empty(values)){
         let data ={}
         data['name'] = values.name
         data['model'] = values.model
         data['vehicle_number'] = values.vehicle_number
        //  data['type_of_vehicle'] = values.type_of_vehicle?.name
         if(values.assigned_driver){
             data['assigned_driver'] = values.assigned_driver?.id
         }else{
            data['assigned_driver'] = null
         }
         data['vehicle_image'] = values?.vehicle_image
         data['status'] = 1
         data['added_by'] = userId
         data['timestamp'] = currentDate

          let result = await Vehicle.updateVehicle(data,id)
          if(result){
            res.send({
                status:1,message:`Vehicle ${data['name']} updated successfully `
            })
          }else{
            res.send({
                status:0,message:`Vehicle ${data['name']} is failed to update `
            })
          }
       }
    } catch (error) {
        console.log(error)
    }
}
const deleteVehicle=async(req,res)=>{
    try {
        const {id} = req.body
        let result = await Vehicle.deleteVehicle(id)
        if(result){
            res.send({
                status:1,message:'Vehicle deleted successfully'
            })
        }else{
            res.send({
                status:0,message:'Failed to delete Vehicle'
            })
        }
    } catch (error) {
        console.log(error)
    }
}
module.exports={
    AddVehicles,
    getAllVehicles,
    getVehicleById,
    updateVehicle,
    deleteVehicle,
}