<?php

namespace App\Services;

use App\Models\Employee;

class EmployeeCodeService
{
    public static function generate(int $userId): string
    {
        $lastEmployee = Employee::where('user_id', $userId)
            ->orderByDesc('id')
            ->first();

        $nextNumber = 1;

        if ($lastEmployee && preg_match('/EMP-(\d+)/', $lastEmployee->employee_code, $matches)) {
            $nextNumber = ((int) $matches[1]) + 1;
        }

        return 'EMP-' . str_pad((string) $nextNumber, 6, '0', STR_PAD_LEFT);
    }
}