<?php
declare(strict_types=1);

namespace Tests\Integration\Members;

use Tests\DatabaseTestCase;
use Tests\Factories\MemberFactory;
use App\Modules\Members\Services\MemberSearchService;

final class MemberSearchTest extends DatabaseTestCase
{
    public function testSearchByNameArabic(): void
    {
        $factory = new MemberFactory($this->db);
        $factory->withOverrides(['full_name_ar' => 'أحمد محمود خالد'])->create();
        $factory->withOverrides(['full_name_ar' => 'محمد علي حسن'])->create();

        $results = MemberSearchService::search('أحمد');

        $this->assertNotEmpty($results);
        $this->assertCount(1, $results);
        $this->assertEquals('أحمد محمود خالد', $results[0]['full_name_ar']);
    }

    public function testSearchByNationalId(): void
    {
        $factory = new MemberFactory($this->db);
        $member = $factory->withOverrides(['national_id' => '28501011234567'])->create();

        $results = MemberSearchService::search('28501011234567');

        $this->assertNotEmpty($results);
        $this->assertEquals($member['id'], $results[0]['id']);
    }

    public function testSearchByPhone(): void
    {
        $factory = new MemberFactory($this->db);
        $member = $factory->withOverrides(['phone_mobile' => '01012345678'])->create();

        $results = MemberSearchService::search('01012345678');

        $this->assertNotEmpty($results);
        $this->assertEquals($member['id'], $results[0]['id']);
    }

    public function testSearchByMembershipNumber(): void
    {
        $factory = new MemberFactory($this->db);
        $member = $factory->withOverrides(['membership_number' => 'MEM-9999'])->create();

        $results = MemberSearchService::search('MEM-9999');

        $this->assertNotEmpty($results);
        $this->assertEquals($member['id'], $results[0]['id']);
    }

    public function testSearchReturnsEmptyForNoMatch(): void
    {
        (new MemberFactory($this->db))->create();

        $results = MemberSearchService::search('zzzznonexistentzzzz');

        $this->assertEmpty($results);
    }

    public function testSearchExcludesArchivedMembers(): void
    {
        $factory = new MemberFactory($this->db);
        $factory->withOverrides([
            'full_name_ar' => 'عضو مؤرشف فريد',
            'is_archived' => 1,
        ])->create();

        $results = MemberSearchService::search('عضو مؤرشف فريد');

        $this->assertEmpty($results);
    }

    public function testSearchReturnsEmptyForEmptyQuery(): void
    {
        (new MemberFactory($this->db))->create();

        $results = MemberSearchService::search('');

        $this->assertEmpty($results);
    }

    public function testSearchRespectsLimit(): void
    {
        $factory = new MemberFactory($this->db);
        for ($i = 0; $i < 5; $i++) {
            $factory->withOverrides([
                'full_name_ar' => 'بحث مشترك ' . $i,
                'national_id' => '2990101' . str_pad((string) (9000 + $i), 7, '0', STR_PAD_LEFT),
                'phone_mobile' => '0109' . str_pad((string) (9000 + $i), 7, '0', STR_PAD_LEFT),
            ])->create();
        }

        $results = MemberSearchService::search('بحث مشترك', 3);

        $this->assertCount(3, $results);
    }
}
