<?php

namespace App\Http\Controllers\Api;

use App\Exceptions\ApiHttpException;
use App\Facades\PatientService;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backoffice\PatientCreateRequest;
use App\Http\Requests\Backoffice\PatientLoginRequest;
use App\Http\Resources\PatientResource;

class PatientsController extends Controller
{
    public function create(PatientCreateRequest $request)
    {
        $data = $request->validated();
        $patient = PatientService::createPatient($data);

        return new PatientResource($patient);
    }

    public function login(PatientLoginRequest $request)
    {
        $data = $request->validated();
        $patient = PatientService::checkPatientToken($data);

        if (!$patient) {
            throw new ApiHttpException(401, 'invalid_token');
        }

        return new PatientResource($patient);
    }
}
