<?php

use App\Enums\UserType;
use App\Models\User;
use Illuminate\Http\UploadedFile;

use function Pest\Laravel\actingAs;

beforeEach(function () {
    $this->admin = User::factory()->create([
        'type' => UserType::ADMIN,
        'email' => 'admin@example.com',
    ]);
});

test('admin can view employee list', function () {
    actingAs($this->admin)
        ->get(route('admin.employees.index'))
        ->assertStatus(200)
        ->assertInertia(fn ($page) => $page
            ->component('Admin/Employees/Index')
            ->has('employees')
        );
});

test('admin can create employee with profile', function () {
    $file = UploadedFile::fake()->create('contract.pdf', 100);

    actingAs($this->admin)
        ->post(route('admin.employees.store'), [
            'name' => 'John Employee',
            'email' => 'john@employee.com',
            'password' => 'password',
            'status' => 'active',
            'job_title' => 'Developer',
            'department' => 'IT',
            'contract' => $file,
        ])
        ->assertRedirect(route('admin.employees.index'))
        ->assertSessionHas('success');

    $this->assertDatabaseHas('users', [
        'email' => 'john@employee.com',
        'type' => UserType::CUSTOMER,
    ]);

    $this->assertDatabaseHas('employee_profiles', [
        'job_title' => 'Developer',
        'department' => 'IT',
    ]);
});

test('admin can update employee', function () {
    $employee = User::factory()->create(['type' => UserType::CUSTOMER]);
    $employee->employeeProfile()->create(['job_title' => 'Junior']);

    actingAs($this->admin)
        ->put(route('admin.employees.update', $employee->id), [
            'name' => 'John Senior',
            'email' => $employee->email,
            'status' => 'active',
            'job_title' => 'Senior Developer',
        ])
        ->assertRedirect(route('admin.employees.index'));

    $this->assertDatabaseHas('employee_profiles', [
        'user_id' => $employee->id,
        'job_title' => 'Senior Developer',
    ]);
});

test('admin can delete employee', function () {
    $employee = User::factory()->create(['type' => UserType::CUSTOMER]);

    actingAs($this->admin)
        ->delete(route('admin.employees.destroy', $employee->id))
        ->assertRedirect(); // Back

    $this->assertNull(User::find($employee->id));
});
