<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Models\normaluser;
use Illuminate\Support\Facades\Session;
use Stevebauman\Location\Facades\Location;

class userORguest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {

        $token = $request->session()->token();
        $token = csrf_token();

        // Currency 
        $ip = $request->ip();

        if (empty(Session::get(key: 'currency'))) {
        $countryCode = Location::get()->countryCode;
        $currencyCode = $this->mapCountryToCurrency($countryCode);
        $exchangeRates = config('currencies.currencies');

        if (isset($exchangeRates[$currencyCode])) {
            Session::put('currency', $currencyCode);
        } else {
            Session::put('currency', 'EGP');
        }
        } else {
        }
        // Currency 

        if ($request->session()->has('userID') == true) {
            $userORguestSession = normaluser::select('normalUser_id')->where([

                ['normalUser_id', $request->session()->get('userID')],
                ['normalUser_type', "1"]
            ])->get()->value('normalUser_id');

            if ($userORguestSession == $request->session()->get('userID')) {
            } else {
            }
        } else {

            if (normaluser::select('normalUser_token')->where('normalUser_token', $token)->value('normalUser_token') == $token) {
                $sessionGUESTsave = normaluser::select('normalUser_id')->where('normalUser_token', $token)->value('normalUser_id');
                $request->session()->put('userID', $sessionGUESTsave);
                $request->session()->put('type', "0");
                $request->session()->put('country', null);
            } else {
                normaluser::create(
                    [
                        'normalUser_name' => "",
                        'normalUser_email' => "",
                        'normalUser_Photo' => "",
                        'normalUser_password' => "",
                        'normalUser_phone' => "",
                        'normalUser_verifyCode' => 0,
                        'normalUser_approve' => 0,
                        'normalUser_token' => $token,
                        'normalUser_type' => '0',
                        'normalUser_vendor_id' => Session::get('website_id')
                    ]
                );
                $sessionGUESTcreated = normaluser::select('normalUser_id')->where('normalUser_token', $token)->value('normalUser_id');
                $request->session()->put('userID', $sessionGUESTcreated);
                $request->session()->put('type', "0");
            }
        }



        return $next($request);
    }
    private function mapCountryToCurrency($countryCode)
    {
        $currencyMapping = [
            'US' => 'USD',
            'CA' => 'CAD',
            'GB' => 'GBP',
            'IN' => 'INR',
            'AU' => 'AUD',
            'JP' => 'JPY',
            'FR' => 'EUR',
            'DE' => 'EUR',
            // Add more mappings as needed
        ];

        return $currencyMapping[$countryCode] ?? 'USD'; // Default to USD if not found
    }
}
