<?php

namespace App\Livewire\Studio\Master;

use App\Livewire\Forms\Studio\Master\FormulaForm;
use App\Models\Bottle;
use App\Models\Formula as FormulaModel;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithCloseModal;
use App\Traits\Components\WithConfirmation;
use App\Traits\Components\WithToast;
use App\Traits\Notification\WithSubscribeNotification;
use App\Traits\Utilities\WithUpdatedData;
use Flux\Flux;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\On;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Rumus')]
class Formula extends Component
{
    use WithAuthorization, WithCloseModal, WithConfirmation, WithSubscribeNotification, WithToast, WithUpdatedData;

    public FormulaForm $form;

    public string $method = 'create';

    public string $modalTitle = '';

    public array $qualities = ['Premium', 'Super Premium', 'Murni'];

    public array $sizes = [];

    public function mount(): void
    {
        $this->sizes = Bottle::distinct()->orderBy('size')->pluck('size')->toArray();
    }

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

        if (FormulaModel::where('quality', $this->form->quality)->where('size', $this->form->size)->exists()) {
            $this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning');

            return;
        }

        $this->form->store();

        $this->toast('Rumus berhasil ditambahkan.');

        Flux::modals()->close();
    }

    public function update(): void
    {
        $this->canOrAbort('update formula');

        $exists = FormulaModel::where('quality', $this->form->quality)
            ->where('size', $this->form->size)
            ->where('id', '!=', $this->form->formula->id)
            ->exists();

        if ($exists) {
            $this->toast('Rumus dengan kualitas dan ukuran tersebut sudah ada.', 'Info', 'warning');

            return;
        }

        $this->form->update();

        $this->toast('Rumus berhasil diperbarui.');

        Flux::modals()->close();
    }

    public function delete(FormulaModel $formula): void
    {
        $this->canOrAbort('delete formula');

        $formula->delete();

        $this->toast('Rumus berhasil dihapus.');

        Flux::modals()->close();
    }

    #[On('modal:open')]
    public function openModal(string $method, string $modalTitle, ?string $id = null): void
    {
        $this->resetValidation();
        $this->resetErrorBag();

        $this->method = $method;
        $this->modalTitle = $modalTitle;

        $id && $this->form->setFormula(FormulaModel::byHashOrFail($id));
    }

    public function render(): View
    {
        $formulas = FormulaModel::orderBy('size')
            ->get()
            ->groupBy('size')
            ->map(
                fn ($items) => $items
                    ->map(fn ($item) => [
                        'hash' => $item->hash,
                        'quality' => $item->quality,
                        'volume' => $item->volume,
                    ])
                    ->sortBy('volume')
                    ->values()
                    ->toArray()
            )
            ->toArray();

        return view('livewire.studio.master.formulas', [
            'pageTitle' => 'Rumus',
            'formulas' => $formulas,
        ]);
    }
}
