<?php

namespace App\Livewire\Components;

use App\Models\Project;
use Livewire\Component;

class PendingMessage extends Component
{
    public $type;
    public Project $project;
    public $count;

    protected $listeners = [
        'PendingMessage:refresh' => 'updateSelf',
    ];

    public function mount(Project $project,string $type = null): void
    {
        $this->project = Project::find($project->id);
        $this->type = $type;
        $this->count = static::getPendingCount($project,$type);
    }

    public static function getPendingCount(Project $p,string $t): int
    {
        return (match($t){
            'reports' => $p->reports(),
            'downloads' => $p->downloads(),
            'mosaics' => $p->mosaics(),
            'mailings' => $p->mailings(),
        })->statusPending()->count();
    }

    public function updateSelf()
    {
        $this->count = static::getPendingCount($this->project, $this->type);
    }

    public function render()
    {
        logger(sprintf('%s', __CLASS__));
        return view('livewire.components.pending-message');
    }
}
