<?php

declare(strict_types=1);

use App\Models\Department;

it('generates an uppercase slug code from name', function (): void {
    expect(Department::generateUniqueCodeFromName('Human Resources'))
        ->toBe('HUMAN-RESOURCES');
});

it('generates code on save when code is blank', function (): void {
    $department = Department::factory()->create([
        'name' => 'Human Resources',
        'code' => '',
    ]);

    expect($department->code)->toBe('HUMAN-RESOURCES');
});

it('does not overwrite an existing code on update', function (): void {
    $department = Department::factory()->create([
        'name' => 'Human Resources',
        'code' => 'HR',
    ]);

    $department->update(['name' => 'People Operations']);

    expect($department->fresh()->code)->toBe('HR');
});

it('appends a suffix when the generated code already exists', function (): void {
    Department::factory()->create([
        'name' => 'Sales Team',
        'code' => 'SALES',
    ]);

    $department = Department::factory()->create([
        'name' => 'Sales',
        'code' => '',
    ]);

    expect($department->code)->toBe('SALES-1');
});
