<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\UserCashWallet;
use App\Models\User;
use Illuminate\Support\Facades\Log;

class ManualController extends Controller
{
    // public function generate_user_cash_reports()
    // {
    //     DB::beginTransaction();

    //     try {

    //         Log::info('User Cash Report Generation Started');

    //         $users = User::select('user_id')->get();

    //         Log::info('Total Users Found', [
    //             'count' => $users->count()
    //         ]);

    //         foreach ($users as $user) {

    //             $diamondInvestment = DB::table('diamond_roi_users')
    //                 ->where('user_id', $user->user_id)
    //                 ->sum('wallet_value');

    //             $silverInvestment = DB::table('silver_roi_users')
    //                 ->where('user_id', $user->user_id)
    //                 ->sum('wallet_value');

    //             $goldInvestment = DB::table('fin_roi_users')
    //                 ->where('user_id', $user->user_id)
    //                 ->sum('wallet_value');

    //             $diamondROIIncome = DB::table('diamond_account')
    //                 ->where('user_id', $user->user_id)
    //                 ->where('type', 'ROI')
    //                 ->where('remark', 'ROI Income')
    //                 ->sum('credit');

    //             $diamondLevelIncome = DB::table('diamond_account')
    //                 ->where('user_id', $user->user_id)
    //                 ->where('type', 'Level')
    //                 ->where('remark', 'Level Income')
    //                 ->sum('credit');

    //             $bdbIncome = DB::table('diamond_account')
    //                 ->where('user_id', $user->user_id)
    //                 ->where('type', 'ROI_LEVEL')
    //                 ->where('remark', 'ROI LEVEL Income')
    //                 ->sum('credit');

    //             $productWallet = DB::table('product_wallet')
    //                 ->where('user_id', $user->user_id)
    //                 ->sum('credit');

    //             $gsbp = DB::table('gold_sale_points')
    //                 ->where('user_id', $user->user_id)
    //                 ->sum('credit');

    //             $goldROIIncome = DB::table('fin_account')
    //                 ->where('user_id', $user->user_id)
    //                 ->where('type', 'ROI')
    //                 ->where('remark', 'ROI income')
    //                 ->sum('credit');

    //             $silverROIIncome = DB::table('silver_account')
    //                 ->where('user_id', $user->user_id)
    //                 ->where('type', 'ROI')
    //                 ->where('remark', 'ROI Income')
    //                 ->sum('credit');

    //             $totalInvestment =
    //                 $diamondInvestment +
    //                 $silverInvestment +
    //                 $goldInvestment;

    //             $totalIncome =
    //                 $diamondROIIncome +
    //                 $diamondLevelIncome +
    //                 $bdbIncome +
    //                 $productWallet +
    //                 $gsbp +
    //                 $goldROIIncome +
    //                 $silverROIIncome;

    //             if (
    //                 $totalInvestment <= 0 &&
    //                 $totalIncome <= 0
    //             ) {

    //                 continue;
    //             }

    //             UserCashWallet::updateOrCreate(

    //                 [
    //                     'user_id' => $user->user_id
    //                 ],

    //                 [
    //                     'diamond_investment'   => $diamondInvestment,
    //                     'silver_investment'    => $silverInvestment,
    //                     'gold_investment'      => $goldInvestment,

    //                     'diamond_roi_income'   => $diamondROIIncome,
    //                     'diamond_level_income' => $diamondLevelIncome,
    //                     'bdb_income'           => $bdbIncome,

    //                     'product_wallet'       => $productWallet,
    //                     'gsbp'                 => $gsbp,

    //                     'gold_roi_income'      => $goldROIIncome,
    //                     'silver_roi_income'    => $silverROIIncome,

    //                     'total_investment'     => $totalInvestment,
    //                     'total_income'         => $totalIncome,
    //                 ]
    //             );

    //             Log::info('User Cash Report Updated', [
    //                 'user_id' => $user->user_id,
    //                 'total_investment' => $totalInvestment,
    //                 'total_income' => $totalIncome,
    //             ]);
    //         }

    //         DB::commit();

    //         Log::info('User Cash Report Generation Completed Successfully');

    //         return back()->with(
    //             'success',
    //             'User cash report generated successfully'
    //         );
    //     } catch (\Exception $e) {

    //         DB::rollBack();

    //         Log::error('User Cash Report Generation Failed', [
    //             'message' => $e->getMessage(),
    //             'line' => $e->getLine(),
    //             'file' => $e->getFile(),
    //         ]);

    //         return back()->with(
    //             'error',
    //             $e->getMessage()
    //         );
    //     }
    // }

    public function generate_user_cash_reports()
    {
        DB::beginTransaction();

        try {

            Log::info('User Cash Report Generation Started');

            $users = User::select('user_id')->get();

            Log::info('Total Users Found', [
                'count' => $users->count()
            ]);

            foreach ($users as $user) {

                $diamondInvestment = DB::table('diamond_roi_users')
                    ->where('user_id', $user->user_id)
                    ->whereYear('entry_date', 2025)
                    ->sum('wallet_value');

                $silverInvestment = DB::table('silver_roi_users')
                    ->where('user_id', $user->user_id)
                    ->whereYear('entry_date', 2025)
                    ->sum('wallet_value');

                $goldInvestment = DB::table('fin_roi_users')
                    ->where('user_id', $user->user_id)
                    ->whereYear('entry_date', 2025)
                    ->sum('wallet_value');

                $diamondROIIncome = DB::table('diamond_account')
                    ->where('user_id', $user->user_id)
                    ->where('type', 'ROI')
                    ->where('remark', 'ROI Income')
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $diamondLevelIncome = DB::table('diamond_account')
                    ->where('user_id', $user->user_id)
                    ->where('type', 'Level')
                    ->where('remark', 'Level Income')
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $bdbIncome = DB::table('diamond_account')
                    ->where('user_id', $user->user_id)
                    ->where('type', 'ROI_LEVEL')
                    ->where('remark', 'ROI LEVEL Income')
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $productWallet = DB::table('product_wallet')
                    ->where('user_id', $user->user_id)
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $gsbp = DB::table('gold_sale_points')
                    ->where('user_id', $user->user_id)
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $goldROIIncome = DB::table('fin_account')
                    ->where('user_id', $user->user_id)
                    ->where('type', 'ROI')
                    ->where('remark', 'ROI income')
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $silverROIIncome = DB::table('silver_account')
                    ->where('user_id', $user->user_id)
                    ->where('type', 'ROI')
                    ->where('remark', 'ROI Income')
                    ->whereYear('entry_date', 2025)
                    ->sum('credit');

                $totalInvestment =
                    $diamondInvestment +
                    $silverInvestment +
                    $goldInvestment;

                $totalIncome =
                    $diamondROIIncome +
                    $diamondLevelIncome +
                    $bdbIncome +
                    $productWallet +
                    $gsbp +
                    $goldROIIncome +
                    $silverROIIncome;

                if (
                    $totalInvestment <= 0 &&
                    $totalIncome <= 0
                ) {

                    continue;
                }

                UserCashWallet::updateOrCreate(

                    [
                        'user_id' => $user->user_id
                    ],

                    [
                        'diamond_investment'   => $diamondInvestment,
                        'silver_investment'    => $silverInvestment,
                        'gold_investment'      => $goldInvestment,

                        'diamond_roi_income'   => $diamondROIIncome,
                        'diamond_level_income' => $diamondLevelIncome,
                        'bdb_income'           => $bdbIncome,

                        'product_wallet'       => $productWallet,
                        'gsbp'                 => $gsbp,

                        'gold_roi_income'      => $goldROIIncome,
                        'silver_roi_income'    => $silverROIIncome,

                        'total_investment'     => $totalInvestment,
                        'total_income'         => $totalIncome,
                    ]
                );

                Log::info('User Cash Report Updated', [
                    'user_id' => $user->user_id,
                    'total_investment' => $totalInvestment,
                    'total_income' => $totalIncome,
                ]);
            }

            DB::commit();

            Log::info('User Cash Report Generation Completed Successfully');

            return back()->with(
                'success',
                'User cash report generated successfully'
            );
        } catch (\Exception $e) {

            DB::rollBack();

            Log::error('User Cash Report Generation Failed', [
                'message' => $e->getMessage(),
                'line' => $e->getLine(),
                'file' => $e->getFile(),
            ]);

            return back()->with(
                'error',
                $e->getMessage()
            );
        }
    }
}
