<?php

namespace App\Modules\HR\Recruitment\Listeners;

use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Services\ActivityLogService;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateHired;

class HiringActivityLogListener
{
    public function __construct(
        private readonly ActivityLogService $activityLog,
    ) {}

    public function handle(RecruitmentCandidateHired $event): void
    {
        $hiring = $event->hiring;
        $tenantId = (int) $hiring->tenant_id;
        $uid = (int) ($hiring->hired_by ?? auth()->id() ?? 0);
        if ($tenantId < 1 || $uid < 1) {
            return;
        }

        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $uid,
            module: 'hr',
            action: 'recruitment_candidate_hired',
            entityType: $hiring::class,
            entityId: (int) $hiring->id,
            description: __('recruitment.activity.candidate_hired', [
                'ref' => $hiring->reference_code,
                'employee' => $hiring->employee?->employee_code ?? (string) $hiring->employee_id,
            ]),
        ));
    }
}
