<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{
    /**
     * Menampilkan halaman login
     */
    public function showLoginForm()
    {
        return view('auth.login');
    }

    /**
     * Memproses login Firebase dari frontend via AJAX
     */
    public function processLogin(Request $request)
    {
        // Validasi request
        $request->validate([
            'firebase_uid' => 'required|string',
            'email' => 'required|email'
        ]);

        // Simpan data user di session
        session([
            'firebase_auth_checked' => true,
            'firebase_uid' => $request->firebase_uid,
            'user_email' => $request->email
        ]);

        return response()->json([
            'success' => true,
            'redirect' => route('dashboard')
        ]);
    }

    /**
     * Proses logout
     */
    public function logout()
    {
        // Hapus data autentikasi dari session
        Session::forget(['firebase_auth_checked', 'firebase_uid', 'user_email']);
        
        return redirect()->route('login');
    }

    /**
     * Memeriksa status autentikasi user untuk mencegah redirect loop
     */
    public function checkSession()
    {
        return response()->json([
            'authenticated' => session()->has('firebase_auth_checked') && session()->get('firebase_auth_checked')
        ]);
    }
} 