<?php

use App\Models\Document;
use App\Models\Group;
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([
        'type' => 'global',
        'conversion_status' => 'completed',
        'markdown_path' => 'markdown/test.md',
    ]);

    // 创建 Markdown 文件
    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('用户可以预览有权限的专用文档', function () {
    // 创建分组和用户
    $group = Group::factory()->create();
    $user = User::factory()->create();
    $user->groups()->attach($group);

    // 创建专用文档
    $document = Document::factory()->create([
        'type' => 'dedicated',
        'group_id' => $group->id,
        'conversion_status' => 'completed',
        'markdown_path' => 'markdown/test.md',
    ]);

    // 创建 Markdown 文件
    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('用户无法预览无权限的专用文档', function () {
    // 创建两个分组
    $group1 = Group::factory()->create();
    $group2 = Group::factory()->create();
    
    // 用户属于分组1
    $user = User::factory()->create();
    $user->groups()->attach($group1);

    // 文档属于分组2
    $document = Document::factory()->create([
        'type' => 'dedicated',
        'group_id' => $group2->id,
        'conversion_status' => 'completed',
    ]);

    // 尝试访问预览页面
    $response = $this->actingAs($user)->get(route('documents.preview', $document));

    // 验证被拒绝
    $response->assertStatus(403);
});

test('预览页面正确处理 Markdown 内容为空的情况', function () {
    // 创建用户和文档（没有 Markdown 内容）
    $user = User::factory()->create();
    $document = Document::factory()->create([
        'type' => 'global',
        '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([
        'type' => 'global',
        'conversion_status' => 'completed',
        'markdown_path' => 'markdown/test.md',
    ]);

    // 创建 Markdown 文件
    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));
});
