<?php

namespace App\Services\Discounts;

use App\Models\DiscountVoucher;

class DiscountVoucherService
{
    public function listAll(): array
    {
        return DiscountVoucher::query()
            ->orderByDesc('id')
            ->get()
            ->all();
    }

    public function listActive(): array
    {
        return DiscountVoucher::query()
            ->where('is_active', 1)
            ->orderByDesc('id')
            ->get()
            ->all();
    }

    public function create(array $payload): DiscountVoucher
    {
        return DiscountVoucher::query()->create($payload);
    }

    public function update(int $id, array $payload): DiscountVoucher
    {
        $voucher = DiscountVoucher::query()->findOrFail($id);
        $voucher->fill($payload);
        $voucher->save();
        return $voucher;
    }

    public function find(int $id): DiscountVoucher
    {
        return DiscountVoucher::query()->findOrFail($id);
    }

    public function delete(int $id): void
    {
        DiscountVoucher::query()->whereKey($id)->delete();
    }
}
