<?php

namespace App\Observers;

use App\Model\stock_inventory\StockModel;
use App\Model\TrackStockModel;
use Auth;
use Illuminate\Http\Request;

class StockObserver
{


    public function created(StockModel $stock)
    {
        if(!($stock->barcode??'')) return true;
        $qty = $stock->stock_qty??0;
        $product = getProductById($stock->product_id)->product_name??'';
        $wh = findWarehouse($stock->warehouse_id)->warehouse_name??'';
        TrackStockModel::create([
            'user_id' => Auth::user()->id,
            'warehouse_id' => $stock->warehouse_id??'0',
            'ref_number' => "create-$stock->id",
            'ref_type' => "import",
            'ref_id' => $stock->id,
            'barcode' => $stock->barcode??'0',
            'product_id' => $stock->product_id??'0',
            'type' => $stock->type??'unknown',
            'qty' => $qty,
            'operation' => '+',
            'date' => now(),
            'last_stock' => $stock->stock_qty??'0',
            'note' => auth()->user()->name." បានបន្ថែម(c+) $product ចំនួន:$qty ($wh) នៅថ្ងៃ ".date('d-m-Y H:i:s A'),
        ]);
    }

    public function updated(StockModel $stock)
    {
        if(!($stock->barcode??'')) return true;
        $product = getProductById($stock->product_id)->product_name??'';
        $wh = findWarehouse($stock->warehouse_id)->warehouse_name??'';
        $last = TrackStockModel::where(['warehouse_id'=>$stock->warehouse_id,'barcode'=>$stock->barcode,'type'=>$stock->type])->latest()->first();

        $last_stock = $last->last_stock??0;


        if($last_stock){
            if($last_stock > $stock->stock_qty){ //if sale
                $qty = $last_stock-$stock->stock_qty;
                $text = 'បានបញ្ចេញ(-=) ';
                $type = "export";
                $op = "-";
            }else{
                $qty = $stock->stock_qty-$last_stock;
                $text = 'បានបន្ថែម(+=) ';
                $type = "import";
                $op = "+";
            }

        }else{
            $text = 'បានបន្ថែម(+) ';
            $qty = $stock->stock_qty;
            $type = "import";
            $op = "+";
        }

        TrackStockModel::create([
            'user_id' => Auth::user()->id,
            'warehouse_id' => $stock->warehouse_id??'0',
            'ref_number' => "update-$type-$stock->id",
            'ref_type' => $type,
            'ref_id' => $stock->id,
            'barcode' => $stock->barcode,
            'product_id' => $stock->product_id??'0',
            'qty' => $qty,
            'date' => now(),
            'type' => $stock->type??'unknown',
            'last_stock' => $stock->stock_qty,
            'operation' => $op,
            'note' => auth()->user()->name." $text $product ចំនួន:$qty ($wh) នៅថ្ងៃ ".date('d-m-Y H:i:s A'),
        ]);

    }

    public function deleted(StockModel $stock)
    {

    }

    public function restored(StockModel $stock)
    {
        //
    }

    public function forceDeleted(StockModel $stock)
    {
        //
    }
}
