<?php

namespace App\Http\Controllers;

use App\Models\BkashTransaction;
use Illuminate\Http\Request;
use Session;
use Illuminate\Support\Facades\DB;

class BkashController extends Controller
{
    public function index()
    {
        if(Session::get('customer_id')){
            $customer = DB::table('tbl_agent')->where('ag_id', Session::get('customer_id'))->first();
            $billinng = DB::table('customer_billing')->where('agid', Session::get('customer_id'))->first();
            $accountStatement = DB::table('tbl_agent')->join('tbl_account', 'tbl_agent.ag_id', '=', 'tbl_account.agent_id')->where('tbl_agent.ag_id', Session::get('customer_id'))->select(['tbl_agent.ag_id', 'tbl_account.acc_amount', 'tbl_account.entry_date', 'tbl_account.acc_description'])->orderBy('tbl_account.acc_id','desc')->limit(10)->get();
            return view('home.index', compact('customer', 'accountStatement', 'billinng'));
        }elseif(Session::get('reseller_id')){
            $reseller = DB::table('_reseller_createuser')->where('UserId', Session::get('reseller_id'))->first();
            $accountStatement = DB::table('_reseller_createuser')
                ->join('re_tbl_reseller_account', '_reseller_createuser.UserId', '=', 're_tbl_reseller_account.reseller_id')
                ->where('_reseller_createuser.UserId', Session::get('reseller_id'))
                ->where('re_tbl_reseller_account.type', 1)
                ->select([
                    '_reseller_createuser.UserId as id',
                    're_tbl_reseller_account.amount as amount',
                    're_tbl_reseller_account.entry_date as date',
                    're_tbl_reseller_account.description as description'
                ])
                ->orderBy('re_tbl_reseller_account.id', 'desc')
                ->limit(10)
                ->get();
            return view('home.reseller-index', compact('reseller','accountStatement'));
        }elseif(Session::get('reseller_customer_id')){
            $resellerCustomer = DB::table('re_tbl_agent')->where('ag_id', Session::get('reseller_customer_id'))->first();
            return view('home.re-customer-index', compact('resellerCustomer'));
        }
    }

    public function login()
    {
        return view('auth.login');
    }

    public function loginCheck(Request $request)
    {
        $customerId = $request->customer_id;
    
        $customer = DB::table('tbl_agent')->where('cus_id', $customerId)->first();
        if ($customer) {
            Session::put('customer_id', $customer->ag_id);
            Session::put('name', $customer->ag_name);
            return redirect('/');
        }
    
        $reseller = DB::table('_reseller_createuser')->where('UserName', $customerId)->first();
        if ($reseller) {
            Session::put('reseller_id', $reseller->UserId);
            Session::put('reseller_name', $reseller->FullName);
            return redirect('/');
        }
    
        $resellerCustomer = DB::table('re_tbl_agent')->where('cus_id', $customerId)->first();
        if ($resellerCustomer) {
            Session::put('reseller_customer_id', $resellerCustomer->ag_id);
            Session::put('reseller_customer_name', $resellerCustomer->ag_name);
            return redirect('/');
        }
    
        return redirect('/login')->with('message', 'Invalid Customer ID or IP');
    }
    public function logout()
    {
        Session::forget('customer_id');
        Session::forget('name');
        Session::forget('reseller_id');
        Session::forget('reseller_name');
        Session::forget('reseller_customer_id');
        Session::forget('reseller_customer_name');
        return redirect('/login');
    }

    public function paymentHistory()
    {
        // $customer = DB::table('tbl_agent')->where('cus_id',Session::get('customer_id'))->first();
        // $accountStatement = DB::table('tbl_account')->where('cus_id',Session::get('customer_id'))->get();
        $accountStatement = DB::table('tbl_agent')->join('tbl_account', 'tbl_agent.ag_id', '=', 'tbl_account.agent_id')->where('tbl_agent.ag_id', Session::get('customer_id'))->select(['tbl_agent.ag_id', 'tbl_account.acc_amount', 'tbl_account.entry_date', 'tbl_account.acc_description'])->orderBy('tbl_account.acc_id','desc')->get();
        // dd($accountStatement);
        return view('home.payment-history', compact('accountStatement'));
    }
    public function bkashPaymentHistory()
    {
        $bkashTransactions = BkashTransaction::where('customer_id', Session::get('customer_id'))->get();
        return view('home.bkash-payment-history', compact('bkashTransactions'));
    }
    public function setting()
    {
        $customer = DB::table('tbl_agent')->where('ag_id', Session::get('customer_id'))->first();
        return view('home.setting', compact('customer'));
    }
}
