<?php

namespace Database\Seeders;

use App\Models\Document;
use App\Models\Group;
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;

    /**
     * Seed the application's database.
     */
    public function run(): void
    {
        $this->command->info('开始生成演示数据...');

        // 0. 创建权限和角色（必须最先执行）
        $this->call(PermissionSeeder::class);

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

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

        // 3. 创建普通用户
        $this->command->info('创建普通用户...');
        $user1 = User::factory()->create([
            'name' => '张三',
            'email' => 'zhangsan@example.com',
            'password' => Hash::make('password'),
        ]);
        $user1->assignRole('user');

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

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

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

        // 3. 创建分组（按照光束线）
        $this->command->info('创建光束线分组...');
        $beamlines = [
            'BL02U1',
            'BL07U',
            'BL08U',
            'BL13HB',
            'BL13U',
            'BL14B',
            'BL14W',
            'BL15U',
            'BL16B',
            'BL16U1',
        ];

        $groups = [];
        foreach ($beamlines as $beamline) {
            $groups[$beamline] = Group::factory()->create([
                'name' => $beamline,
                'description' => null,
            ]);
        }

        // 4. 建立用户和分组的关联关系
        $this->command->info('建立用户和分组的关联关系...');
        // 管理员属于所有光束线分组
        $admin->groups()->attach(array_column($groups, 'id'));

        // 张三和李四属于 BL02U1
        $user1->groups()->attach($groups['BL02U1']->id);
        $user2->groups()->attach($groups['BL02U1']->id);

        // 王五属于 BL07U
        $user3->groups()->attach($groups['BL07U']->id);

        // 赵六属于 BL08U
        $user4->groups()->attach($groups['BL08U']->id);

        // 5. 创建全局文档（仅元数据，不创建实际文件）
        $this->command->info('创建全局文档...');
        Document::create([
            'title' => '公司员工手册',
            'description' => '包含公司规章制度、员工福利、考勤制度等重要信息',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => '员工手册_2024.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'global',
            'group_id' => null,
            'uploaded_by' => $admin->id,
            'conversion_status' => 'pending',
        ]);

        Document::create([
            'title' => '办公室使用规范',
            'description' => '办公室设施使用规范和注意事项',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => '办公室规范.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'global',
            'group_id' => null,
            'uploaded_by' => $admin->id,
            'conversion_status' => 'pending',
        ]);

        Document::create([
            'title' => '公司年度总结报告',
            'description' => '2024年度公司发展总结和2025年规划',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => '年度总结_2024.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'global',
            'group_id' => null,
            'uploaded_by' => $admin->id,
            'conversion_status' => 'pending',
        ]);

        Document::create([
            'title' => '安全管理制度',
            'description' => '公司信息安全和物理安全管理制度',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => '安全管理制度.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'global',
            'group_id' => null,
            'uploaded_by' => $admin->id,
            'conversion_status' => 'pending',
        ]);

        // 6. 创建 BL02U1 光束线专用文档
        $this->command->info('创建 BL02U1 光束线专用文档...');
        Document::create([
            'title' => 'BL02U1 操作手册',
            'description' => 'BL02U1 光束线设备操作规程和注意事项',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => 'BL02U1_操作手册.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'dedicated',
            'group_id' => $groups['BL02U1']->id,
            'uploaded_by' => $user1->id,
            'conversion_status' => 'pending',
        ]);

        Document::create([
            'title' => 'BL02U1 维护记录',
            'description' => 'BL02U1 光束线设备维护和检修记录',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => 'BL02U1_维护记录.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'dedicated',
            'group_id' => $groups['BL02U1']->id,
            'uploaded_by' => $user2->id,
            'conversion_status' => 'pending',
        ]);

        // 7. 创建 BL07U 光束线专用文档
        $this->command->info('创建 BL07U 光束线专用文档...');
        Document::create([
            'title' => 'BL07U 操作手册',
            'description' => 'BL07U 光束线设备操作规程和注意事项',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => 'BL07U_操作手册.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'dedicated',
            'group_id' => $groups['BL07U']->id,
            'uploaded_by' => $user3->id,
            'conversion_status' => 'pending',
        ]);

        // 8. 创建 BL08U 光束线专用文档
        $this->command->info('创建 BL08U 光束线专用文档...');
        Document::create([
            'title' => 'BL08U 操作手册',
            'description' => 'BL08U 光束线设备操作规程和注意事项',
            'file_path' => 'documents/' . date('Y/m/d') . '/' . fake()->uuid() . '.docx',
            'file_name' => 'BL08U_操作手册.docx',
            'file_size' => fake()->numberBetween(100000, 500000),
            'mime_type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
            'type' => 'dedicated',
            'group_id' => $groups['BL08U']->id,
            'uploaded_by' => $user4->id,
            'conversion_status' => 'pending',
        ]);

        $this->command->info('演示数据生成完成！');
        
        // 9. 创建终端数据
        $this->call(TerminalSeeder::class);
        
        // 10. 创建SOP模板数据
        $this->call(SopTemplateSeeder::class);
        
        $this->command->newLine();
        $this->command->info('=== 生成的数据摘要 ===');
        $this->command->info('用户数量: ' . User::count());
        $this->command->info('分组数量: ' . Group::count());
        $this->command->info('文档数量: ' . Document::count());
        $this->command->info('  - 全局文档: ' . Document::global()->count());
        $this->command->info('  - 专用文档: ' . Document::dedicated()->count());
        $this->command->newLine();
        $this->command->info('=== 测试账号信息 ===');
        $this->command->info('超级管理员: admin@example.com / password (super-admin)');
        $this->command->info('张三（BL02U1）: zhangsan@example.com / password (user)');
        $this->command->info('李四（BL02U1）: lisi@example.com / password (user)');
        $this->command->info('王五（BL07U）: wangwu@example.com / password (admin)');
        $this->command->info('赵六（BL08U）: zhaoliu@example.com / password (user)');
    }
}
