<?php

namespace App\Livewire\Campagne;

use Livewire\Component;
use App\Models\Campagna;
use App\Models\Assegnazione;
use App\Models\Territorio;

class CampagnaShow extends Component
{
    public Campagna $campagna;

    public function mount(Campagna $campagna)
    {
        $this->campagna = $campagna;
    }

    public function render()
    {
        // All assignments with returned_at in campaign range that were counted
        $conteggiate = Assegnazione::where('campagna_id', $this->campagna->id)
            ->where('counted_in_campaign', true)
            ->with(['territorio', 'proclamatore'])
            ->orderBy('returned_at')
            ->get();

        // All assignments that were active during this campaign range
        $assegnateNelRange = Assegnazione::where('assigned_at', '<=', $this->campagna->end_date)
            ->where(function ($q) {
                $q->whereNull('returned_at')
                    ->orWhere('returned_at', '>=', $this->campagna->start_date);
            })
            ->count();

        return view('livewire.campagne.campagna-show', [
            'conteggiate' => $conteggiate,
            'assegnateNelRange' => $assegnateNelRange,
        ]);
    }
}
