<?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\TravelWorkflow;
use App\Helpers\CoreApp\Traits\ApiReturnFormatTrait;
use Modules\Travel\Http\Requests\TravelWorkflowRequest;
use Modules\Travel\Repositories\TravelWorkflowRepository;
use Modules\Travel\Transformers\TravelWorkflowResource;

class TravelWorkflowApiController extends Controller
{
    use ApiReturnFormatTrait;

    protected $travelWorkflowRepo;

    public function __construct()
    {
        $this->travelWorkflowRepo = new TravelWorkflowRepository;
    }
    
    /**
     * Display a listing of the resource.
     * @return Response
     */
    public function index()
    {
        try {
            $travelWorkflows    = TravelWorkflow::query()
                                ->where('employee_id', auth()->id())
                                ->when(!isAdminOrHr(), fn ($q) => $q->where('employee_id', auth()->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               = TravelWorkflowResource::collection($travelWorkflows)->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(TravelWorkflowRequest $request)
    {
        try {

            $travelWorkflow = $this->travelWorkflowRepo->storeTravelWorkflow($request);

            return $this->responseWithSuccess(__('Travel Workflow has been created successfully!'), new TravelWorkflowResource($travelWorkflow), 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(TravelWorkflowRequest $request, $id)
    {
        try {

            $travelWorkflow = $this->travelWorkflowRepo->updateTravelWorkflow($request, $id);

            return $this->responseWithSuccess(__('Travel Workflow has been updated successfully!'), new TravelWorkflowResource($travelWorkflow), 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)
    {
        //
    }
}
