<?php

namespace App\Http\Controllers\Api\Employee;

use App\Http\Controllers\Controller;
use App\Http\Resources\Api\Employee\AbsenceResource;
use App\Models\Absence;
use App\Models\Child;
use App\Models\EmployeeAbsence;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Auth;
class EmployeeHelperController extends Controller
{
    public function absenceList()
    {
        $result = Absence::where('employee_id',Auth::guard('employee')->user()->id)->paginate(10);

        $data = AbsenceResource::collection($result)->response()->getData(true);
        return msgdata(true, trans('lang.Success_text'),$data, success());
    }
    public function storeAbsence(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'child_id' => 'required|exists:children,id',
            'type' => 'required|in:in,out',
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }

        $child = Child::find($request->child_id);
        $data = new Absence();
        $data->child_id = $request->child_id;
        $data->employee_id = Auth::guard('employee')->user()->id;
        $data->parent_id = $child->parent_id;
        $data->type = $request->type;
        $data->save();

        return msgdata(true, trans('lang.Success_text'), (object)[], success());

    }

    public function storeEmployeeAbsence(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'lat'=>'required',
            'lng'=>'required',
            'type' => 'required|in:in,out',
        ]);
        if (!is_array($validator) && $validator->fails()) {
            return msg(false, $validator->errors()->first(), validation());
        }


        $companyLat = 30.0444; // Replace with your company's latitude
        $companyLon = 31.2357; // Replace with your company's longitude
        $userLat = $request->lat;    // Replace with the user's latitude
        $userLon = $request->lng;    // Replace with the user's longitude

        $distance = $this->haversine($companyLat, $companyLon, $userLat, $userLon);

        if ($distance <= 5)
        { // Check if the user is within 5 km

            $data = new EmployeeAbsence();
            $data->employee_id = Auth::guard('employee')->user()->id;
            $data->type = $request->type;
            $data->save();

            return msgdata(true, trans('lang.Success_text'), (object)[], success());
        }

        else {
            return msg(false, trans('lang.check_location'), failed());
        }


    }

    public function haversine($lat1, $lon1, $lat2, $lon2)
    {
        $earthRadius = 6371; // Earth radius in kilometers

        $dLat = deg2rad($lat2 - $lat1);
        $dLon = deg2rad($lon2 - $lon1);

        $a = sin($dLat / 2) * sin($dLat / 2) +
            cos(deg2rad($lat1)) * cos(deg2rad($lat2)) *
            sin($dLon / 2) * sin($dLon / 2);
        $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
        $distance = $earthRadius * $c; // Distance in kilometers

        return $distance;
    }

// Example usage:


}
