<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\ClientController;
use App\Http\Controllers\Auth\AuthController;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', [ClientController::class, 'home']);



Route::get('/crypto-price', function (\Illuminate\Http\Request $request) {
    $symbol = strtoupper($request->get('symbol', 'BTC'));

    // Example API (CoinGecko)
 $response = Http::get("https://api.coingecko.com/api/v3/simple/price", [
    'ids' => $symbol,           // e.g. 'bitcoin'
    'vs_currencies' => 'eur'    // 👈 change usd → eur
]);

if ($response->successful()) {
    return response()->json($response->json());
}


    return response()->json(['error' => 'Unable to fetch price'], 500);
});
Route::get('/crypto-next', function (Request $request) {
    $user=$request->user();
    $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');
    $symbol = $request->get('symbol', 'bitcoin');
     $amount = $request->get('amount', 0);
    $bank_details=DB::table("setting")->where("id",3)->get();

     if($amount>$btcInUSD){
        return back()->with('warning','Your Balance is low!');
     }
            return view('Client.crypto_buy_next', compact('amount','btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate','symbol','bank_details'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.crypto_buy_next', compact('bank_details','btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
    $symbol = $request->get('symbol', 'bitcoin');
    
    return view('client.crypto_buy_next', compact('symbol'));
});


Route::get('/crypto-next-sell', function (Request $request) {
    $user=$request->user();
    $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');
    $symbol = $request->get('symbol', 'bitcoin');
     $amount = $request->get('amount', 0);
    $bank_details=DB::table("setting")->where("id",3)->get();

     if($amount>$btcInUSD){
        return back()->with('warning','Your Balance is low!');
     }
            return view('Client.crypto_sell_next', compact('amount','btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate','symbol','bank_details'));
    
        } else {
            echo "Unable to fetch data";
            // Set fallback values
            $btcInEUR = null;
            $btcInUSD = null;
            $btcInGBP = null;
            return view('Client.crypto_sell_next', compact('bank_details','btcInEUR', 'btcInUSD', 'btcInGBP','wallet','exchange_rate'));
        }
    $symbol = $request->get('symbol', 'bitcoin');
    
    return view('client.crypto_sell_next', compact('symbol'));
});


Route::get('/dashboard', [ClientController::class, 'dashboard'])->middleware('auth');
Route::get('/buy-crypto', [ClientController::class, 'buy_crypto'])->middleware('auth');
Route::get('/sell-crypto', [ClientController::class, 'sell_crypto'])->middleware('auth');
Route::get('/wallet-bitcoin', [ClientController::class, 'wallet_btc'])->middleware('auth');
Route::get('/profile', [ClientController::class, 'profile'])->middleware('auth');
Route::get('/wallets', [ClientController::class, 'wallet'])->middleware('auth');
Route::get('/security', [ClientController::class, 'security'])->middleware('auth');
Route::post('/contact_form', [ClientController::class, 'contact_form'])->name('contact_form')->middleware('auth');














Route::post('/buy_complete', [ClientController::class, 'buy_complete'])->name('buy_complete');



Route::post('/sell_complete', [ClientController::class, 'sell_complete'])->name('sell_complete');







Route::get('/login', [AuthController::class, 'index'])->name('login');
Route::post('/login-post', [AuthController::class, 'postLogin'])->name('postLogin');
Route::get('logout', [AuthController::class, 'logout'])->name('logout');
Route::post('change_pw', [AuthController::class, 'change_pw'])->name('change_pw');
Route::get('/forget-password', [AuthController::class, 'showForgetPasswordForm'])->name('showForgetPasswordForm');
Route::post('/submitForgetPasswordForm', [AuthController::class, 'submitForgetPasswordForm'])->name('submitForgetPasswordForm');
Route::get('reset-password/{token}', [AuthController::class, 'showResetPasswordForm'])->name('reset.password.get');
Route::post('/reset-password', [AuthController::class, 'submitResetPasswordForm'])->name('reset.password.post');




Route::get('/admin', [AdminController::class, 'admin'])->middleware(['can:isAdmin','auth']);
Route::get('/all-users', [AdminController::class, 'all'])->middleware(['can:isAdmin','auth']);
Route::get('/edit-user/{id}', [AdminController::class, 'edit_user'])->middleware(['can:isAdmin','auth']);
Route::post('/update-user-post', [AdminController::class, 'edit_user_post'])->name("edit_user_post")->middleware(['can:isAdmin','auth']);
Route::get('/delete-user/{id}', [AdminController::class, 'delete_user'])->middleware(['can:isAdmin','auth']);
Route::get('/new-user', [AdminController::class, 'new_user'])->middleware(['can:isAdmin','auth']);
Route::post('/create-user-post', [AdminController::class, 'create_user_post'])->name('create_user_post')->middleware(['can:isAdmin','auth']);
Route::get('/admin-wallet-address', [AdminController::class, 'admin_wallet'])->middleware(['can:isAdmin','auth']);
Route::post('/create_wallet_post', [AdminController::class, 'create_wallet_post'])->name('create_wallet_post')->middleware(['can:isAdmin','auth']);

Route::get('/admin-exchange-rate', [AdminController::class, 'exchange_rate'])->middleware(['can:isAdmin','auth']);


Route::get('/buy-setting', [AdminController::class, 'buy_setting'])->middleware(['can:isAdmin','auth']);




Route::post('/create_exchange_rate_post', [AdminController::class, 'exchange_rate_post'])->name('exchange_rate_post')->middleware(['can:isAdmin','auth']);






Route::post('/add_bank_buy', [AdminController::class, 'add_bank_buy'])->name('add_bank_buy')->middleware(['can:isAdmin','auth']);






Route::get('/buy-transactions', [AdminController::class, 'buy'])->middleware(['can:isAdmin','auth']);
Route::get('/sell-transactions', [AdminController::class, 'sell'])->middleware(['can:isAdmin','auth']);
