<?php

namespace App\Services\Ticket;

use App\Models\TicketType;

class TicketTypeService
{
    public function list(array $filters = [])
    {
        return TicketType::query()
            ->when(
                !empty($filters['search']),
                fn($q) => $q->where(function ($x) use ($filters) {

                    $x->where(
                        'code',
                        'ILIKE',
                        "%{$filters['search']}%"
                    )
                    ->orWhere(
                        'name',
                        'ILIKE',
                        "%{$filters['search']}%"
                    );
                })
            )
            ->latest('id')
            ->paginate(
                $filters['per_page'] ?? 10
            );
    }

    public function create(array $data): TicketType
    {
        return TicketType::create($data);
    }

    public function update(
        TicketType $ticketType,
        array $data
    ): TicketType {

        $ticketType->update($data);

        return $ticketType->fresh();
    }

    public function delete(
        TicketType $ticketType
    ): bool {

        return $ticketType->delete();
    }
}