<?php

namespace Modules\Bitrix\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Modules\Bitrix\Enums\BitrixWebhooksEnum;

class BitrixWebhooks extends Model
{
    use HasFactory;
    protected $table = 'bx_webhooks';
    protected $fillable = [
        'name',
        'url'
    ];

    /**
     * Get webhook url by its name
     * @param string $name webhook name
     * @return string|bool url of webhook or false
     */
    public static function getUrlByName(BitrixWebhooksEnum $webhook): string|bool
    {
        $row = BitrixWebhooks::where('name', $webhook->name);
        if ($row->count() == 1)
        {
            return $row->first()->url;
        }
        return false;
    }
}
