<?php

namespace App\Traits\Order;

use App\Models\Bottle;
use App\Models\Formula;
use App\Models\Perfume;
use App\Models\Product;

trait WithAddItem
{
    public string $tab = 'new';

    /**
     * Triggered when the bottle ID form field is updated (used in "new" tab).
     * Loads available qualities (Formula) based on the bottle size.
     */
    public function updatedFormBottleId(string $value)
    {
        $bottle = Bottle::find($value);

        if (! $bottle) {
            $this->qualities = [];

            return $this->toast('Botol tidak ditemukan, silakan hubungi Administrator.', 'Gagal', 'danger');
        }

        $formulas = Formula::where('size', $bottle->size)->pluck('quality', 'id')->toArray();

        // check if there is any formula
        if (empty($formulas)) {
            $this->qualities = [];
            $this->toast('Kualitas tidak ditemukan, silakan hubungi Administrator.', 'Gagal', 'danger');

            return;
        }

        $this->qualities = $formulas;
    }

    /**
     * Triggered when the bottle size form field is updated (used in "refill" tab).
     * Loads available qualities (Formula) based on the selected size.
     */
    public function updatedFormBottleSize(string $value)
    {
        $formulas = Formula::where('size', $value)->pluck('quality', 'id')->toArray();

        // check if there is any formula
        if (empty($formulas)) {
            $this->qualities = [];

            return $this->toast('Kualitas tidak ditemukan, silakan hubungi Administrator.', 'Gagal', 'danger');
        }

        $this->qualities = $formulas;
    }

    /**
     * Entry point to add a perfume item to the cart.
     * Determines the action based on the currently active tab.
     */
    public function addPerfume()
    {
        match ($this->tab) {
            'new' => $this->handleNewPerfume(),
            'refill' => $this->handleRefillPerfume(),
            default => $this->toast('Tab tidak dikenal, silakan periksa kembali.', 'Gagal', 'danger'),
        };
    }

    /**
     * Handles the process of adding a new perfume with a bottle and selected quality.
     */
    protected function handleNewPerfume()
    {
        $perfume = Perfume::find($this->form->perfume_id);
        $bottle = Bottle::find($this->form->bottle_id);
        $quality = Formula::find($this->form->quality_id);

        if (! $perfume || ! $bottle || ! $quality) {
            $this->toast('Parfum, botol, atau kualitas tidak valid.', 'Gagal', 'danger');

            return;
        }

        $quantity = $quality->volume;

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

            return;
        }

        $this->addOrUpdateOrderItem($perfume, $quantity);

        $this->addOrUpdateOrderItem($bottle, 1);

        $this->reset(['form.perfume_id', 'form.bottle_id', 'form.quality_id', 'form.bottle_size']);

        $this->qualities = [];

        $this->toast('Parfum dan botol baru ditambahkan ke keranjang.', 'Berhasil');
    }

    /**
     * Handles the process of adding a refill perfume based on selected size and quality.
     */
    protected function handleRefillPerfume()
    {
        $perfume = Perfume::find($this->form->perfume_id);
        $quality = Formula::find($this->form->quality_id);

        $quantity = $quality->volume;

        if (! $perfume || ! $quality || $quantity <= 0) {
            $this->toast('Parfum, ukuran, atau kualitas tidak valid.', 'Gagal', 'danger');

            return;
        }

        $this->addOrUpdateOrderItem($perfume, $quantity);

        $this->toast('Parfum isi ulang ditambahkan ke keranjang.', 'Berhasil');

        $this->reset(['form.perfume_id', 'form.bottle_id', 'form.quality_id', 'form.bottle_size']);

        $this->qualities = [];
    }

    /**
     * Handles adding a regular product (non-perfume) to the cart.
     */
    public function addProduct()
    {
        $product = Product::find($this->form->product_id);

        $quantity = replaceCurrency($this->form->quantity_product);

        if (! $product || $quantity <= 0) {
            $this->toast('Produk atau kuantitas tidak valid', 'Gagal', 'danger');

            return;
        }

        $this->addOrUpdateOrderItem($product, $quantity);

        $this->reset(['form.product_id', 'form.quantity_product']);
    }
}
