<?php

namespace App\Livewire\Studio\Manage\Restock;

use App\Livewire\Forms\Studio\Manage\RestockForm;
use App\Models\Warehouse;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\Restock\WithRestockAddItem;
use App\Traits\Restock\WithRestockDeleteItem;
use App\Traits\Restock\WithRestockItems;
use App\Traits\Restock\WithRestockUpdateItem;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Tambah Restock Toko')]
class Create extends Component
{
    use WithAuthorization, WithConfirmation, WithRestockAddItem, WithRestockDeleteItem, WithRestockItems, WithRestockUpdateItem, WithToast, WithUpdatedData;

    public RestockForm $form;

    public array $warehouses = [];

    public array $outlets = [];

    public array $perfumes = [];

    public array $products = [];

    public array $bottles = [];

    public $restockItems;

    public function mount(): void
    {
        $this->warehouses = Warehouse::orderBy('name')->pluck('name', 'id')->toArray();
        $this->outlets = auth()->user()->outlets->pluck('name', 'id')->toArray();

        $this->restockItems = $this->loadRestockItems();
    }

    public function updated($property, $value): void
    {
        if ($property === 'form.warehouse_id' || $property === 'form.outlet_id') {
            $this->loadCreateItems();
        }
    }

    private function loadCreateItems(): void
    {
        $this->reset(['perfumes', 'products', 'bottles']);

        $this->form->reset(
            'perfume_id',
            'quantity_perfume',
            'product_id',
            'quantity_product',
            'bottle_id',
            'quantity_bottle'
        );

        if (! $this->form->warehouse_id || ! $this->form->outlet_id) {
            return;
        }

        $warehouse = Warehouse::find($this->form->warehouse_id);

        if (! $warehouse) {
            return;
        }

        $this->perfumes = $warehouse->perfumes()
            ->wherePivot('stock', '>', 0)
            ->orderBy('name')
            ->get()
            ->pluck('name', 'id')
            ->toArray();

        $this->products = $warehouse->products()
            ->wherePivot('stock', '>', 0)
            ->orderBy('name')
            ->get()
            ->pluck('name', 'id')
            ->toArray();

        $this->bottles = $warehouse->bottles()
            ->wherePivot('stock', '>', 0)
            ->orderBy('name')
            ->get()
            ->pluck('name', 'id')
            ->toArray();
    }

    public function save(): void
    {
        $this->canOrAbort('create restock');

        if ($this->restockItems->isEmpty()) {
            $this->toast('Keranjang restock tidak boleh kosong.', 'Gagal', 'danger');

            return;
        }

        try {
            $this->form->store();
        } catch (ValidationException $e) {
            throw $e;
        } catch (\Exception $e) {
            $this->toast($e->getMessage(), 'Gagal', 'danger');

            return;
        }

        $this->redirectRoute('studio.manage.restock.index', [
            'notification' => 'Restock berhasil diproses.',
        ], navigate: true);
    }

    public function render(): View
    {
        return view('livewire.studio.manage.restock.form', [
            'pageTitle' => 'Tambah Restock Toko',
        ]);
    }
}
