<?php

namespace App\Http\Controllers;

use App\Repositories\BoxRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\trainingWorkoutRepository;
use Illuminate\Http\Request;

class TrainingWorkoutController extends Controller
{
    public function __construct() {
        $this->middleware('auth:api', ['except' => []]);
    }


    public function index(Request $request) {
        //1. Obtenim query parameters opcionals

        //2. Authorize
        auth()->user()->authorize("super_admin");

        //3. Obtenemos
        try {
            $tw = TrainingWorkoutRepository::all();
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($tw, 200);
    }

    public function show($id) {
        //1. Params

        //2. Authorize
        //sense auth

        //3. Obtenemos
        try {

            $cap = TrainingWorkoutRepository::find($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($cap, 200);
    }

    public function create(Request $request) {
        //1. Params
        $box_id = $request->box_id;

        //2. Authorize
        if($box_id) {
            auth()->user()->authorize("coach", $box_id);
        }
        else {
            auth()->user()->authorize("super_admin");
        }

        try {
            if (isset($request->workouts)) {
                $cap = [];
                foreach ($request->workouts as $w) {
                    array_push($cap,TrainingWorkoutRepository::create($w));
                }
            } else $cap = TrainingWorkoutRepository::create($request->all());
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($cap, 200);
    }

    public function update(Request $request, $id) {
        //1. Params
        $box_id = $request->box_id;

        //2. Authorize
        if($box_id) {
            auth()->user()->authorize("coach", $box_id);
        }
        else {
            auth()->user()->authorize("super_admin");
        }

        try {
            $cap = TrainingWorkoutRepository::update($request->all(), $id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json($cap, 200);
    }

    public function destroy($id) {
        //1. Params
        $box_id = TrainingWorkoutRepository::box($id)->id;

        //2. Authorize
        if($box_id) {
            auth()->user()->authorize("coach", $box_id);
        }
        else {
            auth()->user()->authorize("super_admin");
        }

        try {
            TrainingWorkoutRepository::delete($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        return response()->json(["message" => "Ok"], 200);
    }

    public function box($id) {
        try {
            //1. Params
            $box = TrainingWorkoutRepository::box($id);
            $box_id = $box->id;
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        //2. Authorize
        auth()->user()->authorize("athlete", $box_id);

        if(!$box) return response()->json(["error" => "Este TrainingWorkout no es de ningún box."]);
        else return response()->json($box, 200);
    }

    public function available(Request $request, $user_id) {


        //2. Authorize
        if(!auth()->user()->itsMe($user_id)) {
            auth()->user()->authorize("super_admin");
        }

        $params = [
            "user_id" => $user_id,
            "sort_dir" => $request->has('sort_dir') ? $request->sort_dir : 'ASC' ,
            "sort_col" => $request->has('sort_col') ? $request->sort_col : 'id',
            "name" => $request->has('name') ? $request->name : '',
            "category" => $request->has('category') ? $request->category : '',
            "seconds" => $request->has('seconds') ? $request->seconds : null,
            "section_types" => $request->has('section_types') ? explode('/', $request->section_types) : null,
            "exercise_types" => $request->has('exercise_types') ? explode('/', $request->exercise_types) : null,
            "difficulty" => $request->has('difficulty') ? $request->difficulty : 3,
            "equipment" => $request->has('equipment') ? explode('/', $request->equipment) : null,
            "favorites" => $request->has('favorites') ? ($request->favorites==1 ? true : false) : false,
        ];

        try {
            return response()->json(TrainingWorkoutRepository::available($params),200);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };
    }

    public function sectionTypes() {
        try {
            return response()->json(TrainingWorkoutRepository::sectionTypes(),200);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };
    }

}
