<?php

namespace App\Http\Controllers\Auth;

use App\Models\User;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Services\MailgunService;
use App\Http\Controllers\Mailgun\MailgunController;
use App\Models\CompanyUser;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/dashboard';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
     
     protected $mailgunService;
     
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
         $this->mailgunService = app(MailgunService::class);
    }

    public function login()
    {
        return view('auth.login');
    }

    public function storeLogin(Request $request)
    {
        $credentials = $request->validate([
            'email' => ['required', 'email'],
            'password' => ['required'],
        ]);
 
        if (Auth::attempt($credentials)) {
           // dd(Auth::user());
            if(Auth::user()->role_id == 3){
                $company = CompanyUser::where('user_id',Auth::id())->first();
                
                if($company){
                  
                    Session::put('selected_company', $company->company_id);
                     return redirect('/dashboard');
                }
                
            }else{
             
            if(!Auth::user()->Company) {
                return redirect('/company-info');
            } else {
                $company = Company::where('user_id', Auth::id())->first();
                $domain = $company->domain;
                
                $mailgunDomain = $this->mailgunService->getDomain($domain);
        
                if($mailgunDomain){
                    
                        $state = $mailgunDomain->getDomain()->getState();
            
                        if($state == 'unverified'){
                            return redirect()->route('showDomain',$domain);
                        }elseif($state == 'active'){
                            
                            if(empty($company->internal_email)){
                                
                                $mailgun = new MailgunController();
                                $mailgun->createEmail($domain);
                            }
                
                         Session::put('selected_company', $company->id);
                         return redirect('/dashboard');
                         
                     }
                }
                
               
            }
            
            }
            
            
        }

        return redirect()->back()->with('error', 'Invalid Credentials');

    }

    public function logout(Request $request)
    {
        Auth::logout();
        Session::flush();

        return redirect()->route('login');
    }
}
