<?php

namespace Tests\Feature\Tenant;

use App\Models\TenantAdminPushSubscription;
use Illuminate\Support\Str;

class AdminPushSubscriptionTest extends TenantTestCase
{
    public function test_admin_push_subscription_can_be_registered(): void
    {
        $payload = [
            'endpoint' => 'https://example.com/push/'.Str::random(8),
            'keys' => [
                'p256dh' => base64_encode(random_bytes(32)),
                'auth' => base64_encode(random_bytes(16)),
            ],
            'expiration_time' => null,
            'content_encoding' => 'aes128gcm',
            'device_id' => 'admin-device-123',
        ];

        $response = $this->authenticatedRequest('POST', '/api/v1/tenant/notifications/push-subscriptions', $payload);

        $response->assertCreated();
        $this->assertDatabaseHas('tenant_admin_push_subscriptions', [
            'tenant_id' => $this->tenant->id,
            'user_id' => $this->tenantUser->id,
            'endpoint' => $payload['endpoint'],
            'device_id' => $payload['device_id'],
            'status' => 'active',
        ]);
    }

    public function test_admin_push_subscription_can_be_revoked(): void
    {
        $subscription = TenantAdminPushSubscription::factory()->create([
            'tenant_id' => $this->tenant->id,
            'user_id' => $this->tenantUser->id,
            'status' => 'active',
        ]);

        $response = $this->authenticatedRequest('DELETE', '/api/v1/tenant/notifications/push-subscriptions', [
            'endpoint' => $subscription->endpoint,
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('tenant_admin_push_subscriptions', [
            'id' => $subscription->id,
            'status' => 'revoked',
        ]);
    }
}
