<?php

namespace App\Modules\Admin\ActivityLog\Listeners;

use App\Models\User;
use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Services\ActivityLogService;
use App\Modules\HR\Departments\Events\DepartmentCreated;
use App\Modules\HR\Departments\Events\DepartmentDeleted;
use App\Modules\HR\Departments\Events\DepartmentUpdated;
use App\Modules\HR\Departments\Models\Department;
use App\Modules\HR\Positions\Events\PositionCreated;
use App\Modules\HR\Positions\Events\PositionDeactivated;
use App\Modules\HR\Positions\Events\PositionUpdated;
use App\Modules\HR\Positions\Models\Position;

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

    public function handleDepartmentCreated(DepartmentCreated $event): void
    {
        $this->logDepartment($event->department, 'created', __('activity_logs.actions.department_created', ['name' => $event->department->name]));
    }

    public function handleDepartmentUpdated(DepartmentUpdated $event): void
    {
        $this->logDepartment($event->department, 'updated', __('activity_logs.actions.department_updated', ['name' => $event->department->name]));
    }

    public function handleDepartmentDeleted(DepartmentDeleted $event): void
    {
        $this->logDepartment($event->department, 'deleted', __('activity_logs.actions.department_deleted', ['name' => $event->department->name]));
    }

    public function handlePositionCreated(PositionCreated $event): void
    {
        $this->logPosition($event->position, 'created', __('activity_logs.actions.position_created', ['title' => $event->position->title]));
    }

    public function handlePositionUpdated(PositionUpdated $event): void
    {
        $this->logPosition($event->position, 'updated', __('activity_logs.actions.position_updated', ['title' => $event->position->title]));
    }

    public function handlePositionDeactivated(PositionDeactivated $event): void
    {
        $this->logPosition($event->position, 'deactivated', __('activity_logs.actions.position_deactivated', ['title' => $event->position->title]));
    }

    private function logDepartment(Department $department, string $action, string $description): void
    {
        $tenantId = (int) $department->tenant_id;
        if ($tenantId === 0) {
            return;
        }

        $user = auth()->user();
        $userId = $user instanceof User ? $user->id : null;

        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $userId,
            module: 'hr',
            action: $action,
            entityType: Department::class,
            entityId: $department->id,
            description: $description,
        ));
    }

    private function logPosition(Position $position, string $action, string $description): void
    {
        $tenantId = (int) $position->tenant_id;
        if ($tenantId === 0) {
            return;
        }

        $user = auth()->user();
        $userId = $user instanceof User ? $user->id : null;

        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $userId,
            module: 'hr',
            action: $action,
            entityType: Position::class,
            entityId: $position->id,
            description: $description,
        ));
    }
}
