<?php

namespace Tests\Feature;

use App\Models\KnowledgeBase;
use App\Models\Terminal;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TerminalKnowledgeBaseAssociationTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

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

    /** @test */
    public function terminal_can_associate_with_knowledge_bases()
    {
        // 创建终端
        $terminal = Terminal::factory()->create([
            'name' => '测试终端',
            'code' => 'TEST-001',
        ]);

        // 创建知识库
        $kb1 = KnowledgeBase::create([
            'name' => '知识库1',
            'description' => '测试知识库1',
            'status' => 'active',
        ]);

        $kb2 = KnowledgeBase::create([
            'name' => '知识库2',
            'description' => '测试知识库2',
            'status' => 'active',
        ]);

        // 关联知识库并设置优先级
        $terminal->knowledgeBases()->attach($kb1->id, ['priority' => 1]);
        $terminal->knowledgeBases()->attach($kb2->id, ['priority' => 2]);

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

    /** @test */
    public function knowledge_bases_are_ordered_by_priority()
    {
        // 创建终端
        $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]);
        $terminal->knowledgeBases()->attach($kb2->id, ['priority' => 5]);
        $terminal->knowledgeBases()->attach($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
    }

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

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

        // 验证初始状态
        $this->assertCount(1, $terminal->knowledgeBases);

        // 更新关联（添加新的知识库）
        $terminal->knowledgeBases()->attach($kb2->id, ['priority' => 2]);

        // 重新加载
        $terminal->refresh();

        // 验证更新后的状态
        $this->assertCount(2, $terminal->knowledgeBases);
    }

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

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

        // 验证初始状态
        $this->assertCount(2, $terminal->knowledgeBases);

        // 移除一个关联
        $terminal->knowledgeBases()->detach($kb1->id);

        // 重新加载
        $terminal->refresh();

        // 验证移除后的状态
        $this->assertCount(1, $terminal->knowledgeBases);
        $this->assertFalse($terminal->knowledgeBases->contains($kb1));
        $this->assertTrue($terminal->knowledgeBases->contains($kb2));
    }

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

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

        // 验证初始优先级
        $this->assertEquals(5, $terminal->knowledgeBases->first()->pivot->priority);

        // 更新优先级
        $terminal->knowledgeBases()->updateExistingPivot($kb->id, ['priority' => 1]);

        // 重新加载
        $terminal->refresh();

        // 验证更新后的优先级
        $this->assertEquals(1, $terminal->knowledgeBases->first()->pivot->priority);
    }
}
