<?php

namespace App\Livewire\Studio\Catalog\Perfume;

use App\Jobs\StorePushNotification;
use App\Livewire\Forms\Studio\Catalog\PerfumeForm;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Outlet;
use App\Models\PushNotification;
use App\Traits\Authorization\WithAuthorization;
use App\Traits\Components\WithCategorySelector;
use App\Traits\Components\WithOutletSelector;
use App\Traits\Components\WithToast;
use App\Traits\Utilities\WithUpdatedData;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Tambah Parfum')]
class Create extends Component
{
    use WithAuthorization, WithCategorySelector, WithOutletSelector, WithToast, WithUpdatedData;

    public PerfumeForm $form;

    public array $outlets = [];

    public array $brands = [];

    public array $categories = [];

    public function mount(): void
    {
        $this->outlets = Outlet::pluck('name', 'id')->toArray();

        $this->brands = Brand::pluck('name', 'id')->toArray();

        $this->categories = Category::perfume()->pluck('name', 'id')->toArray();
    }

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

        $this->form->store();

        StorePushNotification::dispatch(
            PushNotification::all(),
            [
                'title' => '✨ Aroma Baru!',
                'body' => 'Ada yang baru nih! Aroma terbaru sudah siap kamu coba. Yuk cobain dulu sebelum kehabisan! 🌸✨',
            ],
        );

        $this->redirectRoute('studio.catalog.perfume.index', [
            'notification' => 'Parfum berhasil ditambahkan.',
        ], navigate: true);
    }

    public function render(): View
    {
        return view('livewire.studio.catalog.perfume.form', [
            'pageTitle' => 'Tambah Parfum',
        ]);
    }
}
