<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Common;
use App\Models\Complaint;
use App\Models\Department;
use Exception;
use Illuminate\Http\Request;

class AnalyticsReportController extends Controller
{
    public $common;
    public function __construct()
    {
        $this->common = new Common;
    }

    public function index(Request $request)
    {
        try {
            $params = [];
            $params['department'] = Department::where('status', 1)->get();

            $department_id = $request->department_id;
            // $params['department_id'] = $department_id ?? 0;

            $params['dep_performance'] = [];
            $params['yearly_request_trend'] = [];
            $params['monthly_request_trend'] = [];
            $d = date('t', mktime(0, 0, 0, date('m'), 1, date('Y')));

            // Base query
            $complaint = Complaint::query();
            if ($department_id != 0) {
                $complaint->where('department_id', $department_id);
            }
            // Department Performance
            for ($month = 1; $month <= 12; $month++) {
                $complete_complaint = (clone $complaint)
                    ->whereYear('created_at', date('Y'))
                    ->whereMonth('created_at', $month)
                    ->where('status', 4)
                    ->count();

                $total_complaint = (clone $complaint)
                    ->whereYear('created_at', date('Y'))
                    ->whereMonth('created_at', $month)
                    ->count();

                // Avoid division by zero
                $percentage = $total_complaint > 0 ? round(($complete_complaint / $total_complaint) * 100, 2) : 0;

                // Store result with month name (or number)
                $params['dep_performance'][] = [
                    'performance' => $percentage,
                    'completed' => $complete_complaint,
                    'total' => $total_complaint
                ];
            }

            // Monthly complaint trends
            for ($month = 1; $month <= 12; $month++) {
                $Sum = (clone $complaint)
                    ->whereYear('created_at', date('Y'))
                    ->whereMonth('created_at', $month)
                    ->count();
                $params['yearly_request_trend'][] = $Sum;
            }
            for ($i = 1; $i <= $d; $i++) {
                $Sum = (clone $complaint)
                    ->whereYear('created_at', date('Y'))
                    ->whereMonth('created_at', date('m'))
                    ->whereDay('created_at', $i)
                    ->count();
                $params['monthly_request_trend'][] = $Sum;
            }

            if ($request->ajax()) {
                return response()->json($params);
            }
            return view('admin.analytics_report.index', $params);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
    public function topUsersByOrders(Request $request)
    {
        try {
            $params = [];
            return view('admin.sales_report.index', $params);
        } catch (\Exception $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}
