<?php

namespace App\Console\Commands\Payouts;

use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
use Yajny\EventsYj\Events\RecalculateBalance;
use Yajny\MailYj\Mails\NewContactEmail;
use Yajny\MailYj\Mails\PayoutsValidationStep1;
use Yajny\MailYj\Mails\PayoutsValidationStep2;
use Yajny\Member\Models\BalanceTransaction;
use Yajny\Member\Models\Sale;
use Yajny\Member\Models\UserNewBonus;
use Yajny\Member\Models\UserPayouts;
use Yajny\Member\Models\UserSales;

class PayoutVerificationStep2 extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'payout:step2 {type=user}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'send email for team to validate payouts';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $type = $this->argument('type');
        $total_amount=0;
        $startDate = Carbon::now()->subDays(30);
        if ($type=="user"){
            $payouts = UserPayouts::where('user_payouts.status', 'approved_manually')
                ->where('user_payouts.created_at', '>=', $startDate)
                ->join('users', 'user_payouts.user_id', '=', 'users.user_id')
                ->where('users.is_merchant', false)
                ->get();
            $type="USERS";
            $total_payouts=count($payouts);

            foreach ($payouts as $payout){
                $this->output->write($payout->id,true);

                $total_amount+=$payout->amount;
            }
            Mail::to(["adnen.ettayeb@yajny.com","ahmed.gamal@arabyads.com"])->locale("en")->queue(new PayoutsValidationStep2($total_payouts,$total_amount,$payouts,$type));

        }else{
            $payouts = UserPayouts::where('user_payouts.status', 'approved_manually')
                ->where('user_payouts.created_at', '>=', $startDate)
                ->join('users', 'user_payouts.user_id', '=', 'users.user_id')
                ->where('users.is_merchant', true)
                ->get();
            $type="MERCHANTS";
            $total_payouts=count($payouts);

            foreach ($payouts as $payout){

                $total_amount+=$payout->amount;
            }

            Mail::to(["ahmed.yassien@yajny.com","mahmoud.hamada@yajny.com","karimabbas@yajny.com","adnen.ettayeb@yajny.com"])->locale("en")->queue(new PayoutsValidationStep2($total_payouts,$total_amount,$payouts,$type));

        }



    }
}
