<?php

namespace Tests\Feature;

use App\Models\User;
use App\Services\Auth\ApiKeyService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ApiV1EndpointsTest extends TestCase
{
    use RefreshDatabase;

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

        config()->set('dewemoji.data_path', base_path('tests/Fixtures/emojis.fixture.json'));
    }

    public function test_categories_endpoint_returns_category_map(): void
    {
        $response = $this->getJson('/v1/categories');

        $response
            ->assertOk()
            ->assertHeader('X-Dewemoji-Tier', 'free')
            ->assertJsonPath('Smileys & Emotion.0', 'face-smiling')
            ->assertJsonPath('People & Body.0', 'hand-fingers-closed');
    }

    public function test_emojis_endpoint_supports_both_q_and_query_params(): void
    {
        $byQ = $this->getJson('/v1/emojis?q=grinning');
        $byQuery = $this->getJson('/v1/emojis?query=grinning');

        $byQ->assertOk()->assertJsonPath('total', 1);
        $byQuery->assertOk()->assertJsonPath('total', 1);
        $byQ->assertJsonPath('items.0.slug', 'grinning-face');
        $byQuery->assertJsonPath('items.0.slug', 'grinning-face');
    }

    public function test_emoji_detail_by_slug_endpoint_returns_item(): void
    {
        $response = $this->getJson('/v1/emoji/grinning-face');

        $response
            ->assertOk()
            ->assertJsonPath('slug', 'grinning-face')
            ->assertJsonPath('name', 'grinning face');
    }

    public function test_emoji_detail_by_query_slug_endpoint_returns_item(): void
    {
        $response = $this->getJson('/v1/emoji?slug=thumbs-up');

        $response
            ->assertOk()
            ->assertJsonPath('slug', 'thumbs-up')
            ->assertJsonPath('supports_skin_tone', true);
    }

    public function test_emojis_endpoint_returns_pro_tier_for_authenticated_personal_user(): void
    {
        $user = User::factory()->create([
            'tier' => 'personal',
        ]);

        $response = $this
            ->actingAs($user)
            ->getJson('/v1/emojis?q=grinning');

        $response
            ->assertOk()
            ->assertHeader('X-Dewemoji-Tier', 'pro');
    }

    public function test_emojis_endpoint_returns_pro_tier_for_personal_api_key(): void
    {
        $user = User::factory()->create([
            'tier' => 'personal',
        ]);

        $issued = app(ApiKeyService::class)->issueKey($user, 'test');

        $response = $this
            ->withHeaders(['Authorization' => 'Bearer '.$issued['plain']])
            ->getJson('/v1/emojis?q=grinning');

        $response
            ->assertOk()
            ->assertHeader('X-Dewemoji-Tier', 'pro');
    }

    public function test_metrics_endpoint_requires_token_or_allowed_ip(): void
    {
        config()->set('dewemoji.metrics.enabled', true);
        config()->set('dewemoji.metrics.token', 'secret-token');
        config()->set('dewemoji.metrics.allow_ips', []);

        $forbidden = $this->getJson('/v1/metrics');
        $forbidden->assertStatus(403);

        $allowed = $this->getJson('/v1/metrics?token=secret-token');
        $allowed->assertOk()->assertJsonPath('ok', true);
    }
}
