<?php

namespace App\Livewire\Studio\Loyalty\Voucher;

use App\Jobs\StorePushNotification;
use App\Livewire\Forms\Studio\Loyalty\VoucherForm;
use App\Models\Outlet;
use App\Models\PushNotification;
use App\Models\Tier;
use App\Traits\Authorization\WithAuthorization;
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 Voucher')]
class Create extends Component
{
    use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;

    public VoucherForm $form;

    public array $outlets;

    public array $tiers;

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

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

        $this->form->store();

        StorePushNotification::dispatch(
            PushNotification::all(),
            [
                'title' => '🎁 Voucher Baru!',
                'body' => 'Ada voucher baru untuk kamu! Jangan sampai kelewatan, yuk cek sekarang dan nikmati penawarannya ✨🎉',
            ],
        );

        $this->redirectRoute('studio.loyalty.voucher.index', [
            'notification' => 'Voucher berhasil ditambahkan.',
        ], navigate: true);
    }

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