<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class Stock extends Model
{
    use HasFactory;

    protected $hidden = ['user_id'];

    private static function saveBasicInfo($stock, $item, $purchase, $leaf_setting)
    {
        $stock->purchase_id = $purchase->id;
        $stock->medicine_id = $item['medicine_id'];
        $stock->leaf_setting_id = $leaf_setting->id;
        $stock->leaf_qty = $item['leaf_qty'];
        $stock->sku_qty = $leaf_setting->total_number*$item['leaf_qty'];
        $stock->supplier_price = $item['supplier_price'];
        $stock->box_mrp = $item['box_mrp'];
        $stock->sell_price = $item['sell_price'];
        $stock->total_purchase_price = $item['supplier_price']*$item['leaf_qty'];
        $stock->save();

        return $stock;
    }

    public static function newStock($request, $purchase): array
    {
        $stocks = [];
        foreach ($request->data as $item) {

            $medicine = Medicine::query()->with('leafSetting:id,leaf_type,total_number')->findOrFail($item['medicine_id'], ['id', 'leaf_setting_id', 'medicine_name']);
            $stock = new self;
            $stock->user_id = Auth::id();
            self::saveBasicInfo($stock, $item, $purchase, $medicine->leafSetting);
            if ($purchase->status == '1') {
                Medicine::updateMedicineStock($item['medicine_id'], $stock->sku_qty, $stock->sell_price);
                $stocks[] = [
                  'subtotal' => $stock->total_purchase_price,
                ];
            } elseif ($purchase->status == '0') {
                $stocks[] = [
                    'subtotal' => $stock->total_purchase_price,
                    'medicine_name' => $medicine->medicine_name,
                    'box_pattern' => $medicine->leafSetting->leaf_type."(".$medicine->leafSetting->total_number.")",
                    'leaf_qty' => $stock->leaf_qty,
                    'sku_qty' => $stock->sku_qty,
                ];
            }
        }

        return $stocks;
    }

    public static function updateStock($request, $purchase, $originalPurchase): array
    {
        $stocks = [];
        foreach ($request->data as $item) {
            $medicine = Medicine::query()->with('leafSetting:id,leaf_type,total_number')->findOrFail($item['medicine_id'], ['id', 'leaf_setting_id']);
            $stock = self::query()->find($item['id']);
            self::saveBasicInfo($stock, $item, $purchase, $medicine->leafSetting);

            if ($originalPurchase->status == '0' && $purchase->status == '1') {
                Medicine::updateMedicineStock($item['medicine_id'], $stock->sku_qty, $stock->sell_price);
                $stocks[] = [
                    'subtotal' => $stock->total_purchase_price,
                ];
            } elseif ($originalPurchase->status == '0' && $purchase->status == '0') {
                $stocks[] = [
                    'subtotal' => $stock->total_purchase_price,
                    'medicine_name' => $medicine->medicine_name,
                    'box_pattern' => $medicine->leafSetting->leaf_type."(".$medicine->leafSetting->total_number.")",
                    'leaf_qty' => $stock->leaf_qty,
                    'sku_qty' => $stock->sku_qty,
                ];
            }
        }

        return $stocks;
    }
}
