<?php

namespace Tests\Feature;

use App\Livewire\WhatsApp\WhatsAppGatewayIndex;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class ParentPhoneCrudTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed();
    }

    public function test_can_view_and_update_parent_phone_number(): void
    {
        $admin = User::factory()->create(['role_id' => 1]); // Admin
        $siswa = User::factory()->create([
            'name' => 'Siswa Test Parent Phone',
            'nip_nis' => '00987654321',
            'class_name' => 'X RPL 1',
            'phone_number' => '08123456789',
            'role_id' => 5,
        ]);

        $this->actingAs($admin);

        Livewire::test(WhatsAppGatewayIndex::class)
            ->call('openParentsModal', 'X RPL 1')
            ->assertSet('showParentsModal', true)
            ->assertSet('selectedClassForParents', 'X RPL 1')
            ->call('openEditParentModal', $siswa->id)
            ->assertSet('editingUserId', $siswa->id)
            ->set('editParentPhone', '089988776655')
            ->call('updateParentPhone')
            ->assertSet('editingUserId', null);

        $this->assertDatabaseHas('users', [
            'id' => $siswa->id,
            'phone_number' => '089988776655',
            'class_name' => 'X RPL 1',
        ]);
    }
}
