<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\SiteDetail;
use App\Models\Deposit;
use App\Models\Investment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Models\CompanyDetail;
use App\Mail\StoreMail;
use App\Mail\AiMail;
use App\Mail\WithdrawalMail;
use App\Mail\VerifyTwoFaMail;

class MailController extends Controller
{
    
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function sendEmail(Request $request)
    { 
        $users = $request->users;
        $data = [
            'subject' => $request->subject,
            'message' => $request->message,
            'users' => $request->users,
            'type' => 'spam'
        ];
        
       
        foreach($users as $key => $val){
            // $message = (new SpamMail($data))
            //     ->onQueue('emails');
            // Mail::to($val)->queue($message);
            
            Mail::to($val)->send(new SpamMail($data));
        }
        
        return redirect()->back()->with('message', 'Email Sent Successfully!');
    }
    
    
    public function storeSetup($storename, $first_name, $email)
    {
        $mngrs = [
            [
                'name' => 'Alex Kolmo',
                'email' => 'alex.kolmo@ge-as.support',
            ],
            [
                'name' => 'Andrey Levitas',
                'email' => 'andrey.levitas@ge-as.support',
            ],
            [
                'name' => 'Douglas Leon',
                'email' => 'douglas.leon@ge-as.support',
            ],
            [
                'name' => 'Dulce Jacqueline',
                'email' => 'dulce.jaqueline@ge-as.support',
            ],
            [
                'name' => 'Jacob Raggie',
                'email' => 'jacob.reggie@ge-as.support',
            ],
            [
                'name' => 'Kelvin Pakman',
                'email' => 'kelvin.pakman@ge-as.support',
            ],
            [
                'name' => 'Liam Cameron',
                'email' => 'liam.cameron@ge-as.support',
            ],
            [
                'name' => 'Rachel Rodrigue',
                'email' => 'rachel.rodrigue@ge-as.support',
            ],
            [
                'name' => 'Rich Jeffery',
                'email' => 'rich.jeffery@ge-as.support',
            ],
        ];
        
        $selectedMngr = collect($mngrs)->random();
        $is_kyc_promo = SiteDetail::where('title', 'is_kyc_promo')->first();
        
        $data = [
            'subject' => 'Your C2C Store Has Been Setup!',
            'first_name' => $first_name,
            'email' => $email,
            'store' => $storename,
            'promo' => $is_kyc_promo->description,
            'manager' => $selectedMngr,
        ];
        
        // update & set store mngr
        if($is_kyc_promo->description == 1) {
            User::where('email', $email)->update([
                'store_mngr' => $selectedMngr
            ]);
        }
        
        // $message = (new StoreMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new StoreMail($data));
    }
    
    public function twoFaMail($token, $email)
    {
        $data = [
            'subject' => 'Your 2FA Authentication Token',
            'token' => $token,
        ];
       
        // $message = (new VerifyTwoFaMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new VerifyTwoFaMail($data));
    }

    public function aiPurchase($storename, $first_name, $email, $amount, $plan_name, $description, $min, $max)
    {
        $data = [
            'subject' => 'AI Bot Plan Purchase Successful',
            'first_name' => $first_name,
            'email' => $email,
            'store' => $storename,
            'amount' => $amount,
            'plan_name' => $plan_name,
            'description' => $description,
            'min' => $min,
            'max' => $max,
        ];
        
        // $message = (new AiMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new AiMail($data));
    }

    public function mainWithdrawApprove($storename, $first_name, $email, $amount, $balance)
    {
        $data = [
            'subject' => 'Withdrawal Request Successful',
            'first_name' => $first_name,
            'email' => $email,
            'store' => $storename,
            'amount' => $amount,
            'type' => 'Main Wallet',
            'status' => 'approve',
            'balance' => $balance,
            'date' => now(),
        ];
        
        // $message = (new WithdrawalMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new WithdrawalMail($data));
    }

    public function rebateWithdrawApprove($storename, $first_name, $email, $amount, $balance)
    {
        $data = [
            'subject' => 'Withdrawal Request Successful',
            'first_name' => $first_name,
            'email' => $email,
            'store' => $storename,
            'amount' => $amount,
            'type' => 'Rebate Bonus Wallet',
            'status' => 'approve',
            'balance' => $balance,
            'date' => now(),
        ];
        
        // $message = (new WithdrawalMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new WithdrawalMail($data));
    }

    public function withdrawDecline($storename, $first_name, $email, $amount, $type)
    {
        $data = [
            'subject' => 'Withdrawal Request Declined',
            'first_name' => $first_name,
            'email' => $email,
            'store' => $storename,
            'amount' => $amount,
            'type' => $type == 'main' ? 'Main Wallet' : 'Rebate Bonus Wallet',
            'status' => 'decline',
            'date' => now(),
        ];
        
        // $message = (new WithdrawalMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new WithdrawalMail($data));
    }
    
    
    public function approve($ref_id, $user_name, $email)
    {
        $data = [
            'subject' => 'Order Request Status',
            'first_name' => $user_name,
            'ref_id' => $ref_id,
            'type' => 'approve'
        ];
       
        // $message = (new SpamMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new SpamMail($data));
    }
    
    
    public function decline($ref_id, $user_name, $email)
    {
        $data = [
            'subject' => 'Order Request Status',
            'first_name' => $user_name,
            'ref_id' => $ref_id,
            'type' => 'decline'
        ];
       
        // $message = (new SpamMail($data))
        //     ->onQueue('emails');
        // Mail::to($email)->queue($message);
        
        Mail::to($email)->send(new SpamMail($data));
    }
    
    
     public function withdraw($first_name, $last_name, $email, $amount, $date)
    {
        $data = [
            'subject' => 'Withdrawal Request',
            'first_name' => $first_name,
            'last_name' => $last_name,
            'amount' => $amount,
            'email' => $email,
            'date' => $date,
            'type' => 'withdraw'
        ];
        
        
        $company_email = CompanyDetail::where('title', 'company_email')->first();
       
        // $message = (new SpamMail($data))
        //     ->onQueue('emails');
        // Mail::to($company_email->description)->queue($message);
        
        Mail::to($company_email->description)->send(new SpamMail($data));
    }

    public function request_approve($user_name, $email, $description)
    {
        $data = [
            'subject' => 'Your Deposit Has Been Approved',
            'message' => $description,
            'first_name' => $user_name,
            'type' => 'request_approve'
        ];
       
        // $message = (new SpamMail($data))
        //     ->onQueue('emails');
        // Mail::to($email->description)->queue($message); 
        
        Mail::to($email->description)->send(new SpamMail($data)); 
    }

    public function request_decline(Request $request, Deposit $deposit){
        
        $update = Deposit::where('id', $deposit->id)->update(['status' => 2]);

        $investment = Investment::where('deposit_id', $deposit->id)->delete();
        
        //get user email
        $user = User::where('id', $request->user_id)->first();
        
        $data = [
            'subject' => 'Deposit Declined',
            'message' => $request->description,
            'first_name' => $user->first_name,
            'type' => 'dep_decline'
        ];
        
        //update message
        SiteDetail::where('title', 'decline')->update(['description' => $request->description]);
        
        //send mail
        // $message = (new SpamMail($data))
        //     ->onQueue('emails');
        // Mail::to($user->email)->queue($message); 
        
        Mail::to($user->email)->send(new SpamMail($data)); 

        //send sms
        $message = $request->description;

        $mobile = $user->mobile;

        $sender = 'GLO NG';

        $client = new \GuzzleHttp\Client();

        $response = $client->post('http://customer.smsprovider.com.ng/api/?username=halebirdpoultry@gmail.com&password=Mybulksmsacct1?&message='.$message.'&sender='.$sender.'&mobiles='.$mobile);

        $body = json_decode($response->getBody());

        //if updated
        if($update){
            return redirect()->route('admin.requests')->with('message', 'Deposit Declined!');
        }else{
            return redirect()->back()->with('error', 'An error occurred');
        }
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

}
