<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class Login extends Controller
{
    public function index(Request $request)
    {
        $request->validate(['phone' => 'required']);
        $users = User::where('phone', $request->input('phone'))->get();
        if (count($users) == 0) {

            return back()->withErrors(['phone' => 'Phone number incorrect!']);
        }
        if ($users->first()->teams->first()->id == 1 || $users->first()->teams->first()->id == 2) {
            return back()->withErrors(['phone' => 'Access Denied !']);
        }
        $user = $users->first();
        Auth::login($user);
        $id = auth()->user()->drives->first()->id;
        $home = '/users/drives/' . $id;
        return redirect()->to($home);
    }
}
