<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
//use Illuminate\Support\Facades\DB; // Import the DB facade
use DB;
// use Auth;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Storage;

class LoginController extends Controller
{

    public function formlogout(Request $request,$key)
    {    


        $ip = $request->ip();
        $ip = $request->get('client_ip');         
                     
        $fullUrl = $request->fullUrl();
       $result = file_get_contents("http://ip-api.com/php/{$ip}");
       $details = unserialize($result);
       $country = $details['country'];


        $logData = [
            'date_time' => now()->format('Y-m-d H:i:s'), 
            'uid' => $request->uid,
            'country' => $country,
            'ip_details' => $details,
            'fullUrl'=> $fullUrl,
            
        ];
    
        $logFile = 'userlogs/userlog.txt';
        $backupSizeLimit = 500 * 1024 * 1024; // 500MB
        
        // Ensure the logs directory exists
        if (!Storage::exists('userlogs')) {
            Storage::makeDirectory('userlogs');
        }


        // Check file size and backup if needed
        if (Storage::exists($logFile) && Storage::size($logFile) >= $backupSizeLimit) {
            $backupName = 'userlogs/userlog_backup_' . now()->format('Y-m-d_H-i-s') . '.txt';
            Storage::move($logFile, $backupName);
            Storage::put($logFile, ""); // Create new empty file
        }
    
        // Append log data
        Storage::append($logFile, json_encode($logData, JSON_PRETTY_PRINT));

   
       
        $request->session()->forget('text_quest');

        session(['text_quest' => '']);
        $request->session()->flush();
        Auth::logout(); 
        if($request['name'] == 'revenueshareforms')
        {
        return redirect()->route('revenueshareforms', ['key' => $key]);
        }
        if($request['name'] == 'youtubeoac')
        {
            
             return redirect()->route('youtubeoac', ['key' => $key]);

        }
        if($request['name'] == 'Contentmappingform')
        {
           
             return redirect()->route('Contentmappingform', ['key' => $key]);

        }
        if($request['name'] == 'accountclosureform')
        {
             
             return redirect()->route('accountclosureform', ['key' => $key]);

        }
        
    }

   



    
   
}



