<?php

namespace App\Http\Controllers\API\v1;

use App\Http\Controllers\Controller;
use App\Models\Employee;
use App\Models\Family;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Throwable;

class FamilyController extends Controller
{
    public function show(string $id)
    {   
        try {
            $id = Crypt::decrypt($id);
        } catch (\Illuminate\Contracts\Encryption\DecryptException $e) {
            $id = $id;
        }

        $data = Family::where('employee_id', '=', $id)->first();
        
        return response()->json($data);
    }
    
    public function store(Request $request, string $employee_id)
    {
        // dd($request->all());
        // dd(Crypt::decrypt($employee_id));
        $employee_id = Crypt::decrypt($employee_id);
        
        $employeeExists = Family::where('employee_id', $employee_id)->exists();

        $validator = Validator::make($request->all(), [
            // 'passport_id' => [
            //     'required',
            //     'min:1',
            // ],
        ]);
        
        // dd($employeeExists);   

        if($employeeExists){
            $data = Family::where('employee_id', $employee_id)->first();
            
            if (!$data) {
                return response()->json(['messages' => 'No data found.'], 404);
            }

            /** If validation fails return with error message */
            if ($validator->fails()) {
                $errors = $validator->errors();
                $first = $errors->getMessages();
                return response()->json([
                    'status'  => false,
                    'message' => (reset($first))[0],
                    'errors'  => $errors
                ], Response::HTTP_UNPROCESSABLE_ENTITY);
            }

            DB::connection()->beginTransaction();

            try {

                $data->fill($request->all());
                $data->save();

                $data->touch();

                DB::connection()->commit();

                return response()->json([
                    'message' => 'Record Successfully updated!',
                    'status' => 'success',
                    'data' => $data,
                ], 201);

            } catch (Throwable $e) {
                DB::connection()->rollback();
                return response()->json([
                    'status'  => false,
                    'message' => 'Unable to process request. Please try again.',
                    'data'    => $e->getMessage()
                ]);
            }
        } else {
            /** If validation fails return with error message */
            if ($validator->fails()) {
                $errors = $validator->errors();
                $first = $errors->getMessages();
                return response()->json([
                    'status'  => false,
                    'message' => (reset($first))[0],
                    'errors'  => $errors
                ], Response::HTTP_UNPROCESSABLE_ENTITY);
            }

            DB::connection()->beginTransaction();

            try {

                $data = Family::create($request->all());

                DB::connection()->commit();

                return response()->json([
                    'message' => 'Record Successfully added!',
                    'status' => 'success',
                ],201);

            } catch (Throwable $e) {
                DB::connection()->rollback();
                return response()->json([
                    'status'  => false,
                    'message' => 'Unable to process request. Please try again.',
                    'data'    => $e->getMessage()
                ]);
            }
        }
    }
}
