<?php

namespace App\Services\Badges;

use App\Models\BadgePrintLog;
use Illuminate\Support\Facades\Log;

class BadgePrintLogService
{
    public function __construct(
        protected BadgePrintLog $badgePrintLogModel,
        protected BadgeTextFormatter $formatter
    ) {
    }

    public function getStatus(array $userIds, ?string $schoolYear): array
    {
        $userIds = $this->formatter->normalizeIds($userIds);

        return $this->badgePrintLogModel->getStatus($userIds, $schoolYear);
    }

    public function log(
        array $userIds,
        ?int $actorId,
        ?string $schoolYear,
        array $rolesMap = [],
        array $classesMap = [],
        int $copies = 1
    ): int {
        $userIds = $this->formatter->normalizeIds($userIds);

        return $this->badgePrintLogModel->logPrints(
            $userIds,
            $actorId,
            $schoolYear,
            is_array($rolesMap) ? $rolesMap : [],
            is_array($classesMap) ? $classesMap : [],
            $copies
        );
    }

    public function logSafely(
        array $userIds,
        ?int $actorId,
        ?string $schoolYear,
        array $rolesMap = [],
        array $classesMap = [],
        int $copies = 1
    ): void {
        try {
            $this->log($userIds, $actorId, $schoolYear, $rolesMap, $classesMap, $copies);
        } catch (\Throwable $e) {
            Log::error('Failed to log badge prints: ' . $e->getMessage());
        }
    }
}