<?php

namespace App\Http\Controllers\In_Charge;

use App\Http\Controllers\Controller;
use App\Models\Complaint;
use App\Models\Service;
use App\Models\Sub_In_Charge;
use Exception;

class DashboardController extends Controller
{
    public function index()
    {
        try {

            $incharge = InCharge_Data();

            // First Card
            $params['ServiceCount'] = Service::where('department_id', $incharge['department_id'])->count();
            $params['SubInChargeCount'] = Sub_In_Charge::where('department_id', $incharge['department_id'])->where('service_id', '!=', "")->count();
            $params['InActiveSubInChargeCount'] = Sub_In_Charge::where('department_id', $incharge['department_id'])->where('service_id', '=', "")->count();
            $params['TotalComplaints'] = Complaint::where('department_id', $incharge['department_id'])->count();

            $params['OpenComplaints'] = Complaint::where('department_id', $incharge['department_id'])->where('status', 1)->count();
            $params['InProgressComplaints'] = Complaint::where('department_id', $incharge['department_id'])->where('status', 2)->count();
            $params['ArchivedComplaints'] = Complaint::where('department_id', $incharge['department_id'])->where('status', 3)->count();
            $params['CompletComplaints'] = Complaint::where('department_id', $incharge['department_id'])->where('status', 4)->count();

            // Initialize data arrays for yearly and monthly complaint statistics
            $open_data = [];
            $inprogress_data = [];
            $archived_data = [];
            $complete_data = [];
            $open_month = [];
            $inprogress_month = [];
            $archived_month = [];
            $complete_month = [];

            $d = date('t', mktime(0, 0, 0, date('m'), 1, date('Y'))); // Days in the current month

            // Yearly Complaint Statistics
            for ($i = 1; $i <= 12; $i++) {
                $open = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 1)->count();
                $inprogress = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 2)->count();
                $archived = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 3)->count();
                $complete = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 4)->count();

                $open_data['sum'][] = (int) $open;
                $inprogress_data['sum'][] = (int) $inprogress;
                $archived_data['sum'][] = (int) $archived;
                $complete_data['sum'][] = (int) $complete;
            }

            // Monthly Complaint Statistics
            for ($i = 1; $i <= $d; $i++) {
                $open = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', date('m'))->whereDay('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 1)->count();
                $inprogress = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', date('m'))->whereDay('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 2)->count();
                $archived = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', date('m'))->whereDay('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 3)->count();
                $complete = Complaint::whereYear('created_at', date('Y'))->whereMonth('created_at', date('m'))->whereDay('created_at', $i)->where('department_id', $incharge['department_id'])->where('status', 4)->count();

                $open_month['sum'][] = (int) $open;
                $inprogress_month['sum'][] = (int) $inprogress;
                $archived_month['sum'][] = (int) $archived;
                $complete_month['sum'][] = (int) $complete;
            }

            // Encode data for JSON response or view usage
            $params['open_year'] = json_encode($open_data);
            $params['open_month'] = json_encode($open_month);
            $params['inprogress_year'] = json_encode($inprogress_data);
            $params['inprogress_month'] = json_encode($inprogress_month);
            $params['archived_year'] = json_encode($archived_data);
            $params['archived_month'] = json_encode($archived_month);
            $params['complete_year'] = json_encode($complete_data);
            $params['complete_month'] = json_encode($complete_month);


            return view('incharge.dashboard.dashboard', $params);
        } catch (Exception $e) {
            return response()->json(array('status' => 400, 'errors' => $e->getMessage()));
        }
    }
}
