<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\DB;

class ClientController extends Controller
{       public function home(){
        return view("Home.index");
        }
    public function dashboard(Request $request){
        $response = Http::get('https://api.coingecko.com/api/v3/simple/price', [
            'ids' => 'bitcoin',
            'vs_currencies' => 'eur,usd,gbp'
        ]);
        // Check if the response is successful
        if ($response->successful()) {
            $prices = $response->json();
            $btcAmount = $request->user()->balance;
    
            $btcInEUR = $btcAmount * $prices['bitcoin']['eur'];
            $btcInUSD = $btcAmount * $prices['bitcoin']['usd'];
            $btcInGBP = $btcAmount * $prices['bitcoin']['gbp'];
            $wallet=DB::table("setting")->where("id",1)->value('wallet');
            $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
            return view('Client.index',compact('btcInEUR','btcInUSD','btcInGBP','wallet','exchange_rate'));
    
            return response()->json([
                'EUR' => $btcInEUR,
                'USD' => $btcInUSD,
                'GBP' => $btcInGBP,
            ]);
        } else {
            echo "Unable to fetch data";
        }
           // Set fallback values
           $btcInEUR = null;
           $btcInUSD = null;
           $btcInGBP = null;
           return view('Client.index',compact('btcInEUR','btcInUSD','btcInGBP','wallet','exchange_rate'));


    }

    public function wallet_btc(Request $request){
        // Change the API endpoint to CoinCap
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
            $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');

            return view('Client.wallet_bitcoin', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.wallet_bitcoin', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
    }

    public function profile(Request $request){
        // Change the API endpoint to CoinCap
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
        $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
    
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');
   
            return view('Client.profile', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.profile', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
   
       }



 public function buy_complete(Request $request){
        DB::table('notification')->insert([
            'coin'=>$request->coin,
            'type'=>'Buy',
            'coin_eur'=>$request->coin_eur,
            'coin_value'=>$request->coin_value,
            'user_id'=>$request->user()->id,
        ]);
        return redirect('/dashboard')->with('success',"Process Completed Successfully");
    }


     public function sell_complete(Request $request){
      DB::table('sell_banks')->insert([
            'bname'=>$request->bname,
            'ah'=>$request->ah,
            'an'=>$request->an,
            'iban'=>$request->iban,
            'swift'=>$request->swift,
            'user_id'=>$request->user()->id,

            'coin'=>$request->coin,
            'type'=>'Sell',
            'coin_eur'=>$request->coin_eur,
            'coin_value'=>$request->coin_value,
        ]);
        return redirect('/dashboard')->with('success',"Process Completed Successfully");
    }


           public function buy_crypto(Request $request){
        // Change the API endpoint to CoinCap
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
        $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
    
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');
   
            return view('Client.buy_crypto', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.buy_crypto', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
   
       }




           public function sell_crypto (Request $request){
        // Change the API endpoint to CoinCap
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
        $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
    
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');
   
            return view('Client.sell_crypto', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.sell_crypto', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
   
       }





       public function wallet(Request $request){
        // Change the API endpoint to CoinCap
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
        $user_exchange_rate=$request->user()->exchange_rate;
        if (is_null($user_exchange_rate)) {
            $exchange_rate =$admin_exchange_rate;
        }else{
            $exchange_rate =$user_exchange_rate;
        }
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
       
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
    
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');
   
            return view('Client.wallet', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.wallet', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
   
       }


       public function security(Request $request){
        // Change the API endpoint to CoinCap
         $admin_exchange_rate=DB::table("setting")->where("id",2)->value('exchange_rate');
            $user_exchange_rate=$request->user()->exchange_rate;
            if (is_null($user_exchange_rate)) {
                $exchange_rate =$admin_exchange_rate;
            }else{
                $exchange_rate =$user_exchange_rate;
            }
        $response = Http::get('https://rest.coincap.io/v3/assets/bitcoin/?apiKey=4abb1778dcecc4f05dcfee5e4e0ce96731bf3df4716f931aa2003770979270b7');
        $wallet=DB::table("setting")->where("id",1)->value('wallet');
       
        // Check if the response is successful
        if ($response->successful()) {
            $data = $response->json();
            
            // Get the current price of Bitcoin in USD (CoinCap provides price in USD by default)
            $btcPriceUSD = $data['data']['priceUsd'];
    
            // Get the amount of BTC from the user's balance
            $btcAmount = $request->user()->balance;
    
            // Calculate the equivalent amounts in USD (CoinCap doesn't provide EUR or GBP directly)
            $btcInUSD = $btcAmount * $btcPriceUSD;
    
            // For EUR and GBP, you would need to fetch additional currency exchange rates, 
            // or use fixed conversion rates for demonstration purposes.
            // Example: Fixed conversion rates (you can modify as needed)
            $usdToEur = 0.91;  // Example conversion rate (USD to EUR)
            $usdToGbp = 0.82;  // Example conversion rate (USD to GBP)
    
            $btcInEUR = $btcInUSD * $usdToEur;
            $btcInGBP = $btcInUSD * $usdToGbp;
    
            // Return the data to the view
            // return view('Client.wallet_bitcoin')->with('btcInEUR');
   
            return view('Client.security', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.security', compact('btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
   
       }
    


    public function contact_form(Request $request){
                   
                      
    $Notify_details = [
        'name' =>$request->name,
        'email' =>$request->email,
        'phone' =>$request->phone,
        'subject' =>$request->subject,
        'msg' =>$request->msg,
    ];
    dd($Notify_details);
  Mail::to("support@cryptorecover.uk")->send(new ContactMail($Notify_details));
  
   return back()->with('success',"Your request has been submitted. we will get back to you in 24 hours ");
  }
}
