<?php

namespace App\Imports;

use App\Http\Controllers\PaymentController;
use App\Models\Balance;
use App\Models\Billpayment;
use App\Models\Client;
use App\Models\Clientsinfo;
use App\Models\Payment;
use App\Models\Pop;
use App\Models\ReselleBalanceLogReport;
use App\Models\ResellerRechargeReport;
use App\Models\SubResellerBalanceLogReport;
use App\Services\ResellerClientOlineRechargeService;
use Carbon\Carbon;
use Exception;
use Maatwebsite\Excel\Concerns\Importable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Concerns\ToCollection;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class OnlinePaymentRechargeInReseller implements ToCollection
{
    use Importable;

    public function collection(Collection $rows)
    {
        // userid wise info update

        $notRecharge = [];
        $amountTotal = 0;
        // foreach ($rows as $row) {
        //     $client = Client::with('pop','clientsinfo')->find($row[4]);
        //     $amount = $row[2];
        //     $trx_id = $row[8];

        //     $payment_method = $row[9];

        //     $user_id = $row[5];

        //     $reseller_id = $client->pop->reseller_id;

        //     $checkResellerRecharge = ResellerRechargeReport::where('txn_id', 'like', "%{$trx_id}%")->count();
        //     $phpDate = Date::excelToDateTimeObject($row[6]);


        //     // dd(Carbon::parse($phpDate)->format('Y-m-d H:i:s'),$row[6]);

        //     if($payment_method != "cash"){

        //         if($checkResellerRecharge > 0){

        //         }else{
        //             if($amount > 0 ){


        //                 $resellerClientOlineRechargeService = new ResellerClientOlineRechargeService();
        //                 $resellerClientOlineRechargeService->addOrUpdateBalanceResellerOrPop("reseller",$reseller_id,$amount);
        //                 $after_amount = Balance::where('type', "reseller")
        //                         ->where('type_id', $reseller_id)
        //                         ->first()->amount;

        //                 $actions = 'Manager recharge received from online client by ' . $client->name .' '. $payment_method. ' from '. $client->clientsinfo->clients_name ;

        //                 ResellerRechargeReport::create([
        //                     'reseller_id'                       => $reseller_id,
        //                     'amount'                            => $amount,
        //                     'after_recharge_balance'            => $after_amount,
        //                     'remarks'                           => $actions ?? '',
        //                     'user_id'                           => $user_id,
        //                     'income_id'                         => null,
        //                     'paid_amount'                       => $amount,
        //                     'payment_type'                      => "online",
        //                     'payment_gateway'                   => $payment_method,
        //                     'payment_gateway_transaction_id'    => null,
        //                     'txn_id'                            => $trx_id,
        //                     'created_at'                        =>Carbon::parse($phpDate)->format('Y-m-d H:i:s')
        //                 ]);
        //             }
        //         }
        //     }




        // }
        // dd("comes");
        $count = 0;
        foreach ($rows as $row) {
            if ($count > 0) {
                $subResellerBalanceLog = SubResellerBalanceLogReport::find($row[0]);
                // dd($subResellerBalanceLog);

                if ($subResellerBalanceLog) {
                    if ($row[5] == $subResellerBalanceLog->amount) {

                        $subResellerBalanceLog->delete();
                    } else {
                        dd("Reseller Balance Log Report amount not match: " . $row[0], $row[6], $subResellerBalanceLog->amount);
                    }
                } else {
                    dd("Reseller Balance Log Report not found for ID: " . $row[0], $subResellerBalanceLog);
                }
            }
            $count++;
        }
    }
}
