<?php

namespace App\Repositories\Eloquent;

use App\Models\InstantBookingOffer;
use App\Repositories\Interfaces\InstantBookingOfferRepositoryInterface;

class InstantBookingOfferRepository extends BaseRepository implements InstantBookingOfferRepositoryInterface
{
    public function __construct(InstantBookingOffer $model) { parent::__construct($model); }

    public function respond(int $requestId, int $professionalId, string $status, ?float $amount): InstantBookingOffer
    {
        return $this->model->updateOrCreate(
            ['instant_booking_request_id' => $requestId, 'professional_id' => $professionalId],
            ['status' => $status, 'amount' => $status === 'accepted' ? $amount : null]
        );
    }

    public function getAcceptedOffer(int $offerId, int $requestId): ?InstantBookingOffer
    {
        return $this->model->where('id', $offerId)
            ->where('instant_booking_request_id', $requestId)
            ->where('status', 'accepted')->first();
    }

    public function deleteForRequest(int $requestId): int
    {
        return $this->model->where('instant_booking_request_id', $requestId)->delete();
    }
}
