<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Investment;
use App\Models\Withdrawal;
use App\Models\Plan;
use App\Models\InvestmentPlan;
use App\Models\Funding;
use App\Models\User;

class MigrationController extends Controller
{
    
    public function verifyCode($qty){
        $acc_num = '';
        $nums = collect([0,2,5,8,9,4,3,6,7,1]);

        $random = $nums->shuffle()->random($qty);
        foreach ($random->all() as $number) {
           $acc_num .= $number;
        }
        return $acc_num;
    }

    public function migrateAll(){
            $curl = curl_init();
            curl_setopt_array($curl, array(
            CURLOPT_URL => "https://arbitrageglobal.comunikates.com/api/load-migration",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            ));
            $response = curl_exec($curl);
            $err = curl_error($curl);


            $json_response = json_decode($response);

            // foreach ($json_response->users as $data) {
            //     $user = new User;
            //     $user->name = $data->name;
            //     $user->profile = $data->profile;
            //     $user->email = $data->email;
            //     $user->currency = 'USD';
            //     $user->country = $data->country;
            //     $user->email_verified_at = $data->email_verified_at;
            //     $user->password = $data->password;
            //     $user->phone = $data->phone;
            //     $user->referral_code = $this->verifyCode(8) ;
            //     $user->address = $data->country;
            //     $user->state = $data->country;
            //     $user->remember_token = $data->remember_token;
            //     $user->save();
            // }

            // foreach ($json_response->plans as $data) {
            //     $rate = $data->daily_roi_percent /24;
            //     $plan = new InvestmentPlan;
            //     $plan->name	 = $data->name ;
            //     $plan->amount_invested	 = $data->max_amount ;
            //     $plan->min_amount	 = $data->min_amount ;
            //     $plan->bonus_per_downline	 = $data->ref_bonus ;
            //     $plan->number_of_downlines	 = $data->ref_bonus ;
            //     $plan->total_earning	 = $data->daily_roi_percent ;
            //     $plan->amount_per_hour	 = $rate;
            //     $plan->initial_earning	 = $data->min_amount ;
            //     $plan->duration	 = $data->durations_copy;
            //     $plan->save();
            // }

            foreach ($json_response->investments as $data) {
                $investment = new Investment;
                if ($data->earning == null) {
                    $investment->earning = 0;
                }else {
                    $investment->earning = $data->earning;
                }
                
                $investment->user_id	 = $data->user_id;
                $investment->investment_plan_id	 = $data->plan_id;
                $investment->amount_invested	 = $data->amount;
                $investment->status	 = $data->status;
                $investment->duration	 = $data->duration;
                
                $investment->save();
            }


            // foreach ($json_response->withdrawals as $data) {
            //     $withdrawal = new Withdrawal;
            //     $withdrawal->user_id = $data->user_id;
            //     $withdrawal->amount = $data->amount;
            //     $withdrawal->source = $data->source;
            //     $withdrawal->address = $data->wallet_address;
            //     $withdrawal->coin_name = $data->wallet_name;
            //     $withdrawal->status = $data->status;
            //     $withdrawal->save();
            // }

            // foreach ($json_response->fundings as $data) {
            //     $funding = new Funding;
            //     $funding->user_id	 = $data->user_id;
            //     $funding->amount	 = $data->amount;
            //     $funding->status	 = $data->status;
            //     $funding->gateway	 = $data->payment_method;
            //     $funding->transaction_id = $data->walet_id;
            //     $funding->save();
            // }

            // foreach ($json_response->topups as $data) {
            //     $funding = new Funding;
            //     $funding->user_id	 = $data->user_id;
            //     $funding->amount	 = $data->amount;
            //     $funding->status	 = 'approved';
            //     $funding->gateway	 = 'funding';
            //     $funding->transaction_id = $this->verifyCode(10);
            //     $funding->save();
            // }

            // foreach ($json_response->topdowns as $data) {
            //     $funding = new Funding;
            //     $funding->user_id	 = $data->user_id;
            //     $funding->amount	 = -$data->amount;
            //     $funding->status	 = 'approved';
            //     $funding->gateway	 = 'funding';
            //     $funding->transaction_id = $this->verifyCode(10);
            //     $funding->save();
            // }


            return response()->json([
                'status' => 'success',
                'message' => 'migration completed successfully'
            ], 200);
    }
}
