<?php

namespace Modules\Travel\Http\Controllers\Api\V11;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Modules\Travel\Entities\TravelPlan;
use Modules\Travel\Http\Requests\TravelPlanRequest;
use Modules\Travel\Transformers\TravelPlanResource;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use Modules\Travel\Repositories\TravelPlanRepository;

class TravelPlanApiController extends Controller
{
    use ApiReturnFormatTrait;

    protected $travelPlanRepo;

    public function __construct()
    {
        $this->travelPlanRepo = new TravelPlanRepository;
    }
    
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        try {
            $travelPlans    = TravelPlan::query()
                            ->where('employee_id', auth()->id())
                            ->when(!isAdminOrHr(), fn ($q) => $q->where('employee_id', auth()->id()))
                            ->when(request('from_date') && request('from_date') != null, function ($q) {
                                $q->where('from_date', '>=', Carbon::parse(request('from_date'))->format('Y-m-d'));
                            })
                            ->when(request('to_date') && request('to_date') != null, function ($q) {
                                $q->where('to_date', '<=', Carbon::parse(request('to_date'))->format('Y-m-d'));
                            })
                            ->latest('id')
                            ->paginate(10);

            $data           = TravelPlanResource::collection($travelPlans)->response()->getData(true);

            return $this->responseWithSuccess('Success', $data, 200);
        } catch (\Throwable $th) {
            return $this->responseWithError(__('Something went wrong.'), [$th->getMessage()], 400);
        }
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Response
     */
    public function store(TravelPlanRequest $request)
    {
        try {

            $travelPlan = $this->travelPlanRepo->storeTravelPlan($request);

            return $this->responseWithSuccess(__('Travel Plan has been created successfully!'), new TravelPlanResource($travelPlan), 200);

        } catch (\Throwable $th) {
            return $this->responseWithError(__('Something went wrong.'), [$th->getMessage()], 400);
        }        
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Response
     */
    public function update(TravelPlanRequest $request, $id)
    {
        try {

            if (!TravelPlan::find($id)) {
                return $this->responseWithError(__('Travel Plan not found!'), [], 404);
            }

            $travelPlan = $this->travelPlanRepo->updateTravelPlan($request, $id);

            return $this->responseWithSuccess(__('Travel Plan has been updated successfully!'), new TravelPlanResource($travelPlan), 200);

        } catch (\Throwable $th) {
            return $this->responseWithError(__('Something went wrong.'), [$th->getMessage()], 400);
        }        
    }
}
