<?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)->orderBy('volume')->pluck('quality', 'id')->toArray();

        $this->qualities = $formulas + ['custom' => 'Custom'];
    }

    /**
     * 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)
    {
        if ($this->tab !== 'refill') {
            return;
        }

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

        $this->qualities = $formulas + ['custom' => 'Custom'];
    }

    /**
     * Entry point to add a perfume item to the cart.
     * Determines the action based on the currently active tab.
     */
    public function addPerfume(): void
    {
        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(): void
    {
        $perfume = Perfume::find($this->form->perfume_id);
        $bottle = Bottle::find($this->form->bottle_id);

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

            return;
        }

        if ($this->form->quality_id === 'custom') {
            $quantity = parseRupiahToInt($this->form->bottle_size);
            $qualityName = 'Custom';

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

                return;
            }
        } else {
            $quality = Formula::find($this->form->quality_id);

            if (! $quality) {
                $this->toast('Kualitas tidak valid.', 'Gagal', 'danger');

                return;
            }

            $quantity = $quality->volume;
            $qualityName = $quality->quality;
        }

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

            return;
        }

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

        $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(): void
    {
        $perfume = Perfume::find($this->form->perfume_id);

        if (! $perfume) {
            $this->toast('Parfum tidak valid.', 'Gagal', 'danger');

            return;
        }

        if ($this->form->quality_id === 'custom') {
            $quantity = parseRupiahToInt($this->form->bottle_size);
            $qualityName = 'Custom';

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

                return;
            }
        } else {
            $quality = Formula::find($this->form->quality_id);

            if (! $quality) {
                $this->toast('Kualitas tidak valid.', 'Gagal', 'danger');

                return;
            }

            $quantity = $quality->volume;
            $qualityName = $quality->quality;
        }

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

            return;
        }

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

        $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(): void
    {
        $product = Product::find($this->form->product_id);

        $quantity = parseRupiahToInt($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']);
    }
}
