<?php

namespace App\Livewire\Member;

use App\Enums\VoucherType;
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()
    {
        $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 ? currency($voucher->min_purchase, 'Rp') : 'No Min. Belanja',
            'discount_amount' => $voucher->type == VoucherType::PERCENTAGE ? $voucher->discount_amount.'%' : currency($voucher->discount_amount, 'Rp'),
            'max_discount' => currency($voucher->max_discount, 'Rp'),
            'end_date' => $voucher->end_date ? timeAgo($voucher->end_date) : 'Tidak Ada Batas Waktu',
        ])->toArray();
    }

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