<?php

declare(strict_types=1);

namespace ImovelZapAi\Visits\Application\Queries;

use Carbon\Carbon;
use Illuminate\Support\Collection;
use ImovelZapAi\Visits\Domain\Enums\VisitStatus;
use ImovelZapAi\Visits\Infrastructure\Persistence\Eloquent\VisitModel;

final class GetBrokerAgendaQuery
{
    /**
     * @return Collection<int, VisitModel>
     */
    public function execute(
        string $tenantId,
        int $brokerUserId,
        ?Carbon $from = null,
        ?Carbon $to = null,
    ): Collection {
        $from ??= now()->startOfDay();
        $to ??= now()->addDays(7)->endOfDay();

        return VisitModel::query()
            ->where('tenant_id', $tenantId)
            ->where('responsible_user_id', $brokerUserId)
            ->whereNotIn('status', [VisitStatus::Cancelled])
            ->whereBetween('scheduled_start', [$from, $to])
            ->with(['contact', 'property'])
            ->orderBy('scheduled_start')
            ->get();
    }
}
