<?php

namespace App\Livewire\Studio\Loyalty\Voucher;

use App\Livewire\Forms\Studio\Loyalty\VoucherForm;
use App\Models\Outlet;
use App\Models\Tier;
use App\Models\Voucher;
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('Ubah Voucher')]
class Edit extends Component
{
    use WithAuthorization, WithOutletSelector, WithToast, WithUpdatedData;

    public VoucherForm $form;

    public array $outlets;

    public array $tiers;

    public function mount(Voucher $voucher): void
    {
        $this->form->setVoucher($voucher);

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

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

        $this->form->update();

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

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