<?php

namespace App\Http\Controllers;

use App\Models\TabUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;

class AuthController extends Controller
{
    public function loginView()
    {
        return view('login');
    }

    // public function login(Request $request)
    // {

    //     $validator = Validator::make($request->all(), [
    //         'username' => ['required'],
    //         'password' => ['required'],
    //     ]);

    //     if ($validator->fails()) {
    //         return redirect()->back()->withErrors($validator)->withInput();
    //     }
    //     $credentials = [
    //         'USERNAME' => $request->input('username'), 
    //         'password' => $request->input('password')
    //     ];
    //     $user = TabUser::where('USERNAME', 'sarun')->where('PASSWORD', 'sarun')->first();
    //     if (Auth::loginUsingId($user->UID)) {
    //         return redirect()->route('index');
    //     } else {
    //         $errors = new \Illuminate\Support\MessageBag();
    //         $errors->add('password', 'The password does not match with username');
    //         return redirect()->back()->withErrors($errors)->withInput();
    //     }
    // }
    public function login(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'username' => ['required'],
            'password' => ['required'],
        ]);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
     
        

        $validated = $validator->validated();

        if (Auth::attempt(array('username' => $validated['username'], 'password' => $validated['password']))) {
            
            // $token = auth()->user()->createToken('app1_token')->plainTextToken;
            // session(['shared_user_id' => auth()->id()]);
            return redirect()->route('index');
        } else {
            $validator->errors()->add(
                'password',
                'The password does not match with username'
            );
            return redirect()->back()->withErrors($validator)->withInput();
        }
    }

    public function registerView()
    {
        return view('register');
    }

    public function register(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'name' => ['required', 'string'],
            'email' => ['required', 'email', 'unique:users'],
            'password' => ['required', "confirmed", Password::min(7)],
        ]);

        $validated = $validator->validated();

        $user = User::create([
            'name' => $validated["name"],
            "email" => $validated["email"],
            "password" => Hash::make($validated["password"])
        ]);

        auth()->login($user);

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

    public function logout()
    {
        auth()->logout();
        return redirect()->route('login');
    }
}
