<?php

namespace Tests\Feature;

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

class DocumentFileNameTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        Storage::fake('local');
    }

    public function test_上传文档时保留原始文件名(): void
    {
        $user = User::factory()->create();
        $documentService = new DocumentService();

        // 创建一个测试文件，使用特定的文件名
        $originalFileName = '测试文档_2024.docx';
        $file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

        // 上传文档
        $document = $documentService->uploadDocument(
            $file,
            '测试文档',
            'global',
            null,
            $user->id
        );

        // 验证文件名被正确保存
        $this->assertEquals($originalFileName, $document->file_name);
        
        // 验证文件路径包含原始文件名
        $this->assertStringContainsString($originalFileName, $document->file_path);
    }

    public function test_下载文档时使用原始文件名(): void
    {
        $user = User::factory()->create();
        $documentService = new DocumentService();

        // 创建一个测试文件
        $originalFileName = '重要文档.docx';
        $file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

        // 上传文档
        $document = $documentService->uploadDocument(
            $file,
            '重要文档',
            'global',
            null,
            $user->id
        );

        // 下载文档
        $response = $documentService->downloadDocument($document, $user);

        // 验证响应头中的文件名（可能被 URL 编码）
        $contentDisposition = $response->headers->get('Content-Disposition');
        
        // 检查是否包含文件名（可能是原始格式或 URL 编码格式）
        $encodedFileName = rawurlencode($originalFileName);
        $this->assertTrue(
            str_contains($contentDisposition, $originalFileName) || 
            str_contains($contentDisposition, $encodedFileName),
            "Content-Disposition 应该包含原始文件名或其编码版本"
        );
    }

    public function test_中文文件名正确处理(): void
    {
        $user = User::factory()->create();
        $documentService = new DocumentService();

        // 创建一个带中文名称的测试文件
        $originalFileName = '知识库管理系统需求文档.docx';
        $file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

        // 上传文档
        $document = $documentService->uploadDocument(
            $file,
            '需求文档',
            'global',
            null,
            $user->id
        );

        // 验证中文文件名被正确保存
        $this->assertEquals($originalFileName, $document->file_name);
    }

    public function test_特殊字符文件名正确处理(): void
    {
        $user = User::factory()->create();
        $documentService = new DocumentService();

        // 创建一个带特殊字符的测试文件
        $originalFileName = '文档(2024-01-01)_v1.0.docx';
        $file = UploadedFile::fake()->create($originalFileName, 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');

        // 上传文档
        $document = $documentService->uploadDocument(
            $file,
            '版本文档',
            'global',
            null,
            $user->id
        );

        // 验证特殊字符文件名被正确保存
        $this->assertEquals($originalFileName, $document->file_name);
    }
}
