<?php

namespace App\Http\Controllers;

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

class TrainingRoomController extends Controller
{

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

    public function index(Request $request) {
        //1. Query params

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

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

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

    public function show($id) {
        //1. En aquest cas particular, primer hem d'obtenir el training room per obtenir el box id per fer auth
        try {
            //3. Get
            $tr = TrainingRoomRepository::find($id);
        }
        catch(\Exception $e) { return response()->json(["error" => $e->getMessage()], 400); };

        //2. Params
        $box_id = $tr->box_id;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

}
