<?php

use App\Models\Legal\LegalPage;

test('legal pages list can be retrieved', function () {
    LegalPage::create([
        'slug' => 'terms-of-service',
        'title' => ['en' => 'Terms of Service'],
        'content' => ['en' => 'Content of Terms'],
        'is_active' => true,
    ]);

    LegalPage::create([
        'slug' => 'privacy-policy',
        'title' => ['en' => 'Privacy Policy'],
        'content' => ['en' => 'Content of Privacy'],
        'is_active' => true,
    ]);

    LegalPage::create([
        'slug' => 'inactive-page',
        'title' => ['en' => 'Inactive Page'],
        'content' => ['en' => 'Content of Inactive'],
        'is_active' => false,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/legal-pages');

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data' => [
                '*' => [
                    'slug',
                    'title',
                ],
            ],
        ]);

    $this->assertCount(2, $response->json('data'));

    $slugs = collect($response->json('data'))->pluck('slug')->toArray();
    $this->assertContains('terms-of-service', $slugs);
    $this->assertContains('privacy-policy', $slugs);
    $this->assertNotContains('inactive-page', $slugs);
});

test('specific legal page can be retrieved', function () {
    $page = LegalPage::create([
        'slug' => 'terms-of-service',
        'title' => ['en' => 'Terms of Service'],
        'content' => ['en' => 'Content of Terms'],
        'is_active' => true,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/legal-pages/terms-of-service');

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data' => [
                'id',
                'slug',
                'title',
                'content',
            ],
        ]);

    $this->assertEquals($page->id, $response->json('data.id'));
    $this->assertEquals('terms-of-service', $response->json('data.slug'));
    $this->assertEquals('Terms of Service', $response->json('data.title'));
});

test('inactive legal page cannot be retrieved', function () {
    LegalPage::create([
        'slug' => 'inactive-page',
        'title' => ['en' => 'Inactive Page'],
        'content' => ['en' => 'Content of Inactive'],
        'is_active' => false,
    ]);

    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/legal-pages/inactive-page');

    $response->assertStatus(404);
});

test('non-existent legal page returns 404', function () {
    $response = $this->withHeaders(['Api-Token' => config('ecommerce.api.token')])
        ->getJson('/api/v1/legal-pages/non-existent-page');

    $response->assertStatus(404);
});
