<?php

namespace App\Http\Controllers;

use App\Repositories\BoxRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\TrainingSessionRepository;
use App\Repositories\UserBenchmarkRepository;
use Illuminate\Http\Request;

class UserBenchmarkController 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");

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

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

    public function show($id) {
        //1. Params
        $box_id = UserBenchmarkRepository::box($id)->id;
        $user_id = UserBenchmarkRepository::user($id)->id;

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

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

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

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

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

            //3. Create
            $exercise = UserBenchmarkRepository::create($request->all());
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    /*public function update(Request $request, $id) {
        try {
            //1. Params
            $box_id = TrainingSessionRepository::box($request->training_session_id)->id;
            $company_id = BoxRepository::company($box_id);

            //2. Authorize

            //3. Update
            $exercise = UserBenchmarkRepository::update($request->all(), $id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    public function destroy($id) {
        try {
            //1. Params
            $box_id = UserBenchmarkRepository::box($id);
            $company_id = BoxRepository::company($box_id);

            //2. Authorize

            //3. Delete
            UserBenchmarkRepository::delete($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

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

        //2. Authorize
        //without authorization

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

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