<?php

namespace Tests\Feature;

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

class TerminalPromptTest extends TestCase
{
    use RefreshDatabase;

    protected User $user;

    protected function setUp(): void
    {
        parent::setUp();
        $this->user = User::factory()->create();
    }

    /** @test */
    public function terminal_can_have_prompt()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '你是一个智能助手，当前用户是 {user}。',
            'variables' => ['user', 'station', 'time'],
        ]);

        $this->assertInstanceOf(TerminalPrompt::class, $terminal->prompt);
        $this->assertEquals($prompt->id, $terminal->prompt->id);
        $this->assertEquals('你是一个智能助手，当前用户是 {user}。', $terminal->prompt->prompt_template);
    }

    /** @test */
    public function prompt_belongs_to_terminal()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '测试提示词',
            'variables' => [],
        ]);

        $this->assertInstanceOf(Terminal::class, $prompt->terminal);
        $this->assertEquals($terminal->id, $prompt->terminal->id);
    }

    /** @test */
    public function prompt_variables_are_cast_to_array()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '测试提示词',
            'variables' => ['user', 'station', 'time'],
        ]);

        $this->assertIsArray($prompt->variables);
        $this->assertEquals(['user', 'station', 'time'], $prompt->variables);
    }

    /** @test */
    public function deleting_terminal_deletes_prompt()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '测试提示词',
            'variables' => [],
        ]);

        $promptId = $prompt->id;
        
        // 使用forceDelete来真正删除记录，触发级联删除
        $terminal->forceDelete();

        $this->assertDatabaseMissing('terminal_prompts', ['id' => $promptId]);
    }

    /** @test */
    public function prompt_template_can_be_empty()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '',
            'variables' => [],
        ]);

        $this->assertEquals('', $prompt->prompt_template);
    }

    /** @test */
    public function prompt_logs_activity()
    {
        $terminal = Terminal::factory()->create();
        
        $prompt = TerminalPrompt::create([
            'terminal_id' => $terminal->id,
            'prompt_template' => '测试提示词',
            'variables' => [],
        ]);

        $this->assertDatabaseHas('activity_log', [
            'subject_type' => TerminalPrompt::class,
            'subject_id' => $prompt->id,
        ]);
    }
}
