<?php

namespace App\Modules\Integrations\Platform\Repositories;

use App\Modules\Integrations\Platform\Models\IntegrationWebhook;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Collection;

final class IntegrationWebhookRepository
{
    /**
     * @return Collection<int, IntegrationWebhook>
     */
    public function activeForTenantAndTopic(int $tenantId, string $topic): Collection
    {
        return IntegrationWebhook::query()
            ->where('tenant_id', $tenantId)
            ->where('is_active', true)
            ->whereJsonContains('topics', $topic)
            ->get();
    }

    public function paginateForTenant(int $tenantId, int $perPage = 25): LengthAwarePaginator
    {
        return IntegrationWebhook::query()
            ->where('tenant_id', $tenantId)
            ->orderByDesc('id')
            ->paginate($perPage);
    }

    public function findForTenant(int $tenantId, int $id): ?IntegrationWebhook
    {
        return IntegrationWebhook::query()
            ->where('tenant_id', $tenantId)
            ->whereKey($id)
            ->first();
    }

    public function delete(IntegrationWebhook $webhook): void
    {
        $webhook->delete();
    }
}
