<?php

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

uses(RefreshDatabase::class);

beforeEach(function () {
    Storage::fake('local');
    $this->service = new DocumentService();
});

test('可以上传有效的 Word 文档', function () {
    $user = User::factory()->create();
    $file = UploadedFile::fake()->create('test.docx', 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    
    $document = $this->service->uploadDocument(
        $file,
        '测试文档',
        'global',
        null,
        $user->id
    );
    
    expect($document)->toBeInstanceOf(Document::class);
    expect($document->title)->toBe('测试文档');
    expect($document->type)->toBe('global');
    expect($document->uploaded_by)->toBe($user->id);
});

test('上传非 Word 文档应该抛出异常', function () {
    $user = User::factory()->create();
    $file = UploadedFile::fake()->create('test.pdf', 100, 'application/pdf');
    
    $this->service->uploadDocument(
        $file,
        '测试文档',
        'global',
        null,
        $user->id
    );
})->throws(\InvalidArgumentException::class, '文件格式不支持，请上传 Word 文档（.doc 或 .docx）');

test('专用文档没有分组应该抛出异常', function () {
    $user = User::factory()->create();
    $file = UploadedFile::fake()->create('test.docx', 100, 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
    
    $this->service->uploadDocument(
        $file,
        '测试文档',
        'dedicated',
        null,
        $user->id
    );
})->throws(\InvalidArgumentException::class, '专用知识库文档必须指定所属分组');

test('用户可以访问全局文档', function () {
    $user = User::factory()->create();
    $document = Document::factory()->global()->create();
    
    $hasAccess = $this->service->validateDocumentAccess($document, $user);
    
    expect($hasAccess)->toBeTrue();
});

test('用户可以访问自己分组的专用文档', function () {
    $group = Group::factory()->create();
    $user = User::factory()->create();
    $user->groups()->attach($group->id);
    
    $document = Document::factory()->dedicated($group->id)->create();
    
    $hasAccess = $this->service->validateDocumentAccess($document, $user);
    
    expect($hasAccess)->toBeTrue();
});

test('用户不能访问其他分组的专用文档', function () {
    $group1 = Group::factory()->create();
    $group2 = Group::factory()->create();
    $user = User::factory()->create();
    $user->groups()->attach($group1->id);
    
    $document = Document::factory()->dedicated($group2->id)->create();
    
    $hasAccess = $this->service->validateDocumentAccess($document, $user);
    
    expect($hasAccess)->toBeFalse();
});

test('可以记录文档下载日志', function () {
    $user = User::factory()->create();
    $document = Document::factory()->global()->create();
    
    $log = $this->service->logDownload($document, $user, '127.0.0.1');
    
    expect($log->document_id)->toBe($document->id);
    expect($log->user_id)->toBe($user->id);
    expect($log->ip_address)->toBe('127.0.0.1');
    expect($log->downloaded_at)->not->toBeNull();
});

test('下载文档时验证权限', function () {
    $group = Group::factory()->create();
    $user = User::factory()->create();
    // 用户不属于该分组
    
    $document = Document::factory()->dedicated($group->id)->create();
    
    $this->service->downloadDocument($document, $user);
})->throws(\Exception::class, '您没有权限访问此文档');

