<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\Customers;
use Session;

class LoginController extends Controller {

    public function __construct() {
        //$this->middleware('auth:customers');
    }

    public function index() {
        
        if (!empty(auth('customers')->user())) {
            return redirect("/");
        }
        
        
        
        return view('front/login');
    }

    public function login(Request $request) {

        
        
        
        $rules = [
            'email' => 'required|string|email',
            'password' => 'required|string',
                //'remember_me' => 'boolean'
        ];


        $header = 200;
        $message = [];
        $validator = Validator::make($request->all(), $rules, $message);
        $response['error'] = 1;

        if ($validator->fails()) {
            $errors = $validator->errors();
            $response['errors'] = $errors;
            $header = 401;
            //return response()->json($response, 402);
        } else {

            $credentials["email"] = $request->email;
            $credentials["password"] = $request->password;
            //$credentials["status"] = "1";

            $response['url'] = url("checkout");
            $response['errors']["credentials"] = "Your email or password is incorrect!";
            $header = 401;
            if (Auth::guard('customers')->attempt(['email' => $request->email, 'password' => $request->password], $request->get('remember'))) {


                if ($request->remember) {
                    $remember = true;
                } else {
                    $remember = false;
                }

                $data = Customers::where($credentials)->first();
                $user = auth()->guard('customers')->user();
                $tokenResult = $user->createToken(env("APP_NAME"));
                $token = $tokenResult->token;

                if ($remember) {
                    $token->expires_at = Carbon::now()->addWeeks(1);
                }

                $token->save();
                $response = [
                    'error' => false,
                    'access_token' => $tokenResult->accessToken,
                    'token_type' => 'Bearer',
                    'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString()
                ];

                $package = Session::get('package');
                $type = Session::get('type');
                $excess_id = Session::get('excess_id');
                if (isset($package) && isset($type) && isset($excess_id)) {
                    $response['url'] = url("checkout");
                } else {
                    $response['url'] = url("account");
                }
                $header = 200;
            } else {
                
            }
        }

        if ($request->ajax()) {
            //return redirect()->back()->withErrors($response["errors"])->withInput();
            return response()->json($response, $header);
        } else {

            if ($response['error'] == 1) {
                return redirect()->back()->withErrors($response["errors"])->withInput();
            } else {
                return redirect()->intended('/admin/');
            }
        }
    }

}
