<?php

use Illuminate\Support\Facades\File;

test('ccli fixture corpus has at least 20 txt files', function (): void {
    $files = glob(base_path('tests/fixtures/ccli/*.txt'));

    expect($files)->not->toBeEmpty();
    expect(count($files))->toBeGreaterThanOrEqual(20);
});

test('each ccli fixture is valid utf8 with section labels and title', function (): void {
    $files = glob(base_path('tests/fixtures/ccli/*.txt'));

    expect($files)->not->toBeEmpty();

    foreach ($files as $file) {
        $content = File::get($file);
        $basename = basename($file);

        expect(mb_detect_encoding($content, 'UTF-8', true))->toBe('UTF-8', $basename.' not UTF-8');
        expect(strlen($content))->toBeGreaterThanOrEqual(100, $basename.' too small');
        expect(strlen($content))->toBeLessThanOrEqual(50000, $basename.' too large');
        expect((bool) preg_match('/^(Verse|Chorus|Bridge|Pre-Chorus|Tag|Ending|Intro|Interlude|Outro|Misc|Strophe|Refrain|Brücke|Vorrefrain|Schluss|Zwischenspiel)/im', $content))->toBeTrue($basename.' has no section label');

        $lines = preg_split('/\r\n|\n|\r/', $content);
        $firstNonEmpty = '';

        foreach ($lines as $line) {
            if (trim($line) !== '') {
                $firstNonEmpty = trim($line);
                break;
            }
        }

        expect($firstNonEmpty)->not->toBeEmpty($basename.' has no title line');
    }
});

test('ccli fixture corpus covers german labels', function (): void {
    $files = glob(base_path('tests/fixtures/ccli/*.txt'));
    $germanLabelFound = false;

    foreach ($files as $file) {
        if (preg_match('/Strophe|Refrain|Brücke/i', File::get($file))) {
            $germanLabelFound = true;
            break;
        }
    }

    expect($germanLabelFound)->toBeTrue('No fixture with German labels found');
});

test('ccli fixture corpus covers repeat markers', function (): void {
    $files = glob(base_path('tests/fixtures/ccli/*.txt'));
    $repeatFound = false;

    foreach ($files as $file) {
        if (preg_match('/\(Repeat\)|[xX]\s*\d+/i', File::get($file))) {
            $repeatFound = true;
            break;
        }
    }

    expect($repeatFound)->toBeTrue('No fixture with repeat markers found');
});
