<?php

namespace Database\Seeders;

use App\Models\Terminal;
use App\Models\TerminalPrompt;
use Illuminate\Database\Seeder;

class TerminalSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $this->command->info('开始创建终端数据...');

        // 1. 创建生产线终端
        $this->command->info('创建生产线终端...');
        $productionTerminals = [
            [
                'name' => '生产线A-工位1',
                'code' => 'PROD-A001',
                'ip_address' => '192.168.1.101',
                'diagram_url' => 'https://example.com/diagrams/prod-a-1.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 80,
                ],
                'is_online' => true,
                'last_online_at' => now(),
            ],
            [
                'name' => '生产线A-工位2',
                'code' => 'PROD-A002',
                'ip_address' => '192.168.1.102',
                'diagram_url' => 'https://example.com/diagrams/prod-a-2.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 80,
                ],
                'is_online' => true,
                'last_online_at' => now(),
            ],
            [
                'name' => '生产线B-工位1',
                'code' => 'PROD-B001',
                'ip_address' => '192.168.1.201',
                'diagram_url' => 'https://example.com/diagrams/prod-b-1.png',
                'display_config' => [
                    'resolution' => '2560x1440',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 75,
                ],
                'is_online' => false,
                'last_online_at' => now()->subHours(2),
            ],
        ];

        foreach ($productionTerminals as $terminalData) {
            $terminal = Terminal::create($terminalData);
            
            // 为每个终端创建默认提示词
            TerminalPrompt::create([
                'terminal_id' => $terminal->id,
                'prompt_template' => "你是{station}的AI助手。当前时间是{time}。请根据用户{user}的问题，提供准确的技术支持和操作指导。",
                'variables' => [
                    'station' => $terminalData['name'],
                    'time' => '{current_time}',
                    'user' => '{current_user}',
                ],
            ]);
        }

        // 2. 创建质检站终端
        $this->command->info('创建质检站终端...');
        $qcTerminals = [
            [
                'name' => '质检站-入库检-1',
                'code' => 'QC-IN001',
                'ip_address' => '192.168.2.101',
                'diagram_url' => 'https://example.com/diagrams/qc-in-1.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 85,
                ],
                'is_online' => true,
                'last_online_at' => now(),
            ],
            [
                'name' => '质检站-出库检-1',
                'code' => 'QC-OUT001',
                'ip_address' => '192.168.2.102',
                'diagram_url' => 'https://example.com/diagrams/qc-out-1.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 85,
                ],
                'is_online' => true,
                'last_online_at' => now(),
            ],
        ];

        foreach ($qcTerminals as $terminalData) {
            $terminal = Terminal::create($terminalData);
            
            TerminalPrompt::create([
                'terminal_id' => $terminal->id,
                'prompt_template' => "你是{station}的质检AI助手。请协助操作员{user}完成质检工作，提供质检标准、操作流程和异常处理建议。",
                'variables' => [
                    'station' => $terminalData['name'],
                    'user' => '{current_user}',
                ],
            ]);
        }

        // 3. 创建包装站终端
        $this->command->info('创建包装站终端...');
        $packagingTerminals = [
            [
                'name' => '包装站-工位1',
                'code' => 'PKG-001',
                'ip_address' => '192.168.3.101',
                'diagram_url' => 'https://example.com/diagrams/pkg-1.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 80,
                ],
                'is_online' => true,
                'last_online_at' => now(),
            ],
            [
                'name' => '包装站-工位2',
                'code' => 'PKG-002',
                'ip_address' => '192.168.3.102',
                'diagram_url' => 'https://example.com/diagrams/pkg-2.png',
                'display_config' => [
                    'resolution' => '1920x1080',
                    'refresh_rate' => 60,
                    'orientation' => 'landscape',
                    'brightness' => 80,
                ],
                'is_online' => false,
                'last_online_at' => now()->subDays(1),
            ],
        ];

        foreach ($packagingTerminals as $terminalData) {
            $terminal = Terminal::create($terminalData);
            
            TerminalPrompt::create([
                'terminal_id' => $terminal->id,
                'prompt_template' => "你是{station}的包装AI助手。请协助操作员{user}完成包装作业，提供包装规范、材料使用和质量要求指导。",
                'variables' => [
                    'station' => $terminalData['name'],
                    'user' => '{current_user}',
                ],
            ]);
        }

        $this->command->info('终端数据创建完成！');
        $this->command->newLine();
        $this->command->info('=== 生成的终端摘要 ===');
        $this->command->info('总终端数量: ' . Terminal::count());
        $this->command->info('  - 生产线终端: ' . Terminal::where('code', 'like', 'PROD-%')->count());
        $this->command->info('  - 质检站终端: ' . Terminal::where('code', 'like', 'QC-%')->count());
        $this->command->info('  - 包装站终端: ' . Terminal::where('code', 'like', 'PKG-%')->count());
        $this->command->info('  - 在线终端: ' . Terminal::where('is_online', true)->count());
        $this->command->info('  - 离线终端: ' . Terminal::where('is_online', false)->count());
        $this->command->info('  - 配置提示词的终端: ' . TerminalPrompt::count());
    }
}
