<?php

namespace Tests\Feature;

use App\Models\Document;
use App\Models\User;
use App\Services\DocumentPreviewService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class DocumentPreviewServiceTest extends TestCase
{
    use RefreshDatabase;

    protected DocumentPreviewService $previewService;

    protected function setUp(): void
    {
        parent::setUp();
        $this->previewService = new DocumentPreviewService();
        Storage::fake('local');
    }

    public function test_可以检查文档是否支持预览(): void
    {
        $user = User::factory()->create();
        
        // 创建一个 .docx 文档
        $document = Document::factory()->create([
            'file_name' => 'test.docx',
            'uploaded_by' => $user->id,
        ]);
        
        $this->assertTrue($this->previewService->canPreview($document));
        
        // 创建一个 .doc 文档
        $document2 = Document::factory()->create([
            'file_name' => 'test.doc',
            'uploaded_by' => $user->id,
        ]);
        
        $this->assertTrue($this->previewService->canPreview($document2));
        
        // 创建一个不支持的格式
        $document3 = Document::factory()->create([
            'file_name' => 'test.pdf',
            'uploaded_by' => $user->id,
        ]);
        
        $this->assertFalse($this->previewService->canPreview($document3));
    }

    public function test_文档不存在时抛出异常(): void
    {
        $user = User::factory()->create();
        $document = Document::factory()->create([
            'file_path' => 'documents/2024/01/01/nonexistent.docx',
            'file_name' => 'nonexistent.docx',
            'uploaded_by' => $user->id,
        ]);

        $this->expectException(\Exception::class);
        $this->expectExceptionMessage('文档文件不存在');
        
        $this->previewService->convertToHtml($document);
    }
}
