<?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\TravelMeeting;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use Modules\Travel\Http\Requests\TravelMeetingRequest;
use Modules\Travel\Transformers\TravelMeetingResource;
use Modules\Travel\Repositories\TravelMeetingRepository;

class TravelMeetingApiController extends Controller
{
    use ApiReturnFormatTrait;

    protected $travelMeetingRepo;

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

            $data           = TravelMeetingResource::collection($travelMeetings)->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(TravelMeetingRequest $request)
    {
        try {

            $travelMeeting = $this->travelMeetingRepo->storeTravelMeeting($request, $request->travel_plan_id);

            return $this->responseWithSuccess(__('Travel Meeting has been created successfully!'), new TravelMeetingResource($travelMeeting), 200);

        } catch (\Throwable $th) {
            return $this->responseWithError(__('Something went wrong.'), [$th->getMessage()], 400);
        }        
    }
    /**
     * Show the specified resource.
     * @param int $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

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

            $travelMeeting = $this->travelMeetingRepo->updateTravelMeeting($request, $id);

            return $this->responseWithSuccess(__('Travel Meeting has been updated successfully!'), new TravelMeetingResource($travelMeeting), 200);

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

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}
