<?php
namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Ales0sa\Laradash\Models\User as UserDash;

class AuthController extends Controller
{


    public function register(Request $request, User $user)
    {
        return $user->saveUser($request)
            ->generateAndSaveApiAuthToken();
    }

    public function token(Request $request, UserDash $user)
    {
        $token = $request->bearerToken();

        return UserDash::where('api_token', $token)
            ->firstOrFail();
    }

    public function bytoken(Request $request, UserDash $user)
    {
        $token = $request->bearerToken();

        return UserDash::where('api_token', $token)
            ->firstOrFail();
    }

    

    public function login(Request $request)
    {
        $credentials = [
            'username' => $request->username,
            'password' => $request->password,
        ];

        if (Auth::guard('web')->attempt($credentials)) {
            $user = Auth::guard('web')
                        ->user()
                        ->generateAndSaveApiAuthToken();

            $ldash_user = UserDash::where('id', $user->id)
                                ->firstOrFail();

            return response()->json(['user'=> $ldash_user]);
        }

        return response()->json(['message' => 'Credenciales No coinciden con nuestros registros'], 401);
    }

    public function loginOP(Request $request)
    {
        $credentials = [
            'username' => $request->username,
            'password' => $request->password,
        ];

        if (Auth::guard('web')->attempt($credentials)) {
            $user = Auth::guard('web')
                        ->user()
                        ->generateAndSaveApiAuthToken();

            $ldash_user = UserDash::where('id', $user->id)
                                ->firstOrFail();

            return response()->json(['user'=> $ldash_user]);
        }

        return response()->json(['message' => 'Error.....'], 401);
    }

    public function token_operator($id)
    {
        $user = UserDash::where('id', $id)
                        ->firstOrFail();
                        
                        $login = Auth::loginUsingId($id);

        if (Auth::guard('web')->check()) {


            $user = Auth::guard('web')
                        ->user()
                        ->generateAndSaveApiAuthToken();


            $ldash_user = UserDash::where('id', $user->id)
                                ->firstOrFail();

            return response()->json(['user'=> $ldash_user]);
        }

        return response()->json(['message' => 'Error.....'], 401);

    }

    public function token_operator_access($id)
    {
        $user = UserDash::where('id', $id)
                        ->firstOrFail();
                        
                        $login = Auth::loginUsingId($id);

        if (Auth::guard('web')->check()) {


            $user = Auth::guard('web')
                        ->user()
                        ->generateAndSaveApiAuthToken();


            $ldash_user = UserDash::where('id', $user->id)
                                ->firstOrFail();

            return response()->json(['user'=> $ldash_user]);
        }

        return response()->json(['message' => 'Error.....'], 401);

    }

    public function by_token($id)
    {
        $user = UserDash::where('id', $id)
                        ->firstOrFail();
                        
                        $login = Auth::loginUsingId($id);

        if (Auth::guard('web')->check()) {


            $user = Auth::guard('web')
                        ->user()
                        ->generateAndSaveApiAuthToken();


            $ldash_user = UserDash::where('id', $user->id)
                                ->firstOrFail();

            return response()->json(['user'=> $ldash_user]);
        }

        return response()->json(['message' => 'Error.....'], 401);

    }

    public function logout(Request $request)
    {
        $user = Auth::guard('api')->user();

        if ($user) {
            $user->api_token = null;
            $user->save();
        }

        return response()->json(['Success' => 'Logged out'], 200);
    }
}