<?php

namespace Tests\Feature;

use App\Models\Tenant;
use App\Services\FacebookAdsService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class AdsAnalyticsTest extends TestCase
{
    use RefreshDatabase;

    private Tenant $tenant;
    private string $token = 'test-tenant-token-123';

    protected function setUp(): void
    {
        parent::setUp();

        $this->tenant = Tenant::factory()->create([
            'auth_token'          => $this->token,
            'subscription_status' => 'trial',
            'trial_ends_at'       => now()->addDays(14),
        ]);
    }

    /** @test */
    public function it_requires_auth_token_for_ads_summary(): void
    {
        $response = $this->getJson('/api/admin/ads/summary');
        $response->assertStatus(401);
    }

    /** @test */
    public function it_returns_ads_history_for_authenticated_tenant(): void
    {
        $response = $this->withHeaders(['X-Tenant-Token' => $this->token])
                         ->getJson('/api/admin/ads/history');

        $response->assertStatus(200)
                 ->assertJsonStructure(['success', 'data']);
    }

    /** @test */
    public function it_returns_empty_history_when_no_ads_fetched(): void
    {
        $response = $this->withHeaders(['X-Tenant-Token' => $this->token])
                         ->getJson('/api/admin/ads/history');

        $response->assertStatus(200)
                 ->assertJson(['success' => true, 'data' => []]);
    }
}
