<?php

namespace App\Http\Controllers\HeadquartersDashboard;

use App\Facades\DataTable\DataTableFacade;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class CumulativeSupervisorPerformanceStatsController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = DB::table('supervisor_performances')->selectRaw("
                province_id,
                province_name,
                SUM(calls) AS total,
                SUM(reviews) AS reviews,
                SUM(good) AS good,
                SUM(bad) AS bad
            ")
            ->groupBy('province_id', 'province_name')
            ->unionAll(DB::table('supervisor_performances')->selectRaw("
                0 as province_id,
                'کشوری' as province_name,
                SUM(calls) AS total,
                SUM(reviews) AS reviews,
                SUM(good) AS good,
                SUM(bad) AS bad
            "));

        return response()->json(
            DataTableFacade::run(
                $query,
                $request,
                allowedFilters: ['*'],
                allowedSortings: ['*'])
        );
    }
}
