<?php

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

if (!function_exists('activity_log')) {
    function activity_log(
        PDO $pdo,
        string $action,
        string $module,
        ?string $targetType = null,
        ?int $targetId = null,
        ?string $description = null,
        ?string $title = null,
        ?string $icon = '📝',
        ?string $color = '#0ea5e9'
    ): void {
        try {
            $userId     = (int)($_SESSION['user_id'] ?? 0);
            $employeeId = (int)($_SESSION['employee_id'] ?? 0);
            $branchId   = (int)($_SESSION['branch_id'] ?? 0);

            $logTitle = $title ?: $description ?: $action;

            $st = $pdo->prepare("
                INSERT INTO system_activity_logs
                (
                    user_id,
                    employee_id,
                    branch_id,
                    module,
                    action_type,
                    entity_type,
                    entity_id,
                    title,
                    description,
                    icon,
                    color,
                    url,
                    created_at
                )
                VALUES
                (
                    :user_id,
                    :employee_id,
                    :branch_id,
                    :module,
                    :action_type,
                    :entity_type,
                    :entity_id,
                    :title,
                    :description,
                    :icon,
                    :color,
                    :url,
                    NOW()
                )
            ");

          $st->execute([
    ':user_id'     => $userId > 0 ? $userId : null,
    ':employee_id' => $employeeId > 0 ? $employeeId : null,
    ':branch_id'   => $branchId > 0 ? $branchId : null,
    ':module'      => $module,
    ':action_type' => $action,
    ':entity_type' => $targetType,
    ':entity_id'   => $targetId,
    ':title'       => $logTitle,
    ':description' => $description,
    ':icon'        => $icon,
    ':color'       => $color,
    ':url'         => $_SERVER['REQUEST_URI'] ?? null,
]);

error_log('ACTIVITY LOG SAVED: ' . $action . ' / ' . $module);

        } catch (Throwable $e) {
            error_log('Activity Log Error: ' . $e->getMessage());
        }
    }
}
if (!function_exists('award_task_points_after_completion')) {
    function award_task_points_after_completion(PDO $pdo, int $taskId, int $points = 1): void
    {
        $userId = (int)($_SESSION['user_id'] ?? 0);

        if ($userId <= 0 || $taskId <= 0) {
            return;
        }

        try {
            $check = $pdo->prepare("
                SELECT id 
                FROM system_activity_logs
                WHERE user_id = ?
                  AND entity_type = 'task'
                  AND entity_id = ?
                  AND action_type = 'complete_task'
                LIMIT 1
            ");
            $check->execute([$userId, $taskId]);

            if ($check->fetch()) {
                return;
            }

            activity_log(
                $pdo,
                'complete_task',
                'tasks',
                'task',
                $taskId,
                'تم إتمام المهمة واعتماد النقاط',
                'إتمام مهمة',
                '✅',
                '#16a34a'
            );

        } catch (Throwable $e) {
            error_log('Award Points Error: ' . $e->getMessage());
        }
    }
}

