<?php

namespace App\Components\Report;

use App\Models\Claim\Status;
use Illuminate\Support\Facades\DB;
use Livewire\Component;

class DaysInStatusReport extends Component {

    public function getStatusRangeCountsProperty() {
        $results = Status::select('statuses.display_name as name')
            ->leftJoin('status_histories', function($join) {
                $join->on('statuses.id', '=', 'status_histories.status_id');
            })
            ->select(
                'statuses.display_name as name',
                DB::raw("COUNT(CASE WHEN DATEDIFF(NOW(), status_histories.created_at) <= 1 THEN 1 END) as range_today"),
                DB::raw("COUNT(CASE WHEN DATEDIFF(NOW(), status_histories.created_at) BETWEEN 2 AND 7 THEN 1 END) as range_week"),
                DB::raw("COUNT(CASE WHEN DATEDIFF(NOW(), status_histories.created_at) BETWEEN 8 AND 30 THEN 1 END) as range_month"),
                DB::raw("COUNT(CASE WHEN DATEDIFF(NOW(), status_histories.created_at) > 30 THEN 1 END) as range_old")
            )
            ->groupBy('statuses.id', 'statuses.display_name')
            ->get();

        return $results;
    }

    public function render()
    {
        return view('components.report.days-in-status-report', [
            'reportData' => $this->statusRangeCounts
        ]);
    }
}
