<?php

declare(strict_types=1);

use App\Models\Department;
use App\Models\Employee;
use App\Models\Position;
use App\Models\Shift;
use App\Services\Employee\EmployeeService;
use Spatie\Activitylog\Models\Activity;

it('logs activity when an employee is created', function (): void {
    $department = Department::factory()->create();
    $position = Position::factory()->create();
    $shift = Shift::factory()->create();

    $employee = app(EmployeeService::class)->create([
        'employee_number' => 'EMP-LOG-001',
        'full_name' => 'Activity Test Employee',
        'gender' => 'male',
        'birth_date' => '1992-08-20',
        'phone' => '+993 61 92 92 48',
        'department_id' => $department->id,
        'position_id' => $position->id,
        'shift_id' => $shift->id,
        'employment_status' => 'active',
        'hire_date' => '2023-01-15',
    ]);

    $activity = Activity::query()
        ->where('subject_type', Employee::class)
        ->where('subject_id', $employee->id)
        ->first();

    expect($activity)->not->toBeNull()
        ->and($activity->event)->toBe('created')
        ->and($activity->description)->toContain('created');
});

it('logs activity when an employee is created via the model', function (): void {
    $employee = Employee::factory()->create([
        'employee_number' => 'EMP-LOG-002',
    ]);

    expect(
        Activity::query()
            ->where('subject_type', Employee::class)
            ->where('subject_id', $employee->id)
            ->where('event', 'created')
            ->exists(),
    )->toBeTrue();
});

it('logs updated attributes when an employee is modified', function (): void {
    $employee = Employee::factory()->create([
        'full_name' => 'Original Name',
    ]);

    $employee->update(['full_name' => 'Updated Name']);

    $activity = Activity::query()
        ->where('subject_type', Employee::class)
        ->where('subject_id', $employee->id)
        ->where('event', 'updated')
        ->first();

    expect($activity)->not->toBeNull()
        ->and($activity->properties->get('attributes'))->toHaveKey('full_name', 'Updated Name');
});
