<?php

namespace App\Http\Controllers\Tenant;

use App\Domain\Billing\RevenueService;
use App\Http\Controllers\Controller;
use App\Support\ClinicTime;
use Carbon\CarbonImmutable;
use Illuminate\Http\Request;

class RevenueReportController extends Controller
{
    public function index(Request $request, RevenueService $revenue)
    {
        abort_unless($request->user()->can('reports.view'), 403);

        $tz = ClinicTime::timezone();
        $from = CarbonImmutable::parse($request->query('from', CarbonImmutable::today($tz)->startOfMonth()), $tz)->startOfDay();
        $to = CarbonImmutable::parse($request->query('to', CarbonImmutable::today($tz)), $tz)->endOfDay();

        return view('reports.revenue', [
            'from' => $from,
            'to' => $to,
            'collected' => $revenue->collectedBetween($from->utc(), $to->utc()),
            'byMethod' => $revenue->collectedByMethod($from->utc(), $to->utc()),
            'daily' => $revenue->collectedDaily(30),
            'byCategory' => $revenue->billedByCategory($from->utc(), $to->utc()),
            'byProcedure' => $revenue->billedByProcedure($from->utc(), $to->utc()),
        ]);
    }
}
