<?php

namespace App\Services\Payments;

use App\Models\PayPalPayment;

class PaypalTransactionsService
{
    public function list(?string $keyword, int $perPage = 10)
    {
        $query = PayPalPayment::query()->orderByDesc('created_at');

        if ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $q->where('transaction_id', 'like', '%' . $keyword . '%')
                    ->orWhere('payer_email', 'like', '%' . $keyword . '%')
                    ->orWhere('event_type', 'like', '%' . $keyword . '%')
                    ->orWhere('order_id', 'like', '%' . $keyword . '%')
                    ->orWhere('parent_school_id', 'like', '%' . $keyword . '%');
            });
        }

        return $query->paginate($perPage);
    }

    public function listAll(?string $keyword): array
    {
        $query = PayPalPayment::query()->orderByDesc('created_at');

        if ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $q->where('transaction_id', 'like', '%' . $keyword . '%')
                    ->orWhere('payer_email', 'like', '%' . $keyword . '%')
                    ->orWhere('event_type', 'like', '%' . $keyword . '%')
                    ->orWhere('order_id', 'like', '%' . $keyword . '%')
                    ->orWhere('parent_school_id', 'like', '%' . $keyword . '%');
            });
        }

        return $query->get()->map(fn ($row) => (array) $row)->all();
    }
}
