<?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 CumulativeVoiceRatingStatsController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = DB::table('country_voice_rating_reports')->selectRaw("
                province_id,
                province_name,
                SUM(message_quality_ok) as message_quality_ok,
                SUM(message_format_ok) as message_format_ok,
                SUM(message_quality_nok) as message_quality_nok,
                SUM(message_format_nok) as message_format_nok
            ")
            ->groupBy('province_id', 'province_name')
            ->unionAll(DB::table('country_voice_rating_reports')->selectRaw("
                0 as province_id,
                'کشوری' as province_name,
                SUM(message_quality_ok),
                SUM(message_format_ok),
                SUM(message_quality_nok),
                SUM(message_format_nok)
            "));

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