<?php

namespace App\Http\Controllers;

use App\Models\LoginHistory;
use App\Models\Announce;
use App\Models\ResentActivity;
use App\Services\WhmcsServices;


class DashboardController extends Controller
{

    protected WhmcsServices $whmcsServices;
    public function __construct(WhmcsServices $whmcsServices)
    {
        $this->whmcsServices = $whmcsServices;
    }


    public function index(){
        
        $data = [];
        $user_id = auth()->user()->id;
        $login_histories = LoginHistory::where('user_id' , $user_id)->orderBy('id' , 'DESC');
        $data['announces'] = Announce::orderBy('id' , 'DESC')->get();
        $data['last_dates'] = ResentActivity::where('user_id' , $user_id)->first();


        $client = $this->getClient();
        $stats = $this->getClientStats($client['stats']);
        $data = array_merge($data, $stats);
        $data['paid_amount'] = substr($data['paid'], 1,-4) == 0 ? 1 : substr($data['paid'], 1,-4);
        $data['unpaid_amount'] = substr($data['unpaid'], 1,-4) == 0 ? 1 : substr($data['unpaid'], 1,-4);
        $data['support_amount'] = $data['support'] == 0 ? 1 : $data['support'];
        $data['orders'] = $this->getClientOrdersByClientId($client['client_id']);
        $data['last_login'] = $login_histories->where('result' , 'Success')->first();
        $data['histories'] = $login_histories->limit(10)->get();
        $data['client'] = $client;
        
     
        return view('Dashboard.index' , $data);
    }














    


    // helper functions

    protected function getClientStats($client){

        $available = $client['creditbalance'];
        $active_domain = $client['numactivedomains'];
        $active_service = $client['productsnumactive'];
        $unpaid = $client['unpaidinvoicesamount'];
        $paid = $client['paidinvoicesamount'];
        $support = $client['numtickets'];
        $active_support = $client['numactivetickets'];

        return array(
            'available'=>$available,
            'active_domain'=>$active_domain,
            'active_service'=>$active_service,
            'unpaid'=>$unpaid,
            'paid'=>$paid,
            'support'=>$support,
            'active_support'=>$active_support
        );
    }

    protected function getClientOrdersByClientId($clientId) {
        $orderIds =  $this->whmcsServices->getClientOrderIdsByClientId($clientId);
        $orders = [];

        if(count($orderIds) > 0){
            $orders[] = array(
                'amount' => 0,
                'date' => 0,
                );
        }else{
            $order_array = $this->whmcsServices->getClientOrdersByIds($orderIds);  
            foreach ($order_array as $key => $value) {
                $orders[] = array(
                    'amount' => $value['orders']['order'][0]['amount'],
                    'date' => $value['orders']['order'][0]['date'],

                );
            }
        }

        return $orders;
    }

    protected function getClient(){
        return $this->whmcsServices->getClientByEmail(auth()->user()->email);
    }

}
