<?php

namespace App\Livewire\Member;

use App\Enums\VoucherType;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Title('Voucher')]
#[Layout('components.layouts.member')]
class Voucher extends Component
{
    public array $vouchers = [];

    public function mount(): void
    {
        $this->vouchers = auth()->user()->vouchers()->get()->map(fn ($voucher) => [
            'id' => $voucher->id,
            'name' => $voucher->name,
            'code' => $voucher->code,
            'is_percentage' => $voucher->type == VoucherType::PERCENTAGE,
            'min_purchase' => $voucher->min_purchase ? formatCurrencyNumber($voucher->min_purchase, 'Rp') : 'No Min. Belanja',
            'discount_amount' => $voucher->type == VoucherType::PERCENTAGE ? $voucher->discount_amount.'%' : formatCurrencyNumber($voucher->discount_amount, 'Rp'),
            'max_discount' => formatCurrencyNumber($voucher->max_discount, 'Rp'),
            'end_date' => $voucher->end_date ? formatRelativeTime($voucher->end_date) : 'Tidak Ada Batas Waktu',
        ])->toArray();
    }

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