<?php

namespace Tests\Feature;

use App\Filament\Resources\TerminalResource;
use App\Models\KnowledgeBase;
use App\Models\Terminal;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class TerminalKnowledgeBaseFormTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

    protected function setUp(): void
    {
        parent::setUp();
        
        // 创建管理员用户
        $this->user = User::factory()->create();
        $this->actingAs($this->user);
    }

    /** @test */
    public function terminal_form_has_knowledge_base_association_field()
    {
        // 创建知识库
        KnowledgeBase::create([
            'name' => '测试知识库',
            'description' => '测试描述',
            'status' => 'active',
        ]);

        // 测试创建表单包含知识库关联字段
        $component = Livewire::test(TerminalResource\Pages\CreateTerminal::class);
        
        // 验证表单可以渲染
        $component->assertSuccessful();
        
        // 验证表单组件存在（通过检查表单实例）
        $this->assertNotNull($component->instance()->form);
    }

    /** @test */
    public function terminal_can_be_created_with_knowledge_base_associations()
    {
        // 创建知识库
        $kb1 = KnowledgeBase::create([
            'name' => '生产流程知识库',
            'status' => 'active',
        ]);

        $kb2 = KnowledgeBase::create([
            'name' => '设备维护知识库',
            'status' => 'active',
        ]);

        // 创建终端
        $terminal = Terminal::factory()->create([
            'name' => '测试终端',
            'code' => 'TEST-001',
        ]);

        // 手动关联知识库（模拟表单提交后的效果）
        $terminal->knowledgeBases()->attach([
            $kb1->id => ['priority' => 1],
            $kb2->id => ['priority' => 2],
        ]);

        // 验证关联关系
        $this->assertCount(2, $terminal->knowledgeBases);
        $this->assertTrue($terminal->knowledgeBases->contains($kb1));
        $this->assertTrue($terminal->knowledgeBases->contains($kb2));
    }

    /** @test */
    public function edit_form_can_load_terminal_with_knowledge_bases()
    {
        // 创建终端和知识库
        $terminal = Terminal::factory()->create();
        
        $kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
        $kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);

        // 关联知识库
        $terminal->knowledgeBases()->attach([
            $kb1->id => ['priority' => 5],
            $kb2->id => ['priority' => 10],
        ]);

        // 测试编辑表单可以加载
        $component = Livewire::test(TerminalResource\Pages\EditTerminal::class, [
            'record' => $terminal->getRouteKey(),
        ]);

        // 验证表单可以渲染
        $component->assertSuccessful();
        
        // 验证基本信息加载正确
        $component->assertFormSet([
            'name' => $terminal->name,
            'code' => $terminal->code,
        ]);
    }

    /** @test */
    public function terminal_knowledge_base_associations_can_be_updated()
    {
        // 创建终端和知识库
        $terminal = Terminal::factory()->create();
        
        $kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
        $kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
        $kb3 = KnowledgeBase::create(['name' => '知识库3', 'status' => 'active']);

        // 初始关联
        $terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);

        // 更新关联（移除旧的，添加新的）
        $terminal->knowledgeBases()->sync([
            $kb2->id => ['priority' => 5],
            $kb3->id => ['priority' => 10],
        ]);

        // 验证更新后的关联
        $terminal->refresh();
        $this->assertCount(2, $terminal->knowledgeBases);
        $this->assertFalse($terminal->knowledgeBases->contains($kb1));
        $this->assertTrue($terminal->knowledgeBases->contains($kb2));
        $this->assertTrue($terminal->knowledgeBases->contains($kb3));
    }

    /** @test */
    public function terminal_can_remove_all_knowledge_base_associations()
    {
        // 创建终端和知识库
        $terminal = Terminal::factory()->create();
        $kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
        
        // 初始关联
        $terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);
        $this->assertCount(1, $terminal->knowledgeBases);

        // 移除所有关联
        $terminal->knowledgeBases()->detach();

        // 验证所有关联已移除
        $terminal->refresh();
        $this->assertCount(0, $terminal->knowledgeBases);
    }

    /** @test */
    public function knowledge_bases_are_ordered_by_priority_in_form()
    {
        // 创建终端和知识库
        $terminal = Terminal::factory()->create();
        
        $kb1 = KnowledgeBase::create(['name' => '知识库1', 'status' => 'active']);
        $kb2 = KnowledgeBase::create(['name' => '知识库2', 'status' => 'active']);
        $kb3 = KnowledgeBase::create(['name' => '知识库3', 'status' => 'active']);

        // 按不同优先级关联
        $terminal->knowledgeBases()->attach([
            $kb1->id => ['priority' => 10],
            $kb2->id => ['priority' => 5],
            $kb3->id => ['priority' => 1],
        ]);

        // 重新加载并验证排序
        $terminal->refresh();
        $orderedKbs = $terminal->knowledgeBases;
        
        $this->assertEquals($kb3->id, $orderedKbs[0]->id); // priority 1
        $this->assertEquals($kb2->id, $orderedKbs[1]->id); // priority 5
        $this->assertEquals($kb1->id, $orderedKbs[2]->id); // priority 10
    }
}
