<?php

namespace App\Components\Report;

use Livewire\Component;
use App\Models\Client\Client;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class EstimatedValueByTranche extends Component
{
    public function render()
    {
        $rows = Client::query()
            ->join('claims', 'claims.client_id', '=', 'clients.id')
            ->join('redress_estimates', 'redress_estimates.claim_id', '=', 'claims.id')
            ->leftJoin('tranches', 'tranches.id', '=', 'clients.tranche_id')
            ->selectRaw('
                COALESCE(tranches.tranche_name, "No Tranche") as tranche_name,
                COUNT(DISTINCT clients.id) as client_count,
                SUM(redress_estimates.value_min) as total_min,
                SUM(redress_estimates.value_max) as total_max,
                AVG(redress_estimates.value_min) as average_min,
                AVG(redress_estimates.value_max) as average_max
            ')
            ->groupBy('tranche_name')
            ->orderBy('tranche_name')
            ->get();
        
          return view('components.report.estimated-value-by-tranche',compact('rows'));
    }
}
