<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Team;
use Auth;

class TeamsController extends Controller
{
    public function __construct()
	{
		if (!request()->wantsJson()) {
            $this->middleware('auth');    
        }
	}

    /**
     * Display a listing of the Teams.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {   
        //return response()->JSON(auth);
        //Auth::loginUsingId(13);
        $response = auth()->user()->type == 'admin' ? Team::getAllTeams() : Team::getAllAuthTeams();
        if (request()->wantsJson()) {
            return response()->JSON($response);
        } else {
            return view('user-pages.teams.reactive',[
                'teams' => $response
            ]);
        }
    }

    /**
     * Show the form for creating a new Game.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if (request()->wantsJson())
            return null;
        
 		return view('user-pages.teams.reactive',[
            'games' => auth()->user()->games
        ]);
    }

     /**
     * Show the form for editing the specified Team.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function edit(Team $team)
    {
        if (request()->wantsJson())
            return null;
        
        $this->authorize('view',$team);
        $games = auth()->user()->games;

 		return view('user-pages.teams.reactive')->with(compact('team','games'));
    }

     /**
     * Display the specified Team.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function show(Team $team)
    {
    	$this->authorize('view',$team);

        if (request()->wantsJson()) {
            return $team->load('game');
        } else {
            return view('user-pages.teams.reactive')->with(compact('game'));
        }   
    }

     /**
     * Update the specified Team in storage.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function update(Team $team)
    {
    	$this->authorize('update',$team);

        $attributes = $this->validateTeam();
        $attributes['user_id'] = auth()->user()->type == 'admin' ? request()->get('user_id') : auth()->id();

    	$team->update($attributes);

        if (request()->wantsJson()) {
            return response()->JSON($team);
        } else {
            return redirect('/teams');
        }
    }

     /**
     * Store a newly created Team in storage.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function store(Team $team)
    {
        $this->authorize('store', $team);

        $attributes = $this->validateTeam();
        $attributes['user_id'] = auth()->user()->type == 'admin' ? request()->get('user_id') : auth()->id();

 		$team = Team::create($attributes);

 		if (request()->wantsJson()) {
            return response()->JSON($team);
        } else {
            return redirect('/teams');
        }
    }

    /**
     * Remove the specified Game from storage.
     *
     * @param  \App\Team  $team
     * @return \Illuminate\Http\Response
     */
    public function destroy(Team $team)
    {
        $this->authorize('view',$team); 
        $team->delete();

        if (request()->wantsJson()) {
            return response()->JSON(['status' => 'success']);
        } else {
            return redirect('/teams')->with('message','The team ' . $team->name . ' deleted successfully');
        }
    }

     /**
     * Validate the specified team.
     *
     * @return  \Illuminate\Http\Request  $request
     */
    protected function validateTeam()
    {
    	return request()->validate([
            'game_id' => 'required',
            'name' => 'required'
    	]);
    }
}
