<?php

namespace App\Repositories\Eloquent;

use App\Models\InstantBookingRequest;
use App\Repositories\Interfaces\InstantBookingRequestRepositoryInterface;
use Illuminate\Support\Collection;

class InstantBookingRequestRepository extends BaseRepository implements InstantBookingRequestRepositoryInterface
{
    public function __construct(InstantBookingRequest $model) { parent::__construct($model); }

    public function getByUuid(string $uuid): ?InstantBookingRequest
    {
        return $this->model->where('uuid', $uuid)->first();
    }

    public function getByUuidForUpdate(string $uuid): ?InstantBookingRequest
    {
        return $this->model->where('uuid', $uuid)->lockForUpdate()->first();
    }

    public function getByBookingId(int $bookingId): ?InstantBookingRequest
    {
        return $this->model->where('booking_id', $bookingId)->first();
    }

    public function getPendingForProfessional(int $professionId, int $professionalId, int $professionalUserId): Collection
    {
        return $this->model->where('profession_id', $professionId)
            ->where('client_id', '!=', $professionalUserId)
            ->where('status', 'waiting')->where('expires_at', '>', now())
            ->whereNull('selected_offer_id')
            ->whereDoesntHave('offers', fn ($query) => $query->where('professional_id', $professionalId))
            ->latest()->get();
    }
}
