<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\{Ticket, Invoice, CsatSurvey, User};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ReportController extends Controller
{
    public function index()
    {
        return view('admin.reports.index');
    }

    public function tickets(Request $request)
    {
        $from = $request->from ?? now()->startOfMonth()->toDateString();
        $to   = $request->to   ?? now()->toDateString();
        $data = [
            'total'         => Ticket::whereBetween('created_at',[$from,$to])->count(),
            'resolved'      => Ticket::whereBetween('created_at',[$from,$to])->where('status','resolved')->count(),
            'by_priority'   => Ticket::whereBetween('created_at',[$from,$to])->select('priority',DB::raw('count(*) as c'))->groupBy('priority')->pluck('c','priority'),
            'by_division'   => Ticket::whereBetween('created_at',[$from,$to])->select('division',DB::raw('count(*) as c'))->groupBy('division')->pluck('c','division'),
            'by_technician' => Ticket::whereBetween('created_at',[$from,$to])->whereNotNull('assigned_to')->with('assignedTo')->select('assigned_to',DB::raw('count(*) as c'))->groupBy('assigned_to')->get(),
        ];
        return view('admin.reports.tickets', compact('data','from','to'));
    }

    public function sla(Request $request)
    {
        $from    = $request->from ?? now()->startOfMonth()->toDateString();
        $to      = $request->to   ?? now()->toDateString();
        $tickets = Ticket::with('client')->whereBetween('created_at',[$from,$to])->whereNotNull('sla_config_id')->get();
        $compliance = $tickets->count() ? round($tickets->where('sla_status','ok')->count() / $tickets->count() * 100, 1) : 0;
        return view('admin.reports.sla', compact('tickets','compliance','from','to'));
    }

    public function csat(Request $request)
    {
        $from    = $request->from ?? now()->startOfMonth()->toDateString();
        $to      = $request->to   ?? now()->toDateString();
        $surveys = CsatSurvey::whereNotNull('rating')->whereBetween('created_at',[$from,$to])->with('ticket.client')->get();
        $avg     = $surveys->count() ? round($surveys->avg('rating'), 2) : 0;
        $dist    = $surveys->groupBy('rating')->map->count();
        return view('admin.reports.csat', compact('surveys','avg','dist','from','to'));
    }

    public function revenue(Request $request)
    {
        $year      = $request->year ?? now()->year;
        $monthly   = Invoice::where('status','paid')->whereYear('paid_at',$year)->select(DB::raw('MONTH(paid_at) as m'),DB::raw('SUM(total) as total'))->groupBy('m')->pluck('total','m');
        $byDivision= Invoice::where('status','paid')->whereYear('paid_at',$year)->select('division',DB::raw('SUM(total) as total'))->groupBy('division')->pluck('total','division');
        return view('admin.reports.revenue', compact('monthly','byDivision','year'));
    }

    public function technician(Request $request)
    {
        $from        = $request->from ?? now()->startOfMonth()->toDateString();
        $to          = $request->to   ?? now()->toDateString();
        $technicians = User::where('role','technician')->withCount([
            'tickets as tickets_assigned' => fn($q) => $q->whereBetween('created_at',[$from,$to]),
            'tickets as tickets_resolved' => fn($q) => $q->whereBetween('created_at',[$from,$to])->where('status','resolved'),
        ])->get();
        return view('admin.reports.technician', compact('technicians','from','to'));
    }
}
