<?php

   namespace App\Http\Controllers;

   use App\Notifications\InviteSent;
   use App\User;
   use Illuminate\Http\Request;
   use Illuminate\Support\Facades\Auth;
   use Junaidnasir\Larainvite\Facades\Invite;

   class InvitationsController extends Controller {

      public function index() {
         $invitations = auth()->user()->invitations;

         return view( 'invitations.index', compact( 'invitations' ) );
      }

      public function create( Request $request ) {
         $this->validate( $request, [ 'email' => 'unique:users,email', ] );
         $user_id = auth()->user()->id;
         $code = Invite::invite( $request->email, $user_id );
         $invitation = Invite::get( $code );
         auth()->user()->notify( new InviteSent( $invitation ) );
         flash( 'Invite sent to ' . $invitation->email );

         return redirect( action( 'InvitationsController@index' ) );
      }

      public function acceptInvite( $code ) {
         try {
            $invitation = Invite::get( $code );
         } catch ( Exception $e ) {
            return view( 'errors.404' );
         }
         if ( isset( $invitation ) ) {
            return view( 'invitations.accept', compact( 'invitation' ) );
         }
      }

      public function createUser( Request $request ) {
         // ensure invite still valid
         try {
            $invitation = Invite::get( $request->code );
         } catch ( Exception $e ) {
            return view( 'errors.404' );
         }
         if ( isset( $invitation ) ) {
            // check that email hasnt changed from invite and the user doesnt exist
            // make sure passwords match AND MEET MINIMUM STANDARDS
            $this->validate( $request,
                             [
                                "name"                  => 'min:2|max:255',
                                "password"              => 'required|min:2|max:255|confirmed',
                                "password_confirmation" => 'required',
                                "email"                 => 'unique:users,email|size:' . strlen( $invitation->email ),
                             ] );
            // create user
            $newUser = $request->except( '_token' );
            $newUser[ 'password' ] = bcrypt( $request->password );
            if ( Invite::isAllowed( $invitation->code, $invitation->email ) ) {
               Invite::consume( $invitation->code );
               $user = User::create( $newUser );
            } else {
               if ( $invitation->status == 'expired' ) {
                  flash( 'Uh oh! Your invitation code has expired.', 'danger' );
               } elseif ( $invitation->status == 'canceled' ) {
                  flash( 'Uh oh! Your invitation code has been canceled.', 'danger' );
               } elseif ( $invitation->status == 'successful' ) {
                  flash( 'Your invitation code already been used!', 'danger' );
               }

               return redirect()->back();
            }
            // log in as user
            Auth::login( $user, true );
            // redirect to "/"
            flash( 'Your account has been created and you are logged in to ' . $user->email );

            return redirect( '/equipment' );
         }
      }
   }