<?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('开始生成演示数据...');

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

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

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

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

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

        // 3. 创建分组
        $this->command->info('创建分组...');
        $techGroup = Group::factory()->create([
            'name' => '技术部',
            'description' => '负责公司技术研发和系统维护工作',
        ]);

        $marketGroup = Group::factory()->create([
            'name' => '市场部',
            'description' => '负责市场推广和品牌建设工作',
        ]);

        $hrGroup = Group::factory()->create([
            'name' => '人力资源部',
            'description' => '负责人力资源管理和员工关系维护',
        ]);

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

        // 张三和李四属于技术部
        $user1->groups()->attach($techGroup->id);
        $user2->groups()->attach($techGroup->id);

        // 王五属于市场部
        $user3->groups()->attach($marketGroup->id);

        // 赵六属于人力资源部
        $user4->groups()->attach($hrGroup->id);

        // 5. 创建全局文档
        $this->command->info('创建全局文档...');
        Document::factory()->global()->create([
            'title' => '公司员工手册',
            'description' => '包含公司规章制度、员工福利、考勤制度等重要信息',
            'file_name' => '员工手册_2024.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->global()->create([
            'title' => '办公室使用规范',
            'description' => '办公室设施使用规范和注意事项',
            'file_name' => '办公室规范.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->global()->create([
            'title' => '公司年度总结报告',
            'description' => '2024年度公司发展总结和2025年规划',
            'file_name' => '年度总结_2024.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->global()->create([
            'title' => '安全管理制度',
            'description' => '公司信息安全和物理安全管理制度',
            'file_name' => '安全管理制度.docx',
            'uploaded_by' => $admin->id,
        ]);

        // 6. 创建技术部专用文档
        $this->command->info('创建技术部专用文档...');
        Document::factory()->dedicated($techGroup->id)->create([
            'title' => '系统架构设计文档',
            'description' => '公司核心系统的架构设计和技术选型说明',
            'file_name' => '系统架构设计.docx',
            'uploaded_by' => $user1->id,
        ]);

        Document::factory()->dedicated($techGroup->id)->create([
            'title' => '代码规范指南',
            'description' => '团队代码编写规范和最佳实践',
            'file_name' => '代码规范.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->dedicated($techGroup->id)->create([
            'title' => '数据库设计文档',
            'description' => '数据库表结构设计和关系说明',
            'file_name' => '数据库设计.docx',
            'uploaded_by' => $user2->id,
        ]);

        Document::factory()->dedicated($techGroup->id)->create([
            'title' => 'API 接口文档',
            'description' => '系统对外提供的 API 接口说明和使用示例',
            'file_name' => 'API接口文档.docx',
            'uploaded_by' => $user1->id,
        ]);

        // 7. 创建市场部专用文档
        $this->command->info('创建市场部专用文档...');
        Document::factory()->dedicated($marketGroup->id)->create([
            'title' => '市场推广方案',
            'description' => '2025年第一季度市场推广计划和预算',
            'file_name' => '市场推广方案_Q1.docx',
            'uploaded_by' => $user3->id,
        ]);

        Document::factory()->dedicated($marketGroup->id)->create([
            'title' => '品牌宣传策略',
            'description' => '品牌定位和宣传渠道策略',
            'file_name' => '品牌宣传策略.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->dedicated($marketGroup->id)->create([
            'title' => '客户调研报告',
            'description' => '目标客户群体调研分析报告',
            'file_name' => '客户调研报告.docx',
            'uploaded_by' => $user3->id,
        ]);

        // 8. 创建人力资源部专用文档
        $this->command->info('创建人力资源部专用文档...');
        Document::factory()->dedicated($hrGroup->id)->create([
            'title' => '招聘流程指南',
            'description' => '公司招聘流程和面试评估标准',
            'file_name' => '招聘流程指南.docx',
            'uploaded_by' => $user4->id,
        ]);

        Document::factory()->dedicated($hrGroup->id)->create([
            'title' => '员工培训计划',
            'description' => '2025年员工培训计划和课程安排',
            'file_name' => '员工培训计划_2025.docx',
            'uploaded_by' => $admin->id,
        ]);

        Document::factory()->dedicated($hrGroup->id)->create([
            'title' => '薪酬福利制度',
            'description' => '公司薪酬结构和福利政策说明',
            'file_name' => '薪酬福利制度.docx',
            'uploaded_by' => $user4->id,
        ]);

        Document::factory()->dedicated($hrGroup->id)->create([
            'title' => '绩效考核标准',
            'description' => '员工绩效考核指标和评估流程',
            'file_name' => '绩效考核标准.docx',
            'uploaded_by' => $admin->id,
        ]);

        $this->command->info('演示数据生成完成！');
        $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');
        $this->command->info('张三（技术部）: zhangsan@example.com / password');
        $this->command->info('李四（技术部）: lisi@example.com / password');
        $this->command->info('王五（市场部）: wangwu@example.com / password');
        $this->command->info('赵六（人力资源部）: zhaoliu@example.com / password');
    }
}
