<?php

namespace Modules\Linksehat\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\Organization;
use App\Models\Practice;
use App\Models\PractitionerRole;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Linksehat\Transformers\Hospital\HospitalResource;
use Modules\Linksehat\Transformers\Speciality\SpecialityResource;

class SearchController extends Controller
{
    public function searchSpecialityOrPerson(Request $request)
    {
        $doctors = PractitionerRole::query()
            ->with(['prices'])
            ->whereHas('prices', function ($query) {
                $query->where('priceable_type', Practice::class);
            })
            ->leftJoin('specialities', function ($query) {
                $query->on('practitioner_roles.speciality_id', '=', 'specialities.id');
            })
            ->leftJoin('persons', function ($query) {
                $query->on('practitioner_roles.practitioner_id', '=', 'persons.id');
            })
            ->whereRaw("(persons.name LIKE '%{$request->value}%' OR specialities.name LIKE '%{$request->value}%')")
            ->where('organization_id', $request->organization_id)
            ->get(['persons.name AS person_name', 'specialities.name AS speciality_name', 'practitioner_id', 'practitioner_roles.id']);

        return Helper::responseJson(['doctors' => SpecialityResource::collection($doctors)]);
    }

    public function searchHospital(Request $request)
    {
        abort_if($request->has('value') != true || empty($request->value), 400, 'Missing Parameter value');

        $queryHospitals = Organization::query()
            ->when($request->lat && $request->lng, function (EloquentBuilder $query) use ($request) {
                $query->getQuery()
                    ->selectRaw("organizations.id, organizations.name, addresses.text AS address, cities.name AS city_name, 6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance")
                    ->groupBy(['organizations.id', 'organizations.name', 'addresses.text', 'practitioner_roles.organization_id', 'practitioner_roles.speciality_id', 'addresses.lat', 'addresses.lng', 'cities.name'])
                    ->orderBy('distance', 'ASC');
            }, function (EloquentBuilder $query) {
                $query->getQuery()
                    ->select(['organizations.id', 'organizations.name', 'addresses.text AS address', 'cities.name AS city_name'])
                    ->groupBy(['organizations.id', 'organizations.name', 'addresses.text', 'practitioner_roles.organization_id', 'practitioner_roles.speciality_id', 'cities.name'])
                    ->orderBy('organizations.name');
            })
            ->leftJoin('addresses', function (Builder $query) {
                $query->on('organizations.main_address_id', '=', 'addresses.id');
            })
            ->leftJoin('practitioner_roles', function (Builder $query) {
                $query->on('organizations.id', '=', 'practitioner_roles.organization_id');
            })
            ->leftJoin('specialities', function (Builder $query) {
                $query->on('practitioner_roles.speciality_id', '=', 'specialities.id');
            })
            ->leftJoin('cities', function ($query) {
                $query->on('addresses.city_id', '=', 'cities.id');
            })
            ->where('specialities.name', 'LIKE', "{$request->value}%")
            ->where('addresses.addressable_type', '=', Organization::class)
            ->whereNot('practitioner_roles.speciality_id')
            ->get();

        return Helper::responseJson(data: ['hospitals' => HospitalResource::collection($queryHospitals)], message: 'Data rumah sakit berhasil di ambil');
    }
}
