<?php

namespace App\Services;

use App\Models\PaymentSplit;
use Illuminate\Database\Eloquent\Collection;

class PaymentSplitService
{
    public function getAll(): Collection
    {
        return PaymentSplit::query()
            ->with(['payment.schedule.client.user', 'provider.user', 'providerWithdrawal'])
            ->orderBy('created_at', 'desc')
            ->get();
    }

    public function findById(int $id): ?PaymentSplit
    {
        return PaymentSplit::query()
            ->with(['payment.schedule.client.user', 'provider.user', 'providerWithdrawal'])
            ->find($id);
    }

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

    public function update(int $id, array $data): ?PaymentSplit
    {
        $model = $this->findById($id);

        if (! $model) {
            return null;
        }

        $model->update($data);

        return $model->fresh();
    }

    public function delete(int $id): bool
    {
        $model = $this->findById($id);

        if (! $model) {
            return false;
        }

        return $model->delete();
    }
}
