<?php

namespace App\Http\Controllers;

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

class TrainingTypeController extends Controller
{

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

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

        //2. Authorize
        //without authorization... only authentication

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

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

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

        //2. Authorize
        //without authorization

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

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

    public function create(Request $request) {
        //1. Params

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

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

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

    public function update(Request $request, $id) {
        //1. Params

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

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

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

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

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

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

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