<?php

namespace App\Models;

use App\Helpers\FileUploadHelper;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Auth;

class Medicine extends Model
{
    use HasFactory;

    protected $hidden = ['user_id'];

    private static function saveBasicInfo($medicine, $request)
    {
        $medicine->leaf_setting_id = $request->leaf_setting_id;
        //        $medicine->unit_id = $request->unit_id;
        $medicine->category_id = $request->category_id;
        //        $medicine->type_id = $request->type_id;
        $medicine->medicine_company_id = $request->medicine_company_id;
        $medicine->medicine_name = $request->medicine_name;
        $medicine->strength = $request->strength;
        $medicine->shelf_id = $request->shelf_id;
        $medicine->medicine_details = $request->medicine_details;
        $medicine->image = FileUploadHelper::uploadImage($request->file('image'), 'storage/medicine/', $medicine->image);
        $medicine->box_mrp = $request->box_mrp;
        $medicine->supplier_price = $request->supplier_price;
        $medicine->save();

        return $medicine;
    }

    public static function newMedicine($request)
    {
        $medicine = new self;
        $medicine->user_id = Auth::id();
        $medicine->status = '1';

        return self::saveBasicInfo($medicine, $request);
    }

    public static function change_status($id): string
    {
        $medicine = self::query()->find($id);
        if ($medicine->status == '1') {
            $medicine->status = '0';
            $message = 'Inactive successfully!';
        } else {
            $medicine->status = '1';
            $message = 'Active successfully!';
        }
        $medicine->save();

        return $message;
    }

    public static function updateMedicine($request, $id)
    {
        return self::saveBasicInfo(self::query()->find($id), $request);
    }

    public static function updateMedicineStock($id, $sku_qty, $sell_price): void
    {
        $medicine = self::query()->find($id);
        $medicine->qty = $medicine->qty + $sku_qty;
        $medicine->sell_price = $sell_price;
        $medicine->save();
    }

    public static function soldMedicine($id, $sku_qty)
    {
        $medicine = self::query()->find($id);
        $medicine->qty = $medicine->qty - $sku_qty;
        if ($medicine->qty < 0) {
            throw new \Error('Stock is out of stock');
        }
        $medicine->save();
    }

    public static function updateSoldMedicine($id, $sku_qty, $originalSold)
    {
        $medicine = self::query()->find($id);
        $medicine->qty = ($medicine->qty + $originalSold->sku_qty) - $sku_qty;
        if ($medicine->qty < 0) {
            throw new \Error('Stock is out of stock');
        }
        $medicine->save();
    }

    public function leafSetting(): BelongsTo
    {
        return $this->belongsTo(LeafSetting::class, 'leaf_setting_id');
    }

    public function medicineCompany(): BelongsTo
    {
        return $this->belongsTo(MedicineCompany::class, 'medicine_company_id');
    }
}
