<?php

namespace App\Traits\Purchase;

use App\Models\Bottle;
use App\Models\Perfume;
use App\Models\Product;
use App\Models\PurchaseItem;
use Illuminate\Support\Str;

trait WithAddItem
{
    public function addItem(string $type)
    {
        switch ($type) {
            case 'parfum':
                $model = Perfume::class;
                $id = $this->form->perfume_id;
                $quantity = replaceCurrency($this->form->quantity_perfume);
                break;

            case 'produk':
                $model = Product::class;
                $id = $this->form->product_id;
                $quantity = replaceCurrency($this->form->quantity_product);
                break;

            case 'botol':
                $model = Bottle::class;
                $id = $this->form->bottle_id;
                $quantity = replaceCurrency($this->form->quantity_bottle);
                break;

            default:
                $this->toast('Tipe item tidak dikenal.', 'Gagal', 'danger');

                return;
        }

        $itemModel = $model::find($id);

        if (! $itemModel) {
            $this->toast(Str::ucfirst($type).' tidak ditemukan, silakan periksa kembali.', 'Gagal', 'danger');

            return;
        }

        $costPrice = (int) $itemModel->cost_price;

        if ($quantity <= 0) {
            $this->toast('Jumlah '.$type.' tidak valid, silakan periksa kembali.', 'Gagal', 'danger');

            return;
        }

        if ($costPrice <= 0) {
            $this->toast('Harga '.$type.' tidak valid, silakan periksa kembali.', 'Gagal', 'danger');

            return;
        }

        $existingItem = $this->purchaseItems
            ->where('purchasable_type', $model)
            ->where('purchasable_id', $itemModel->id)
            ->first();

        if ($existingItem) {
            $existingItem->quantity += $quantity;
            $existingItem->total_price = $existingItem->unit_price * $existingItem->quantity;
            $existingItem->save();

            $existingItem->refresh();

            $this->purchaseItems = $this->purchaseItems->map(fn ($i) => $i->id === $existingItem->id ? $existingItem : $i);

            $this->toast('Jumlah '.$type.' diperbarui di keranjang.', 'Berhasil');
        } else {
            $item = PurchaseItem::create([
                'user_id' => auth()->id(),
                'purchasable_id' => $itemModel->id,
                'purchasable_type' => $model,
                'quantity' => $quantity,
                'unit_price' => $costPrice,
                'total_price' => $costPrice * $quantity,
            ]);

            $this->purchaseItems->push($item);

            $this->toast(Str::ucfirst($type).' ditambahkan ke keranjang.', 'Berhasil');
        }

        $this->form->total = currency($this->getTotal());

        switch ($type) {
            case 'parfum':
                $this->reset(['form.perfume_id', 'form.quantity_perfume']);
                break;

            case 'produk':
                $this->reset(['form.product_id', 'form.quantity_product']);
                break;

            case 'botol':
                $this->reset(['form.bottle_id', 'form.quantity_bottle']);
                break;
        }
    }
}
