<?php

namespace App\Services\Dashboard\Province;

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

class ReportService
{
    public function cumulative(Request $request)
    {
        $query = DB::table('asteriskcdrdb.cdr')
            ->selectRaw("
                DATE(calldate) AS d,
                COUNT(CASE WHEN (recordingfile = '' OR recordingfile LIKE 'q-%') THEN 1 END) AS total,
                COUNT(CASE WHEN lastdata = 'IAX2/serverpeer/100' THEN 1 END) AS teh4,
                COUNT(CASE WHEN lastdata = 'IAX2/242serverpeer/200' THEN 1 END) AS teh5,
                COUNT(CASE WHEN dst = '500' THEN 1 END) AS os4,
                COUNT(CASE WHEN dst = '600' THEN 1 END) AS os5,
                COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 THEN 1 END) AS ok,
                COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'NO ANSWER' AND dst BETWEEN 101 AND 104 AND duration > 14 THEN 1 END) AS nok,
                COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 AND billsec > 9 THEN 1 END) AS b10,
                COUNT(CASE WHEN lastapp = 'Busy' THEN 1 END) AS dnd,
                COUNT(CASE WHEN dst BETWEEN 502 AND 599 THEN 1 END) AS trans,
                COUNT(CASE WHEN disposition <> 'BUSY' AND dst BETWEEN 101 AND 105 THEN 1 END) AS all,
                AVG(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' THEN billsec END) AS avg
            ");
//            ->whereBetween('calldate', [$from, $to]) // use Carbon or date strings
//            ->groupBy(DB::raw('DATE(calldate)'))
//            ->orderByDesc(DB::raw('DATE(calldate)'))
//            ->get();

        return DataTableFacade::run(
            $query,
            $request,
            allowedFilters: ['*'],
            allowedSortings: ['*'],
            allowedGroupBy: ['d']
        );
    }

    public function peopleMessages(Request $request)
    {
        $query = DB::table('people_messages')
            ->selectRaw('
                COUNT(CASE WHEN is_listen = 1 THEN 1 END) AS l,
                COUNT(CASE WHEN is_listen = 0 THEN 1 END) AS nl
            ');
//            ->whereBetween('date', [$date_from, $date_to])
//            ->first();

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

    public function detailedLogs(Request $request)
    {
        $query = DB::table('asteriskcdrdb.cdr')
            ->where('recordingfile', '<>', '');
//            ->whereBetween('calldate', ["$date_from $time_from", "$date_to $time_to"])
//            ->when($cid, function ($query, $cid) {
//                return $query->where('src', 'like', "%{$cid}%");
//            })
//            ->when($c_op, function ($query, $c_op) {
//                return $query->where('dst', $c_op);
//            }, function ($query) {
//                return $query->whereBetween('dst', [100, 110]);
//            })
//            ->orderByDesc('calldate');

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

    public function operatorPerformanceDaily(Request $request)
    {
        $query = DB::table('asteriskcdrdb.cdr')
            ->selectRaw("
                *
                (all - (ok + nok)) AS fail
                FROM (
                    SELECT
                        DATE(calldate) AS d,
                        dst,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 THEN 1 END) AS ok,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'NO ANSWER' AND dst BETWEEN 101 AND 104 AND duration > 14 THEN 1 END) AS nok,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 AND billsec > 9 THEN 1 END) AS b10,
                        COUNT(CASE WHEN lastapp = 'Busy' THEN 1 END) AS dnd,
                        COUNT(CASE WHEN dst BETWEEN 502 AND 599 THEN 1 END) AS trans,
                        COUNT(CASE WHEN disposition <> 'BUSY' AND dst BETWEEN 101 AND 105 THEN 1 END) AS all
                    FROM asteriskcdrdb.cdr
                    WHERE dst BETWEEN 101 AND 105
                    GROUP BY d, dst
                    ORDER BY d, dst
                );
            ");
//            ->whereBetween('calldate', ["$date_from $time_from", "$date_to $time_to"])
//            ->when($cid, function ($query, $cid) {
//                return $query->where('src', 'like', "%{$cid}%");
//            })
//            ->when($c_op, function ($query, $c_op) {
//                return $query->where('dst', $c_op);
//            }, function ($query) {
//                return $query->whereBetween('dst', [100, 110]);
//            })
//            ->orderByDesc('calldate');

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

    public function operatorPerformanceTotal(Request $request)
    {
        $query = DB::table('asteriskcdrdb.cdr')
            ->selectRaw("
                *
                (all - (ok + nok)) AS fail
                FROM (
                    SELECT
                        DATE(calldate) AS d,
                        dst,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 THEN 1 END) AS ok,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'NO ANSWER' AND dst BETWEEN 101 AND 104 AND duration > 14 THEN 1 END) AS nok,
                        COUNT(CASE WHEN dcontext = 'from-internal' AND disposition = 'ANSWERED' AND dst BETWEEN 101 AND 104 AND billsec > 9 THEN 1 END) AS b10,
                        COUNT(CASE WHEN lastapp = 'Busy' THEN 1 END) AS dnd,
                        COUNT(CASE WHEN dst BETWEEN 502 AND 599 THEN 1 END) AS trans,
                        COUNT(CASE WHEN disposition <> 'BUSY' AND dst BETWEEN 101 AND 105 THEN 1 END) AS all
                    FROM asteriskcdrdb.cdr
                    WHERE dst BETWEEN 101 AND 105
                    GROUP BY dst
                    ORDER BY dst);
            ");

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