<?php

namespace Modules\Patient\Repositories;

use Modules\Patient\Models\Patient;
use Modules\Patient\DTO\PatientSettingFilter;

class PatientRepository
{
    public function all(PatientSettingFilter $filter, array $columns = ['*'], array $relations = [])
    {
        return Patient::select($columns)
            ->with($relations)
            ->search($filter->search)
            ->orderBy('id', 'desc')
            ->paginate($filter->perPage ?? 15);
    }

    public function find($id)
    {
        return Patient::findOrFail($id);
    }

    public function syncFromErp(array $data)
    {
        Patient::updateOrCreate(
            ['bts_id' => $data['bts_id']],
            [
                'name'     => $data['name'],
                'phone'    => $data['phone'],
                'dob'      => $data['dob'],
                'gender'   => $data['gender'],
                'current_fund_id' => $data['current_fund_id'] ?? null,
            ]
        );
    }

}
