<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class AuthController extends Controller
{

    /**
     * Get current logged in admin
     */
    public function getCurrentAdmin()
    {
        $admin = Auth::guard('admin')->user();
        return response()->json([
            'success' => true,
            'admin' => [
                'id' => $admin->id,
                'nama_lengkap' => $admin->nama_lengkap,
                'email' => $admin->email,
                'foto_profil' => $admin->foto_profil ? asset('images/photo/' . $admin->foto_profil) : null,
            ]
        ]);
    }

    /**
     * Show login form
     */
    public function showLoginForm()
    {
        return view('auth.login');
    }

    /**
     * Handle login request
     */
    public function login(Request $request)
    {
        // Validasi input
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required|min:6',
        ], [
            'email.required' => 'Email harus diisi',
            'email.email' => 'Format email tidak valid',
            'password.required' => 'Password harus diisi',
            'password.min' => 'Password minimal 6 karakter',
        ]);

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

        // Coba login
        $credentials = $request->only('email', 'password');

        if (Auth::guard('admin')->attempt($credentials, $request->filled('remember'))) {
            $request->session()->regenerate();

            return redirect()->intended(route('admin.dashboard'))
                ->with('success', 'Login berhasil! Selamat datang.');
        }

        return redirect()->back()
            ->with('error', 'Email atau password salah!')
            ->withInput($request->except('password'));
    }

    /**
     * Handle logout request
     */
    public function logout(Request $request)
    {
        Auth::guard('admin')->logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect()->route('login')
            ->with('success', 'Logout berhasil!');
    }

    /**
     * Show forgot password form
     */
    public function showForgotPasswordForm()
    {
        return view('auth.forgot-password');
    }

    /**
     * Process forgot password request
     */
    public function processForgotPassword(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
        ], [
            'email.required' => 'Email harus diisi',
            'email.email' => 'Format email tidak valid',
        ]);

        $admin = Admin::where('email', $request->email)->first();

        if ($admin) {
            return redirect()->route('password.reset', ['email' => $request->email]);
        }

        return redirect()->back()->with('swal_error', 'Email yang Anda masukkan tidak terdaftar dalam sistem. Harap masukkan email yang sudah terdaftar!');
    }

    /**
     * Show reset password form
     */
    public function showResetPasswordForm($email)
    {
        return view('auth.reset-password', compact('email'));
    }

    /**
     * Process reset password request
     */
    public function processResetPassword(Request $request)
    {
        $request->validate([
            'email' => 'required|email|exists:admins,email',
            'password' => 'required|min:6|confirmed',
        ], [
            'email.required' => 'Email harus diisi',
            'email.exists' => 'Email tidak terdaftar',
            'password.required' => 'Password baru harus diisi',
            'password.min' => 'Password minimal 6 karakter',
            'password.confirmed' => 'Konfirmasi password tidak cocok',
        ]);

        $admin = Admin::where('email', $request->email)->first();
        $admin->password = Hash::make($request->password);
        $admin->save();

        return redirect()->route('login')->with('swal_success', 'Perubahan password baru berhasil dilakukan!');
    }
}