<?php

namespace App\Controllers;

use CodeIgniter\API\ResponseTrait;
use CodeIgniter\HTTP\ResponseInterface;

class BkoDash extends BaseController
{
    use ResponseTrait;
    // protected $db;

    var $request;

    function __construct()
    {
        $this->request = \Config\Services::request();
    }

    public function bkoDashHome() :ResponseInterface
    {
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
//        $query = $this->db->query("SELECT c.firstname, c.lastname, a.uid,a.loan_amount,
//                                   a.payment_month,a.sales_agent,a.gender,
//                                   a.marital_status,a.email,a.address,
//                                   a.state,a.country,a.status,a.added,
//                                   a.updated
//                                   FROM applications a
//                                   LEFT JOIN customers c ON c.uid = a.customer_uid  LIMIT 10");

        $dash_data["active_loans"] = $this->applicationCount(5)["app_count"];;
        $dash_data["applications"] = $this->applicationCount(1)["app_count"];
        $dash_data["today_application"] = 0;

        $dash_data["curr_month"] = date('F');
        $dash_data["curr_application_amount"] = $this->monthApplicationCount()["amount"];
        $dash_data["curr_application_percentage"] = "0";
        $dash_data["curr_application_direction"] = "up";


        $dash_data["recent_applications"] = [];

        $dash_data["ready_loans"]    = $this->applicationCount(2)["amount"];
        $dash_data["verified_loans"] = $this->applicationCount(4)["amount"];
        $dash_data["approved_loans"] = $this->applicationCount(5)["amount"];


        $query = $this->db->query("SELECT c.firstname AS firstname, c.lastname AS lastname, a.uid,a.loan_amount,
                                   a.payment_month,a.sales_agent,a.gender,
                                   a.marital_status,a.email,a.address,
                                   a.state,a.country,a.status,a.added,
                                   a.updated 
                                   FROM applications a 
                                   LEFT JOIN customers c ON c.uid = a.customer_uid  LIMIT 10");
        $row   = $query->getResult('array');

        $q2 = "SELECT * FROM bvn_checks ORDER BY id DESC LIMIT 10";
        $query2 = $this->db->query($q2);
        $row2   = $query2->getResult('array');

        $data = [
            'call_return' => '100',
            'dash_data' => $dash_data,
            'recent_applications' =>  $row,
            'recent_bvn' =>  $row2
        ];
        return $this->respond($data, 200);
        //  return $this->respond([], 200);
    }


    private function monthApplicationCount(){
        //$status_result = 0;
        $mysqlA = "select count(id) AS app_count, sum(loan_amount) AS amount  from applications WHERE added BETWEEN '2024-09-01' AND now()";
        $query2 = $this->db->query($mysqlA);
        $row2   = $query2->getResult('array');
        if ($row2 && count($row2)> 0  && $row2[0]["app_count"] > 0 ){
            return  $row2[0];
        }
        return ["app_count"=> 0, "amount"=> 0 ];
    }
    private function applicationCount($applicationStatus){
        //$status_result = 0;
        $mysqlA = "select count(id) AS app_count, sum(loan_amount) AS amount from applications WHERE status= ".$applicationStatus;
        $query2 = $this->db->query($mysqlA);
        $row2   = $query2->getResult('array');
        if ($row2 && count($row2)> 0 ){
            return  $row2[0];
        }
        return ["app_count"=> 0, "amount"=> 0 ];
    }
    public function pendingApplication() :ResponseInterface
    {
        //SELECT uid,loan_amount,payment_month,sales_agent,gender,marital_status,email,address,state,country,status,added,updated FROM applications;
        $query = $this->db->query("SELECT c.firstname, c.lastname, a.uid,a.loan_amount,
                                   a.payment_month,a.sales_agent,a.gender,
                                   a.marital_status,a.email,a.address,
                                   a.state,a.country,a.status,a.added,
                                   a.updated 
                                   FROM applications a 
                                   LEFT JOIN customers c ON c.uid = a.customer_uid ");
        $row   = $query->getResult('array');
        $data = [
            'call_return' => '100',
            'records' =>  $row
        ];
        return $this->respond($data, 200);
        //  return $this->respond([], 200);
    }


}