<?php

namespace App\Components\Report\Wip;

use App\Models\Solicitor\Solicitor;
use App\Models\Tranche;
use App\Models\WipEntry;
use Livewire\Component;

class CampaignTrancheReport extends Component
{
    public ?int $selectedSolicitorId = null;
    public ?int $selectedTrancheId = null;

    public function mount(): void
    {
        $this->selectedSolicitorId = session('workspace_solicitor_id');
    }

    public function getTranchesProperty()
    {
        return Tranche::query()
            ->when($this->selectedSolicitorId, fn ($query) =>
                $query->where('solicitor_id', $this->selectedSolicitorId)
            )
            ->orderBy('tranche_name')
            ->get();
    }

    public function getRowsProperty()
    {
        return WipEntry::query()
        ->join('clients', 'wip_entries.client_id', '=', 'clients.id')
        ->leftJoin('tranches', 'clients.tranche_id', '=', 'tranches.id')
        ->leftJoin('solicitors', 'clients.solicitor_id', '=', 'solicitors.id')
        ->leftJoin('users', 'wip_entries.user_id', '=', 'users.id')
        ->selectRaw("
            clients.id as client_id,
            clients.slug as client_slug,
            clients.name_first as client_name_first,
            clients.name_last as client_name_last,
            clients.solicitor_id,
            solicitors.name as solicitor_name,
            clients.tranche_id,
            tranches.tranche_name,
            SUM(wip_entries.units) as total_units,
            SUM(wip_entries.seconds) as total_seconds,
            SUM(wip_entries.hours) as total_hours,
            COUNT(wip_entries.id) as total_entries,
            SUM(
                wip_entries.units *
                CASE users.wip_grade
                    WHEN 'C' THEN 21.80
                    WHEN 'D' THEN 15.00
                    ELSE 0
                END
            ) as total_value
        ")
        ->when($this->selectedSolicitorId, fn ($query) =>
            $query->where('clients.solicitor_id', $this->selectedSolicitorId)
        )
        ->when($this->selectedTrancheId, fn ($query) =>
            $query->where('clients.tranche_id', $this->selectedTrancheId)
        )
        ->groupBy(
            'clients.id',
            'clients.slug',
            'clients.name_first',
            'clients.name_last',
            'clients.solicitor_id',
            'solicitors.name',
            'clients.tranche_id',
            'tranches.tranche_name'
        )
        ->orderByDesc('total_units')
        ->get();
    }

    public function getTotalUnitsProperty(): int
    {
        return (int) $this->rows->sum('total_units');
    }

    public function getTotalValueProperty(): float
    {
        return (float) $this->rows->sum('total_value');
    }

    public function getTotalHoursProperty(): float
    {
        return (float) $this->rows->sum('total_hours');
    }

    public function render()
    {
        return view('components.report.wip.campaign-tranche-report', [
            'solicitors' => Solicitor::orderBy('name')->get(),
            'tranches' => $this->tranches,
            'rows' => $this->rows,
            'totalUnits' => $this->totalUnits,
            'totalHours' => $this->totalHours,
            'totalValue' => $this->totalValue,
        ]);
    }
}