<?php

use Illuminate\Support\Facades\Route;
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('/', function (Request $request) {

    
    $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.young-trade.com/api/investment-plans',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);
        
        // Check for curl errors
        if ($err) {
            \Log::error('CURL Error: ' . $err);
            $json_response = null;
        } else {
            $json_response = json_decode($response);
        }

        // Check if JSON decode was successful and response is valid
        if (!$json_response || !isset($json_response->plans)) {
            \Log::error('Invalid API response or JSON decode failed', ['response' => $response]);
            // Provide default empty values
            $json_response = (object)[
                'plans' => [],
                'investments' => [],
                'tradings' => [],
                'withdrawals' => [],
                'coins' => []
            ];
        }

        if ($request->has('ref')) {
            $ref_id = $request->ref;
            $request->session()->put('ref_id', $ref_id);
        }
    return view('pages.index')->with([
        'plans' => $json_response->plans ?? [],
        'investments' => $json_response->investments ?? [],
        'tradings' => $json_response->tradings ?? [],
        'withdrawals' => $json_response->withdrawals ?? [],
        'coins' => $json_response->coins ?? []
    ]);

    // return view('pages.index');

});

Route::get('/staking', function (Request $request) {

    $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.young-trade.com/api/investment-plans',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);
        
        // Check for curl errors
        if ($err) {
            \Log::error('CURL Error: ' . $err);
            $json_response = null;
        } else {
            $json_response = json_decode($response);
        }

        // Check if JSON decode was successful and response is valid
        if (!$json_response || !isset($json_response->plans)) {
            \Log::error('Invalid API response or JSON decode failed', ['response' => $response]);
            // Provide default empty values
            $json_response = (object)[
                'plans' => [],
                'investments' => [],
                'tradings' => [],
                'withdrawals' => [],
                'coins' => []
            ];
        }

        if ($request->has('ref')) {
            $ref_id = $request->ref;
            $request->session()->put('ref_id', $ref_id);
        }
    return view('pages.staking')->with([
        'plans' => $json_response->plans ?? [],
        'investments' => $json_response->investments ?? [],
        'tradings' => $json_response->tradings ?? [],
        'withdrawals' => $json_response->withdrawals ?? [],
        'coins' => $json_response->coins ?? []
    ]);
    // return view('pages.staking');
});

Route::get('/ido', function () {
    return view('pages.ido');
});

Route::get('/swap', function (Request $request) {
    
    $curl = curl_init();
        curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.young-trade.com/api/investment-plans',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
        ));

        $response = curl_exec($curl);
        $err = curl_error($curl);

        curl_close($curl);
        
        // Check for curl errors
        if ($err) {
            \Log::error('CURL Error: ' . $err);
            $json_response = null;
        } else {
            $json_response = json_decode($response);
        }

        // Check if JSON decode was successful and response is valid
        if (!$json_response || !isset($json_response->plans)) {
            \Log::error('Invalid API response or JSON decode failed', ['response' => $response]);
            // Provide default empty values
            $json_response = (object)[
                'plans' => [],
                'investments' => [],
                'tradings' => [],
                'withdrawals' => [],
                'coins' => []
            ];
        }

        if ($request->has('ref')) {
            $ref_id = $request->ref;
            $request->session()->put('ref_id', $ref_id);
        }
    return view('pages.swap')->with([
        'plans' => $json_response->plans ?? [],
        'investments' => $json_response->investments ?? [],
        'tradings' => $json_response->tradings ?? [],
        'withdrawals' => $json_response->withdrawals ?? [],
        'coins' => $json_response->coins ?? []
    ]);

    // return view('pages.swap');
});

Route::get('/apex', function () {
    return view('pages.apex');
});

Route::get('/contact', function () {
    return view('pages.contact-us');
});

Route::get('/terms', function () {
    return view('pages.terms-conditaions');
});