<?php

namespace Database\Seeders;

use App\Models\Station;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    use WithoutModelEvents;

    public function run(): void
    {
        $this->command->info('开始生成演示数据...');

        // 0. 权限和角色
        $this->call(PermissionSeeder::class);

        // 1. 系统设置
        $this->call(SystemSettingSeeder::class);

        // 2. 创建用户

        $pass = 'TRG}E^5BvPcbyErc';

        $this->command->info('创建用户...');
        $admin = User::factory()->create([
            'name' => '系统管理员',
            'email' => 'admin@example.com',
            'password' => Hash::make($pass),
        ]);
        $admin->assignRole('super-admin');

        $user1 = User::factory()->create([
            'name' => '张三',
            'email' => 'zhangsan@example.com',
            'password' => Hash::make($pass),
        ]);
        $user1->assignRole('user');

        $user2 = User::factory()->create([
            'name' => '李四',
            'email' => 'lisi@example.com',
            'password' => Hash::make($pass),
        ]);
        $user2->assignRole('user');

        $user3 = User::factory()->create([
            'name' => '王五',
            'email' => 'wangwu@example.com',
            'password' => Hash::make($pass),
        ]);
        $user3->assignRole('admin');

        $user4 = User::factory()->create([
            'name' => '赵六',
            'email' => 'zhaoliu@example.com',
            'password' => Hash::make($pass),
        ]);
        $user4->assignRole('user');

        // 3. 创建线站、知识库、终端
        $this->call(TerminalSeeder::class);

        // 4. 用户关联线站（管理员不关联 = 可访问全部）
        $this->command->info('建立用户-线站关联...');
        $bl02u1 = Station::where('name', 'BL02U1')->first();
        $bl07u = Station::where('name', 'BL07U')->first();
        $bl08u = Station::where('name', 'BL08U')->first();

        $user1->stations()->attach($bl02u1);
        $user2->stations()->attach($bl02u1);
        $user3->stations()->attach($bl07u);
        $user4->stations()->attach($bl08u);

        // 5. 操作指引
        $this->call(GuideSeeder::class);

        $this->command->newLine();
        $this->command->info('=== 测试账号信息 ===');
        $this->command->info("{$admin->name} : {$admin->email} / $pass");
        $this->command->info("{$user1->name}（{$bl02u1->name}）: {$user1->email} / $pass");
        $this->command->info("{$user2->name}（{$bl02u1->name}）: {$user2->email} / $pass");
        $this->command->info("{$user3->name}（{$bl07u->name}）: {$user3->email} / $pass");
        $this->command->info("{$user4->name}（{$bl08u->name}）: {$user4->email} / $pass");
    }
}
