import stocktransaction from "../models/stocktransaction.js";

export const addStock=async(req,res)=>{
    try {
        const{TransactionId,TransactionDate,QuantityMoved,TransactionType,UserId,WarehouseCode,ProductCode}=req.body;
        const stockExists=await stocktransaction.findOne({TransactionId});

        if(stockExists){
            return res.status(400).json({message:"stockTransaction already exists"})
        }

        await new stocktransaction({TransactionId,TransactionDate,QuantityMoved,TransactionType,UserId,WarehouseCode,ProductCode}).save();
         
        res.status(201).json({message:"new stockTransaction created"})
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
        
    }
};

export const getstock=async(req,res)=>{
    try {
         const stockExists=await stocktransaction.findById(req.params.id);

       if (!stockExists) {
      return res.status(404).json({ message: "stock transaction not found" });
    }
    res.status(200).json(stockExists);
    } catch (error) {
          res.status(500).json({message:"internal server error",error:error.message})
    }
};

export const getAllStocks=async(req,res)=>{
    try {
         const stockExists=await stocktransaction.find();

       if (!stockExists) {
      return res.status(404).json({ message: "stock transaction not found" });
    }
    res.status(200).json(stockExists);
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
};

export const updateStock=async(req,res)=>{
    try {
        const stockExists=await stocktransaction.findByIdAndUpdate(req.params.id,req.body,{new:true});

       if (!stockExists) {
      return res.status(404).json({ message: "stock transaction not found" });
    }
    res.status(200).json(stockExists);
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
};
export const deleteStock=async(req,res)=>{
    try {
        const stockExists=await stocktransaction.findByIdAndDelete(req.params.id);

       if (!stockExists) {
      return res.status(404).json({ message: "stock transaction not found" });
    }
    res.status(200).json({message:"stock transaction deleted"});
    } catch (error) {
        res.status(500).json({message:"internal server error",error:error.message})
    }
}