<?php

namespace Tests\Feature;

use App\Livewire\Employees\EmployeeManager;
use App\Models\Employee;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class EmployeeCrudTest extends TestCase
{
    use RefreshDatabase;

    public function test_employee_can_be_created(): void
    {
        Livewire::test(EmployeeManager::class)
            ->set('name', 'Test Employee')
            ->set('email', 'test@example.com')
            ->set('phone', '9999999999')
            ->set('department', 'Development')
            ->set('salary', '50000')
            ->set('status', true)
            ->call('save')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('employees', [
            'email' => 'test@example.com',
            'department' => 'Development',
        ]);
    }

    public function test_employee_can_be_updated(): void
    {
        $employee = Employee::factory()->create();

        Livewire::test(EmployeeManager::class)
            ->call('edit', $employee->id)
            ->set('name', 'Updated Employee')
            ->call('save')
            ->assertHasNoErrors();

        $this->assertDatabaseHas('employees', [
            'id' => $employee->id,
            'name' => 'Updated Employee',
        ]);
    }

    public function test_employee_can_be_deleted(): void
    {
        $employee = Employee::factory()->create();

        Livewire::test(EmployeeManager::class)
            ->call('delete', $employee->id);

        $this->assertDatabaseMissing('employees', [
            'id' => $employee->id,
        ]);
    }
}
