<?php

namespace App\Modules\HR\Recruitment\Listeners;

use App\Modules\HR\Recruitment\Events\RecruitmentApplicationCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentApplicationDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentApplicationUpdated;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateCvCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateCvDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentCandidateUpdated;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewScorecardCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewScorecardDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewScorecardUpdated;
use App\Modules\HR\Recruitment\Events\RecruitmentInterviewUpdated;
use App\Modules\HR\Recruitment\Events\RecruitmentJobRequestApproved;
use App\Modules\HR\Recruitment\Events\RecruitmentJobRequestCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentJobRequestDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentJobRequestUpdated;
use App\Modules\HR\Recruitment\Events\RecruitmentVacancyCreated;
use App\Modules\HR\Recruitment\Events\RecruitmentVacancyDeleted;
use App\Modules\HR\Recruitment\Events\RecruitmentVacancyUpdated;
use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Services\ActivityLogService;
use Illuminate\Database\Eloquent\Model;

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

    public function handleCandidateCreated(RecruitmentCandidateCreated $e): void
    {
        $this->log($e->candidate, 'recruitment_candidate_created', __('recruitment.activity.candidate_created', ['ref' => $e->candidate->reference_code]));
    }

    public function handleCandidateUpdated(RecruitmentCandidateUpdated $e): void
    {
        $this->log($e->candidate, 'recruitment_candidate_updated', __('recruitment.activity.candidate_updated', ['ref' => $e->candidate->reference_code]));
    }

    public function handleCandidateDeleted(RecruitmentCandidateDeleted $e): void
    {
        $this->log($e->candidate, 'recruitment_candidate_deleted', __('recruitment.activity.candidate_deleted', ['ref' => $e->candidate->reference_code]));
    }

    public function handleCvCreated(RecruitmentCandidateCvCreated $e): void
    {
        $this->log($e->candidateCv, 'recruitment_cv_created', __('recruitment.activity.cv_created', ['ref' => $e->candidateCv->reference_code]));
    }

    public function handleCvDeleted(RecruitmentCandidateCvDeleted $e): void
    {
        $this->log($e->candidateCv, 'recruitment_cv_deleted', __('recruitment.activity.cv_deleted', ['ref' => $e->candidateCv->reference_code]));
    }

    public function handleJobRequestCreated(RecruitmentJobRequestCreated $e): void
    {
        $this->log($e->jobRequest, 'recruitment_job_request_created', __('recruitment.activity.job_request_created', ['ref' => $e->jobRequest->reference_code]));
    }

    public function handleJobRequestUpdated(RecruitmentJobRequestUpdated $e): void
    {
        $this->log($e->jobRequest, 'recruitment_job_request_updated', __('recruitment.activity.job_request_updated', ['ref' => $e->jobRequest->reference_code]));
    }

    public function handleJobRequestDeleted(RecruitmentJobRequestDeleted $e): void
    {
        $this->log($e->jobRequest, 'recruitment_job_request_deleted', __('recruitment.activity.job_request_deleted', ['ref' => $e->jobRequest->reference_code]));
    }

    public function handleJobRequestApproved(RecruitmentJobRequestApproved $e): void
    {
        $this->log($e->jobRequest, 'recruitment_job_request_approved', __('recruitment.activity.job_request_approved', ['ref' => $e->jobRequest->reference_code]), $e->actorUserId);
    }

    public function handleVacancyCreated(RecruitmentVacancyCreated $e): void
    {
        $this->log($e->vacancy, 'recruitment_vacancy_created', __('recruitment.activity.vacancy_created', ['ref' => $e->vacancy->reference_code]));
    }

    public function handleVacancyUpdated(RecruitmentVacancyUpdated $e): void
    {
        $this->log($e->vacancy, 'recruitment_vacancy_updated', __('recruitment.activity.vacancy_updated', ['ref' => $e->vacancy->reference_code]));
    }

    public function handleVacancyDeleted(RecruitmentVacancyDeleted $e): void
    {
        $this->log($e->vacancy, 'recruitment_vacancy_deleted', __('recruitment.activity.vacancy_deleted', ['ref' => $e->vacancy->reference_code]));
    }

    public function handleApplicationCreated(RecruitmentApplicationCreated $e): void
    {
        $this->log($e->application, 'recruitment_application_created', __('recruitment.activity.application_created', ['ref' => $e->application->reference_code]));
    }

    public function handleApplicationUpdated(RecruitmentApplicationUpdated $e): void
    {
        $this->log($e->application, 'recruitment_application_updated', __('recruitment.activity.application_updated', ['ref' => $e->application->reference_code]));
    }

    public function handleApplicationDeleted(RecruitmentApplicationDeleted $e): void
    {
        $this->log($e->application, 'recruitment_application_deleted', __('recruitment.activity.application_deleted', ['ref' => $e->application->reference_code]));
    }

    public function handleInterviewCreated(RecruitmentInterviewCreated $e): void
    {
        $this->log($e->interview, 'recruitment_interview_created', __('recruitment.activity.interview_created', ['ref' => $e->interview->reference_code]));
    }

    public function handleInterviewUpdated(RecruitmentInterviewUpdated $e): void
    {
        $this->log($e->interview, 'recruitment_interview_updated', __('recruitment.activity.interview_updated', ['ref' => $e->interview->reference_code]));
    }

    public function handleInterviewDeleted(RecruitmentInterviewDeleted $e): void
    {
        $this->log($e->interview, 'recruitment_interview_deleted', __('recruitment.activity.interview_deleted', ['ref' => $e->interview->reference_code]));
    }

    public function handleScorecardCreated(RecruitmentInterviewScorecardCreated $e): void
    {
        $this->log($e->scorecard, 'recruitment_scorecard_created', __('recruitment.activity.scorecard_created', ['ref' => $e->scorecard->reference_code]));
    }

    public function handleScorecardUpdated(RecruitmentInterviewScorecardUpdated $e): void
    {
        $this->log($e->scorecard, 'recruitment_scorecard_updated', __('recruitment.activity.scorecard_updated', ['ref' => $e->scorecard->reference_code]));
    }

    public function handleScorecardDeleted(RecruitmentInterviewScorecardDeleted $e): void
    {
        $this->log($e->scorecard, 'recruitment_scorecard_deleted', __('recruitment.activity.scorecard_deleted', ['ref' => $e->scorecard->reference_code]));
    }

    private function log(Model $entity, string $action, string $description, ?int $userId = null): void
    {
        $tenantId = (int) ($entity->getAttribute('tenant_id') ?? 0);
        if ($tenantId < 1) {
            return;
        }
        $uid = $userId ?? (int) (auth()->id() ?? 0);
        if ($uid < 1) {
            return;
        }
        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $uid,
            module: 'hr',
            action: $action,
            entityType: $entity::class,
            entityId: (int) $entity->getKey(),
            description: $description,
        ));
    }
}
