<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\DB;
use DateTime;
use Illuminate\Support\Facades\Session;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        Session::flush();
        $data = DB::table('configuracion')
            ->where('idconf', '=', '1')
            ->first();
        if ($data->fecha <= date("Y-m-d")) {
            $diff = 0;
        } else {
            $earlier = new DateTime(date("Y-m-d"));
            $later = new DateTime($data->fecha);
            $diff = $later->diff($earlier)->format("%a");
        }

        Session::put('fecha', $data->fecha);
        Session::put('dias', $diff);

        $busque = DB::table('busqueda')
            ->where('estado', '=', 1)
            ->first();
        Session::put('activar', $busque->idbusqueda);
        /*$fechaLimite = '2025-07-21';
        $claveCorrecta = 'bateriasEcuadorrr@';
        $hashEsperado = 'a82e34964f8656a6aed7d1d89ccd7a251e75c2e348248da6691a817f1659e267';
        $hashActual = hash('sha256', $claveCorrecta . $fechaLimite);
        if (date('Y-m-d') > $fechaLimite || $hashActual !== $hashEsperado) {
            die('ACCESS BLOCKED. Please contact the administrator.');
        }*/

        $this->middleware('guest')->except('logout');
    }

    /**
     * Override attemptLogin to prevent login if hardcoded date is expired.
     */
    protected function attemptLogin(Request $request)
    {
        /*$fechaLimite = '2025-07-21';
        $claveCorrecta = 'bateriasEcuadorrr@';
        $hashEsperado = 'a82e34964f8656a6aed7d1d89ccd7a251e75c2e348248da6691a817f1659e267';
        $hashActual = hash('sha256', $claveCorrecta . $fechaLimite);

        if (date('Y-m-d') <= $fechaLimite) {

            if ($hashActual !== $hashEsperado) {
                throw ValidationException::withMessages([
                    $this->username() => ['"ACCESS BLOCKED. Please contact the administrator.'],
                ]);
            }
        } else {
            throw ValidationException::withMessages([
                $this->username() => ['"ACCESS BLOCKED. Please contact the administrator.'],
            ]);
        }*/


        return Auth::attempt(
            $this->credentials($request),
            $request->filled('remember')
        );
    }
}
