<?php

namespace App\Modules\HR\Attendance\Listeners;

use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Services\ActivityLogService;
use App\Modules\HR\Attendance\Events\AttendanceCorrectionApproved;
use App\Modules\HR\Attendance\Events\AttendanceCorrectionRejected;
use App\Modules\HR\Attendance\Events\AttendanceCorrectionSubmitted;
use App\Modules\HR\Attendance\Models\AttendanceCorrectionRequest;
use App\Modules\HR\Attendance\Models\AttendanceRecord;

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

    public function handleSubmitted(AttendanceCorrectionSubmitted $event): void
    {
        $c = $event->correction;
        $this->logCorrection(
            $c,
            'attendance_correction_submitted',
            __('attendance_corrections.activity.submitted', ['id' => $c->id]),
            $event->actorUserId,
        );
    }

    public function handleApproved(AttendanceCorrectionApproved $event): void
    {
        $c = $event->correction;
        $this->logCorrection(
            $c,
            'attendance_correction_approved',
            __('attendance_corrections.activity.approved', ['id' => $c->id]),
            $event->reviewerUserId,
        );
        $this->logRecord(
            $c,
            'attendance_modified_after_correction',
            __('attendance_corrections.activity.record_updated', [
                'id' => $c->attendance_record_id,
                'correction' => $c->id,
            ]),
            $event->reviewerUserId,
        );
    }

    public function handleRejected(AttendanceCorrectionRejected $event): void
    {
        $c = $event->correction;
        $this->logCorrection(
            $c,
            'attendance_correction_rejected',
            __('attendance_corrections.activity.rejected', ['id' => $c->id]),
            $event->reviewerUserId,
        );
    }

    private function logCorrection(
        AttendanceCorrectionRequest $c,
        string $action,
        string $description,
        int $userId,
    ): void {
        $this->write((int) $c->tenant_id, $userId, $action, AttendanceCorrectionRequest::class, $c->id, $description);
    }

    private function logRecord(
        AttendanceCorrectionRequest $c,
        string $action,
        string $description,
        int $userId,
    ): void {
        $this->write(
            (int) $c->tenant_id,
            $userId,
            $action,
            AttendanceRecord::class,
            (int) $c->attendance_record_id,
            $description,
        );
    }

    private function write(
        int $tenantId,
        int $userId,
        string $action,
        string $entityType,
        int $entityId,
        string $description,
    ): void {
        if ($tenantId < 1) {
            return;
        }
        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $userId,
            module: 'hr',
            action: $action,
            entityType: $entityType,
            entityId: $entityId,
            description: $description,
        ));
    }
}
