import test from 'node:test';
import assert from 'node:assert/strict';

import { DocumentFormatter } from '../js/utils/document-formatter.js';

test('parses comma-separated and JSON string tags', () => {
    assert.deepEqual(DocumentFormatter.parseTags('akademik, panduan, 2026'), [
        'akademik',
        'panduan',
        '2026',
    ]);
    assert.deepEqual(DocumentFormatter.parseTags('["kerja praktik","kp"]'), [
        'kerja praktik',
        'kp',
    ]);
});

test('formats tag labels without breaking abbreviations', () => {
    assert.equal(DocumentFormatter.formatTag('SOP'), 'SOP');
    assert.equal(DocumentFormatter.formatTag('kp'), 'KP');
    assert.equal(DocumentFormatter.formatTag('kerja praktek'), 'Kerja Praktek');
    assert.equal(DocumentFormatter.formatTag('Kerja Praktek'), 'Kerja Praktek');
    assert.equal(DocumentFormatter.formatTag('umum'), 'Umum');
});

test('maps parsed tags to fixed repository categories', () => {
    assert.equal(DocumentFormatter.categoryFromTags(['TA']), 'skripsi');
    assert.equal(DocumentFormatter.categoryFromTags(['Kalender']), 'akademik');
    assert.equal(DocumentFormatter.categoryFromTags(['kp']), 'praktik');
    assert.equal(DocumentFormatter.categoryFromTags(['surat']), 'umum');
    assert.equal(DocumentFormatter.categoryFromTags([]), 'umum');
});

test('derives file type and safe download filename from document name', () => {
    assert.equal(DocumentFormatter.fileTypeFromName('Panduan Akademik.pdf'), 'pdf');
    assert.equal(DocumentFormatter.fileTypeFromName('Form KP.DOCX'), 'docx');
    assert.equal(DocumentFormatter.fileTypeFromName('Catatan.txt'), 'txt');
    assert.equal(DocumentFormatter.fileTypeFromName('Materi Seminar.pptx'), 'pptx');
    assert.equal(DocumentFormatter.downloadFilename('Form KP', 'docx'), 'Form KP.docx');
    assert.equal(DocumentFormatter.downloadFilename('Template lama.DOC', 'doc'), 'Template lama.DOC');
    assert.equal(DocumentFormatter.downloadFilename('Kalender.pdf', 'pdf'), 'Kalender.pdf');
});

test('maps supported file names to display metadata for cards', () => {
    assert.deepEqual(DocumentFormatter.fileInfoFromName('Panduan Akademik.pdf'), {
        type: 'pdf',
        icon: 'fas fa-file-pdf',
        css: 'doc-type-pdf',
    });
    assert.deepEqual(DocumentFormatter.fileInfoFromName('Form KP.DOCX'), {
        type: 'docx',
        icon: 'fas fa-file-word',
        css: 'doc-type-docx',
    });
    assert.deepEqual(DocumentFormatter.fileInfoFromName('Catatan.txt'), {
        type: 'txt',
        icon: 'fas fa-file-alt',
        css: 'doc-type-txt',
    });
    assert.deepEqual(DocumentFormatter.fileInfoFromName('Materi Seminar.pptx'), {
        type: 'pptx',
        icon: 'fas fa-file-powerpoint',
        css: 'doc-type-pptx',
    });
    assert.deepEqual(DocumentFormatter.fileInfoFromName('Lampiran.7z'), {
        type: '7z',
        icon: 'fas fa-file-archive',
        css: 'doc-type-archive',
    });
});

test('derives file metadata from expanded Directus file records when display title has no extension', () => {
    const pdfItem = {
        nama_berkas: 'SOP Kerja Praktek',
        file: {
            filename_download: 'SOP Kerja Praktek.pdf',
            type: 'application/pdf',
        },
    };
    const docxItem = {
        nama_berkas: 'Panduan Penulisan Proposal Kerja Praktek',
        file: {
            filename_download: 'Panduan Penulisan Proposal Kerja Praktek.docx',
            type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        },
    };

    assert.deepEqual(DocumentFormatter.fileInfoFromItem(pdfItem), {
        type: 'pdf',
        icon: 'fas fa-file-pdf',
        css: 'doc-type-pdf',
    });
    assert.deepEqual(DocumentFormatter.fileInfoFromItem(docxItem), {
        type: 'docx',
        icon: 'fas fa-file-word',
        css: 'doc-type-docx',
    });
    assert.equal(DocumentFormatter.downloadFilenameFromItem(pdfItem), 'SOP Kerja Praktek.pdf');
    assert.equal(DocumentFormatter.downloadFilenameFromItem(docxItem), 'Panduan Penulisan Proposal Kerja Praktek.docx');
});

test('falls back to MIME type when Directus filename metadata has no extension', () => {
    assert.deepEqual(DocumentFormatter.fileInfoFromItem({
        nama_berkas: 'Berkas Presentasi',
        file: { type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation' },
    }), {
        type: 'pptx',
        icon: 'fas fa-file-powerpoint',
        css: 'doc-type-pptx',
    });
});
