<?php

use App\Models\Member;

/*
|--------------------------------------------------------------------------
| Member API Tests
|--------------------------------------------------------------------------
|
| Feature tests for the /api/v1/members endpoint
|
*/

it('returns a successful response for members index', function () {
    $response = $this->getJson('/api/v1/members');

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

it('returns members with correct structure', function () {
    $response = $this->getJson('/api/v1/members');

    $response->assertStatus(200)
        ->assertJsonStructure([
            'data',
            'pagination' => [
                'currentPage',
                'lastPage',
                'perPage',
                'total',
                'count',
                'hasMorePages',
                'nextPageUrl',
                'prevPageUrl',
            ],
        ]);
});

it('can paginate members', function () {
    $response = $this->getJson('/api/v1/members?perPage=5&page=1');

    $response->assertStatus(200)
        ->assertJsonPath('pagination.perPage', 5)
        ->assertJsonPath('pagination.currentPage', 1);
});

it('can filter members by member type', function () {
    $response = $this->getJson('/api/v1/members?memberType=1');

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

it('can filter members by letter', function () {
    $response = $this->getJson('/api/v1/members?letter=A');

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

it('can order members alphabetically', function () {
    $response = $this->getJson('/api/v1/members?orderBy=alphabetical');

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

/*
|--------------------------------------------------------------------------
| Member Show Endpoint Tests
|--------------------------------------------------------------------------
*/

it('returns a member by slug', function () {
    $member = Member::full()
        ->where('status', 'published')
        ->first();

    if (!$member) {
        $this->markTestSkipped('No published members in the database');
    }

    $response = $this->getJson("/api/v1/members/{$member->lang_slug}");

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

it('returns 404 for non-existent member', function () {
    $response = $this->getJson('/api/v1/members/non-existent-slug-12345');

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