<?php

namespace Modules\Prevention;

use App\Member\Member;
use Modules\Dashboard\Block;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class PsPendingBlock extends Block
{

    /**
     * @var Collection<Member>
     */
    public Collection $members;

    public function mount(): void
    {
        $this->members = Member::where(function ($query) {
            $time = now()->subYears(5)->endOfYear();

            return $query
                ->orWhere(fn ($query) => $query->whereNull('ps_at')->whereNull('more_ps_at'))
                ->orWhere(fn ($query) => $query->whereNull('ps_at')->where('more_ps_at', '<=', $time))
                ->orWhere(fn ($query) => $query->where('ps_at', '<=', $time)->whereNull('more_ps_at'))
                ->orWhere(fn ($query) => $query->where('ps_at', '>=', $time)->where('more_ps_at', '<=', $time));
        })
            ->whereCurrentGroup()
            ->orderByRaw('lastname, firstname')
            ->whereHas('memberships', fn ($builder) => $builder->isLeader()->active())
            ->get();
    }

    public function title(): string
    {
        return 'Ausstehende Präventionsschulungen';
    }

    public function render(): string
    {
        return <<<'HTML'
            <div>
                @foreach ($members as $member)
                <div class="flex mt-2 items-center leading-none text-gray-100">
                    <span class="grow">{{ $member->fullname }}</span>
                </div>
                @endforeach
            </div>
        HTML;
    }
}