<?php

namespace App\Http\Controllers;

use App\Box;
use App\Repositories\BoxRepository;
use App\Repositories\CompanyRepository;
use App\Repositories\UserSubscriptionRepository;
use App\Repositories\TrainingSessionRepository;
use App\Repositories\UserRepository;
use App\TrainingSession;
use App\TrainingType;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use PhpParser\Error;

class TrainingSessionController extends Controller
{
    private $trainingSessionRepository;
    private $boxRepository;
    private $companyRepository;

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

    public function index(Request $request) {
        //1. Query params
        auth()->user()->authorize("super_admin");

        //2. Authorize
        try {
            $ts = TrainingSessionRepository::all();
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

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

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

        //3. Retrieve
        try {
            $ts = TrainingSessionRepository::find($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        //3. Get
        try {
            $benchmarks = TrainingSessionRepository::userBenchmarks($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

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

    public function join($id,$idUser){
        //1. Params
        $box_id = TrainingSessionRepository::box($id)->id;
        $company_id = BoxRepository::company($box_id)->id;

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

        try{
            //3. Join
            $session = TrainingSessionRepository::join($id,$idUser);
        } catch (\Exception $exception){ return response()->json(["error" => $exception->getMessage()], 400); }

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

    public function leave($id,$idUser) {
        try {
            //1. Params
            $box_id = TrainingSessionRepository::box($id)->id;
            $company_id = BoxRepository::company($box_id)->id;

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

            $session = TrainingSessionRepository::leave($id, $idUser);
        } catch (\Exception $exception) { return response()->json(["error" => $exception->getMessage()], 400); }

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