<?php

use App\Models\Document;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;

uses(RefreshDatabase::class);

beforeEach(function () {
    Storage::fake('local');
});

test('用户可以预览已转换的文档', function () {
    $user = User::factory()->create();
    $document = Document::factory()->create([
        'conversion_status' => 'completed',
        'markdown_path' => 'markdown/test.md',
    ]);

    Storage::disk('local')->put($document->markdown_path, '# 测试标题\n\n这是测试内容。');

    $response = $this->actingAs($user)->get(route('documents.preview', $document));

    $response->assertStatus(200);
    $response->assertSee($document->title);
    $response->assertSee('测试标题');
});

test('预览页面正确处理 Markdown 内容为空的情况', function () {
    $user = User::factory()->create();
    $document = Document::factory()->create([
        'conversion_status' => 'completed',
        'markdown_path' => null,
    ]);

    $response = $this->actingAs($user)->get(route('documents.preview', $document));

    $response->assertStatus(200);
    $response->assertSee('Markdown 内容为空');
    $response->assertSee('下载原始文档');
});

test('预览页面显示下载按钮', function () {
    $user = User::factory()->create();
    $document = Document::factory()->create([
        'conversion_status' => 'completed',
        'markdown_path' => 'markdown/test.md',
    ]);

    Storage::disk('local')->put($document->markdown_path, '# 测试');

    $response = $this->actingAs($user)->get(route('documents.preview', $document));

    $response->assertStatus(200);
    $response->assertSee('下载原文档');
    $response->assertSee(route('documents.download', $document));
});
