<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Models\Docum;
use App\Models\Human;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BirthRecord extends Controller
{

    public function birth_check(Request $request): JsonResponse
    {
        // Validate incoming JSON data
        $validatedData = $request->validate([
            'entry_num' => 'required|string',
        ]);

        //@Todo return on not found
        $rid = Docum::where('ser_num', '=', $validatedData['entry_num'])->value('rid_h');
        if($rid){
            $human = Human::where('rid','=', $rid)->whereColumn('rid', 'pid')
                ->get(['name', 'surn', 'last_name', 'sex', 'date_birth', 'date_death', 'o_pid']);

            if($human->isEmpty()){
                return response()->json([
                    'message' => 'No record found',
                ], 401);
            }else{
                return response()->json([
                    'message' => 'Birth Record Found!',
                    'data' => $human
                ], 201);
            }
        }


    }
}
