<?php

namespace App\Http\Controllers\Api\Dashboard\Admin\Workout;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Dashboard\Admin\Workout\WorkoutRequest;
use App\Http\Resources\Api\Dashboard\Admin\Workout\{SimpleWorkoutResource, WorkoutResource};
use App\Models\{Workout, User};
use Illuminate\Http\Request;

class WorkoutController extends Controller
{

    public function indexWithoutPagination()
    {
        $countries = Workout::latest()->get();
        return WorkoutResource::collection($countries)->additional(['status' => 'success', 'message' => '']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $countries = Workout::when($request->keyword, function ($q) use ($request) {
            $q->whereTranslationLike('title', '%' . $request->keyword . '%')
                ->orWhereTranslationLike('desc', '%' . $request->keyword . '%');
        })->when($request->workout_category_id, function ($q) use ($request) {
            $q->where('workout_category_id', $request->workout_category_id);
        })->latest()->paginate(25);
        return WorkoutResource::collection($countries)->additional(['status' => 'success', 'message' => '']);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(WorkoutRequest $request)
    {
        $Workout = Workout::create($request->validated()+ ['added_by' => 'admin']);
        return WorkoutResource::make($Workout)->additional(['status' => 'success', 'message' => trans('Created successfully')]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $Workout = Workout::findOrFail($id);
        return WorkoutResource::make($Workout)->additional(['status' => 'success', 'message' => '']);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(WorkoutRequest $request, $id)
    {
        $Workout = Workout::findOrFail($id);
        $Workout->update($request->validated());

        return WorkoutResource::make($Workout)->additional(['status' => 'success', 'message' => trans('Updated successfully')]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $Workout = Workout::findOrFail($id);
        if ($Workout->delete()) {
            return response()->json(['status' => 'success', 'data' => null, 'message' => trans('Deleted successfully')]);
        }
        return response()->json(['status' => 'fail', 'data' => null, 'message' => trans('Something went wrong, please try again')], 422);
    }
}
