<?php

namespace App\Modules\HR\Employees\Services;

use App\Modules\HR\Employees\DTOs\EmployeeSkillWriteData;
use App\Modules\HR\Employees\Models\Employee;
use App\Modules\HR\Employees\Models\EmployeeSkill;
use App\Modules\HR\Employees\Repositories\EmployeeSkillRepository;

class EmployeeSkillService
{
    public function __construct(
        private readonly EmployeeSkillRepository $skills,
        private readonly EmployeeProfileActivityLogger $activity,
    ) {}

    public function store(Employee $employee, EmployeeSkillWriteData $data): EmployeeSkill
    {
        $skill = $this->skills->create(array_merge($data->toArray(), [
            'tenant_id' => (int) $employee->tenant_id,
            'employee_id' => (int) $employee->id,
            'created_by' => auth()->id(),
        ]));

        $this->activity->log($employee, 'skill_created', __('activity_logs.actions.employee_skill_created', [
            'name' => $employee->full_name,
            'skill' => $skill->name,
        ]));

        return $skill;
    }

    public function update(Employee $employee, EmployeeSkill $skill, EmployeeSkillWriteData $data): EmployeeSkill
    {
        $updated = $this->skills->update($skill, $data->toArray());
        $this->activity->log($employee, 'skill_updated', __('activity_logs.actions.employee_skill_updated', [
            'name' => $employee->full_name,
            'skill' => $updated->name,
        ]));

        return $updated;
    }

    public function destroy(Employee $employee, EmployeeSkill $skill): void
    {
        $label = $skill->name;
        $this->skills->delete($skill);
        $this->activity->log($employee, 'skill_deleted', __('activity_logs.actions.employee_skill_deleted', [
            'name' => $employee->full_name,
            'skill' => $label,
        ]));
    }
}
