<?php

namespace App\Http\Controllers\Auth;

use App\Helpers\OtpHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use App\Mail\OtpMail;
use App\Models\Otp;
use App\Providers\RouteServiceProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\View\View;

class AuthenticatedSessionController extends Controller
{
    /**
     * Display the login view.
     */
    public function create(): View
    {
       /* if (config('settings.send_sms') === '1') {
            // Replace the IP with the value from the configuration
            $smsApiIp = config('settings.sms_ip');

            // Send a GET request to the Django API
            $apiReachable = @fsockopen($smsApiIp, 8080, $errno, $errstr, 5);

            if (!$apiReachable) {
                // Set the "danger" message in the session
                session()->flash('danger', 'SMS System down');
            }
        }*/

        return view('auth.login');
    }

  

    /*public function store(LoginRequest $request): RedirectResponse
    {
        /*if (config('settings.send_sms') === '1') {
            // Replace the IP with the value from the configuration
            $smsApiIp = config('settings.sms_ip');
    
            // Send a GET request to the Django API
            $apiReachable = @fsockopen($smsApiIp, 8080, $errno, $errstr, 5);
    
            if (!$apiReachable) {
                // Set the "danger" message in the session
                session()->flash('danger', 'SMS System down');
                return redirect()->back();
            }
        }*/
    
        // Generate OTP
      /*  $otp = OtpHelper::generateOtp();
        Otp::create(['email' => $request->email, 'otp' => $otp]);
    
        // Send OTP via email
        Mail::to($request->email)->send(new OtpMail($otp));
    
        // Store OTP and user credentials in session
        session([
            'otp_sent' => true,
            'otp_email' => $request->email,
            'otp_password' => $request->password,
        ]);
    
        // Redirect to OTP verification page
        return redirect()->route('otp.verify');
    }*/
    

public function store(LoginRequest $request): RedirectResponse
    {
        // Send a GET request to the Django API
        $apiReachable = @fsockopen('69.63.75.76', 8080, $errno, $errstr, 5);

        if (!$apiReachable) {
            // Set the "danger" message in the session
            session()->flash('danger', 'SMS System down');
            return redirect()->back();
        }
        
        $request->authenticate();

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

        return redirect()->intended(RouteServiceProvider::HOME);
    }

    /**
     * Destroy an authenticated session.
     */
    public function destroy(Request $request): RedirectResponse
    {
        Auth::guard('web')->logout();

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

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

        return redirect('/');
    }
}
