<?php

namespace Modules\User\Repositories;

use Illuminate\Database\Eloquent\Collection;
use Modules\User\Entities\BankDetail;
use Modules\User\Repositories\Contracts\BankDetailRepositoryInterface;

class BankDetailRepository implements BankDetailRepositoryInterface
{
    protected function baseQuery()
    {
        return BankDetail::query()->where('user_id', auth()->id());
    }

    public function getAllPaginated(int $perPage = 10, string $search = '')
    {
        $query = $this->baseQuery();

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

    public function getAllWithRelations(string $search = ''): Collection
    {
        $query = BankDetail::query()
                ->where('user_id', auth()->id())
                ->with(['user:id,name,email', 'bank:id,name'])
                ->orderBy('account_number');

        return $query->get();
    }

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

    public function findById(string $id): ?BankDetail
    {
        return BankDetail::query()
            ->where('user_id', auth()->id())
            ->find($id);
    }

    public function delete(string $id): bool
    {
        $bankDetail = $this->findById($id);
        if ($bankDetail) {
            return $bankDetail->delete();
        }

        return false;
    }

    public function all(string $search = ''): Collection
    {
        return $this->baseQuery()->get();
    }
}
