<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    public function index()
    {

        if (Auth::check()) {
            return redirect()->route('dashboard');
        }
        return view('auth.login');
    }

    public function login(Request $request)
    {
        $credentials = $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        if (Auth::attempt($credentials)) {
            $user = Auth::user();
            if ($user->status_aktif == 0) {
                Auth::logout();
                if ($request->ajax() || $request->wantsJson()) {
                    return response()->json([
                        'success' => false,
                        'message' => 'Akun Anda telah dinonaktifkan.'
                    ], 403);
                }
                return back()->with('error', 'Akun Anda telah dinonaktifkan.');
            }

            $hasWebRole = $user->roles()->whereIn('nama_role', ['hrd', 'manager', 'supervisor'])->exists();
            if (!$hasWebRole) {
                Auth::logout();
                if ($request->ajax() || $request->wantsJson()) {
                    return response()->json([
                        'success' => false,
                        'message' => 'Akun Anda tidak memiliki akses ke Web. Silakan gunakan aplikasi Mobile.'
                    ], 403);
                }
                return back()->with('error', 'Akun Anda tidak memiliki akses ke Web. Silakan gunakan aplikasi Mobile.');
            }

            $request->session()->regenerate();

            if ($request->ajax() || $request->wantsJson()) {
                return response()->json([
                    'success' => true,
                    'message' => 'Login berhasil!',
                    'redirect' => route('dashboard')
                ]);
            }

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

        if ($request->ajax() || $request->wantsJson()) {
            return response()->json([
                'success' => false,
                'message' => 'Email atau Password yang Anda masukkan salah.'
            ], 401);
        }

        return back()
            ->withInput($request->only('email'))
            ->with('error', 'Email atau Password yang Anda masukkan salah.')
            ->withErrors(['email' => 'Email atau Password yang Anda masukkan salah.']);
    }

    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/login');
    }
}
