<?php

namespace App\Services\Administrator;

class TeacherSubmissionSupportService
{
    public function submissionStatus(int $filled, int $expected): array
    {
        if ($expected <= 0) {
            return [
                'label' => 'No students',
                'badge' => 'bg-secondary',
                'detail' => '',
                'completed' => true,
            ];
        }

        $completed = $filled >= $expected;

        return [
            'label' => $completed ? 'Submitted' : 'Missing',
            'badge' => $completed ? 'bg-success' : 'bg-danger',
            'detail' => "{$filled}/{$expected}",
            'completed' => $completed,
        ];
    }

    public function attendanceStatus(bool $submitted, int $expected): array
    {
        if ($expected <= 0) {
            return [
                'label' => 'No students',
                'badge' => 'bg-secondary',
                'completed' => true,
            ];
        }

        return [
            'label' => $submitted ? 'Submitted' : 'Missing',
            'badge' => $submitted ? 'bg-success' : 'bg-danger',
            'completed' => $submitted,
        ];
    }

    public function buildMissingItems(array $statusMap): array
    {
        $labels = [
            'midterm_score_status' => 'midterm scores',
            'midterm_comment_status' => 'midterm comments',
            'participation_status' => 'participation',
            'ptap_comment_status' => 'PTAP comments',
            'attendance_status' => 'attendance',
        ];

        $items = [];
        foreach ($statusMap as $key => $status) {
            if (!(bool) ($status['completed'] ?? true) && isset($labels[$key])) {
                $items[] = $labels[$key];
            }
        }

        return array_values($items);
    }

    public function teacherRolePriority(string $roleKey): int
    {
        return match (strtolower($roleKey)) {
            'main' => 1,
            'ta' => 2,
            default => 3,
        };
    }

    public function truncateNotificationMessage(string $html, int $limit = 1000): string
    {
        $text = trim(strip_tags($html));
        if ($text === '') {
            return '';
        }

        return mb_strlen($text) <= $limit
            ? $text
            : mb_substr($text, 0, $limit) . '…';
    }

    public function parseMissingItemsPayload(string $payload): array
    {
        if ($payload === '') {
            return [];
        }

        $decoded = json_decode(base64_decode($payload, true) ?: '', true);
        if (!is_array($decoded)) {
            return [];
        }

        $items = [];
        foreach ($decoded as $item) {
            $item = trim((string) $item);
            if ($item !== '') {
                $items[] = $item;
            }
        }

        return array_values(array_unique($items));
    }

    public function formatMissingItemsText(array $items): string
    {
        $items = array_values(array_filter(array_map('trim', $items), fn ($v) => $v !== ''));
        $count = count($items);

        if ($count === 0) {
            return '';
        }
        if ($count === 1) {
            return $items[0];
        }
        if ($count === 2) {
            return $items[0] . ' and ' . $items[1];
        }

        $last = array_pop($items);
        return implode(', ', $items) . ' and ' . $last;
    }
}
