<?php

namespace Tests\Feature\Tenant\Settings;

use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class NotificationPreferencesTest extends TestCase
{
    use RefreshDatabase;

    public function test_tenant_admin_can_fetch_notification_preferences(): void
    {
        $tenant = Tenant::factory()->create([
            'notification_preferences' => [
                'photo_thresholds' => false,
            ],
        ]);

        $user = User::factory()->create([
            'tenant_id' => $tenant->id,
            'role' => 'tenant_admin',
            'email_verified_at' => now(),
        ]);

        Sanctum::actingAs($user, abilities: ['tenant-admin', 'tenant:'.$tenant->id]);

        $response = $this->getJson('/api/v1/tenant/settings/notifications');

        $response->assertOk();
        $response->assertJsonPath('data.preferences.photo_thresholds', false);
        $response->assertJsonStructure([
            'data' => [
                'defaults',
                'preferences',
                'overrides',
            ],
        ]);
    }

    public function test_super_admin_can_fetch_notification_preferences_for_specific_tenant(): void
    {
        $tenant = Tenant::factory()->create();

        $superAdmin = User::factory()->create([
            'role' => 'super_admin',
            'tenant_id' => null,
            'email_verified_at' => now(),
        ]);

        Sanctum::actingAs($superAdmin, abilities: ['tenant-admin', 'super-admin']);

        $response = $this
            ->withHeader('X-Tenant-ID', (string) $tenant->id)
            ->getJson('/api/v1/tenant/settings/notifications');

        $response->assertOk();
        $response->assertJsonPath('data.defaults.photo_thresholds', true);
    }
}
