<?php

namespace App\Modules\HR\Shifts\Listeners;

use App\Models\User;
use App\Modules\Admin\ActivityLog\DTOs\ActivityLogWriteData;
use App\Modules\Admin\ActivityLog\Services\ActivityLogService;
use App\Modules\HR\Shifts\Events\ShiftAssigned;
use App\Modules\HR\Shifts\Events\ShiftCreated;
use App\Modules\HR\Shifts\Events\ShiftDeactivated;
use App\Modules\HR\Shifts\Events\ShiftUpdated;
use App\Modules\HR\Shifts\Models\EmployeeShiftAssignment;
use App\Modules\HR\Shifts\Models\Shift;

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

    public function handleCreated(ShiftCreated $event): void
    {
        $this->logShift($event->shift, 'shift_created', __('activity_logs.actions.shift_created', ['name' => $event->shift->name]));
    }

    public function handleUpdated(ShiftUpdated $event): void
    {
        $this->logShift($event->shift, 'shift_updated', __('activity_logs.actions.shift_updated', ['name' => $event->shift->name]));
    }

    public function handleDeactivated(ShiftDeactivated $event): void
    {
        $this->logShift($event->shift, 'shift_deactivated', __('activity_logs.actions.shift_deactivated', ['name' => $event->shift->name]));
    }

    public function handleAssigned(ShiftAssigned $event): void
    {
        $a = $event->assignment->loadMissing(['employee', 'shift']);
        $tenantId = (int) $a->tenant_id;
        if ($tenantId < 1) {
            return;
        }
        $user = auth()->user();
        $userId = $user instanceof User ? $user->id : null;
        $desc = __('activity_logs.actions.shift_assigned', [
            'employee' => $a->employee?->full_name ?? '—',
            'shift' => $a->shift?->name ?? '—',
        ]);
        $this->activityLog->log(new ActivityLogWriteData(
            tenantId: $tenantId,
            userId: $userId,
            module: 'hr',
            action: 'shift_assigned',
            entityType: EmployeeShiftAssignment::class,
            entityId: $a->id,
            description: $desc,
        ));
    }

    private function logShift(Shift $shift, string $action, string $description): void
    {
        $tenantId = (int) $shift->tenant_id;
        if ($tenantId < 1) {
            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: Shift::class,
            entityId: $shift->id,
            description: $description,
        ));
    }
}
