<?php

namespace App\Services\Reports;

use App\Models\Configuration;
use App\Models\User;

class SlipPrinterConfigService
{
    public function context(): array
    {
        return [
            'school_year' => Configuration::getConfig('school_year'),
            'semester' => Configuration::getConfig('semester'),
        ];
    }

    public function currentAdminName(?int $userId): string
    {
        if ($userId && $userId > 0) {
            $user = User::query()->select('firstname', 'lastname')->find($userId);
            if ($user) {
                $full = trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? ''));
                if ($full !== '') {
                    return $full;
                }
            }
        }

        return '';
    }

    public function printerConfig(): array
    {
        $get = function ($keys, $default = null) {
            $keys = is_array($keys) ? $keys : [$keys];
            foreach ($keys as $key) {
                $val = env($key);
                if ($val !== null && $val !== '') {
                    return $val;
                }
            }
            return $default;
        };

        return [
            'chars_per_line' => (int) $get(['printer.chars_per_line', 'PRINTER_CHARS_PER_LINE'], 48),
            'feed_lines' => (int) $get(['printer.feed_lines', 'PRINTER_FEED_LINES'], 3),
            'paper' => (string) $get(['printer.paper', 'SLIP_PAPER'], 'card'),
        ];
    }
}
