<?php

namespace Tests\Feature\Resume;

use App\Models\ResumeReport;
use App\Services\Resume\LocalResumeTextExtractor;
use Illuminate\Support\Facades\Storage;
use RuntimeException;
use Tests\TestCase;
use ZipArchive;

class LocalResumeTextExtractorTest extends TestCase
{
    public function test_it_extracts_plain_text_resumes(): void
    {
        Storage::fake('local');
        Storage::disk('local')->put('resumes/1/resume.txt', " Remote operations manager \n");

        $resumeReport = new ResumeReport([
            'disk' => 'local',
            'path' => 'resumes/1/resume.txt',
            'mime_type' => 'text/plain',
        ]);

        $text = app(LocalResumeTextExtractor::class)->extract($resumeReport);

        $this->assertSame('Remote operations manager', $text);
    }

    public function test_it_fails_when_resume_file_is_missing(): void
    {
        Storage::fake('local');

        $resumeReport = new ResumeReport([
            'disk' => 'local',
            'path' => 'resumes/1/missing.txt',
            'mime_type' => 'text/plain',
        ]);

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Resume file was not found in private storage.');

        app(LocalResumeTextExtractor::class)->extract($resumeReport);
    }

    public function test_it_extracts_docx_resumes(): void
    {
        if (! class_exists(ZipArchive::class)) {
            $this->markTestSkipped('ZipArchive is not available.');
        }

        Storage::fake('local');

        $path = Storage::disk('local')->path('resumes/1/resume.docx');
        mkdir(dirname($path), 0777, true);

        $zip = new ZipArchive;
        $zip->open($path, ZipArchive::CREATE);
        $zip->addFromString('word/document.xml', <<<'XML'
<?xml version="1.0" encoding="UTF-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
    <w:body>
        <w:p><w:r><w:t>Remote customer success manager</w:t></w:r></w:p>
        <w:p><w:r><w:t>CRM documentation async support</w:t></w:r></w:p>
    </w:body>
</w:document>
XML);
        $zip->close();

        $resumeReport = new ResumeReport([
            'disk' => 'local',
            'path' => 'resumes/1/resume.docx',
            'original_filename' => 'resume.docx',
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        ]);

        $text = app(LocalResumeTextExtractor::class)->extract($resumeReport);

        $this->assertStringContainsString('Remote customer success manager', $text);
        $this->assertStringContainsString('CRM documentation async support', $text);
    }

    public function test_it_fails_for_unsupported_resume_formats(): void
    {
        Storage::fake('local');
        Storage::disk('local')->put('resumes/1/resume.rtf', '{\\rtf1}');

        $resumeReport = new ResumeReport([
            'disk' => 'local',
            'path' => 'resumes/1/resume.rtf',
            'original_filename' => 'resume.rtf',
            'mime_type' => 'application/rtf',
        ]);

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Unsupported resume format. Upload PDF, DOC, DOCX, or TXT.');

        app(LocalResumeTextExtractor::class)->extract($resumeReport);
    }
}
