<?php

namespace Database\Seeders;

use App\Models\Bug;
use App\Models\Project;
use App\Models\ProjectModule;
use App\Models\Task;
use App\Models\User;
use App\Models\ActivityLog;
use App\Models\PointTransaction;
use App\Models\AppNotification;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class ScaleSeeder extends Seeder
{
    public function run(): void
    {
        // 1. Create 1000 Employees
        echo "👷 Creating 1,000 Employees...\n";
        $employees = [];
        $password = Hash::make('password');
        
        for ($i = 1; $i <= 1000; $i++) {
            $employees[] = [
                'name' => "Scale User $i",
                'email' => "scale.user.$i@flowieons.ai",
                'password' => $password,
                'status' => 'active',
                'created_at' => now(),
                'updated_at' => now(),
            ];

            if ($i % 250 === 0) {
                User::insert($employees);
                $employees = [];
                echo "   Progress: $i/1000\n";
            }
        }

        // Assign Roles
        echo "🔗 Assigning Roles...\n";
        $userIds = User::where('email', 'like', 'scale.user.%')->pluck('id');
        $developerRoleId = DB::table('roles')->where('slug', 'developer')->value('id');
        
        $roleAttachments = [];
        foreach ($userIds as $userId) {
            $roleAttachments[] = ['user_id' => $userId, 'role_id' => $developerRoleId];
            if (count($roleAttachments) >= 500) {
                DB::table('role_user')->insert($roleAttachments);
                $roleAttachments = [];
            }
        }
        if (!empty($roleAttachments)) DB::table('role_user')->insert($roleAttachments);

        // 2. Create 500 Projects
        echo "📂 Creating 500 Projects...\n";
        $adminId = User::where('email', 'admin@flowieons.ai')->value('id');
        $projects = [];
        for ($i = 1; $i <= 500; $i++) {
            $projects[] = [
                'name' => "Scale Project $i",
                'description' => "Large scale project description $i",
                'owner_id' => $adminId,
                'status' => 'active',
                'created_at' => now(),
                'updated_at' => now(),
            ];

            if ($i % 100 === 0) {
                Project::insert($projects);
                $projects = [];
                echo "   Progress: $i/500\n";
            }
        }

        $projectIds = Project::pluck('id');

        // 3. Create Modules (3 per project)
        echo "🧩 Creating 1,500 Modules...\n";
        $modules = [];
        foreach ($projectIds as $projectId) {
            for ($m = 1; $m <= 3; $m++) {
                $modules[] = [
                    'project_id' => $projectId,
                    'name' => "Module $m",
                    'description' => "Module description $m",
                    'created_at' => now(),
                    'updated_at' => now(),
                ];
            }
            if (count($modules) >= 500) {
                ProjectModule::insert($modules);
                $modules = [];
            }
        }
        if (!empty($modules)) ProjectModule::insert($modules);

        $moduleIds = ProjectModule::pluck('id');

        // 4. Create 25,000 Tasks
        echo "📋 Creating 25,000 Tasks...\n";
        $tasks = [];
        for ($i = 1; $i <= 25000; $i++) {
            $tasks[] = [
                'project_id' => $projectIds->random(),
                'project_module_id' => $moduleIds->random(),
                'task_status_id' => rand(1, 4),
                'title' => "Scale Task $i",
                'description' => "Scale task description $i",
                'priority' => ['low', 'medium', 'high', 'critical'][rand(0, 3)],
                'created_by' => $adminId,
                'created_at' => now(),
                'updated_at' => now(),
            ];

            if ($i % 2500 === 0) {
                Task::insert($tasks);
                $tasks = [];
                echo "   Progress: $i/25000\n";
            }
        }

        $taskIds = Task::pluck('id');

        // 5. Create 20,000 Bugs
        echo "🐛 Creating 20,000 Bugs...\n";
        $bugs = [];
        for ($i = 1; $i <= 20000; $i++) {
            $bugs[] = [
                'project_id' => $projectIds->random(),
                'module_id' => $moduleIds->random(),
                'reported_by' => $userIds->random(),
                'assigned_to' => $userIds->random(),
                'title' => "Scale Bug $i",
                'description' => "Scale bug description $i",
                'severity' => ['low', 'medium', 'high', 'critical'][rand(0, 3)],
                'status' => 'open',
                'created_at' => now(),
                'updated_at' => now(),
            ];

            if ($i % 2000 === 0) {
                Bug::insert($bugs);
                $bugs = [];
                echo "   Progress: $i/20000\n";
            }
        }

        // 6. Create 100,000 Activity Logs
        echo "📜 Creating 100,000 Activity Logs...\n";
        $logs = [];
        $now = now();
        for ($i = 1; $i <= 100000; $i++) {
            $logs[] = [
                'user_id' => $userIds->random(),
                'action' => 'update',
                'module' => 'task',
                'description' => 'System scaled activity log entry',
                'metadata' => json_encode(['task_id' => rand(1, 25000)]),
                'created_at' => $now->copy()->subMinutes(rand(1, 10000)),
                'updated_at' => $now,
            ];

            if ($i % 2500 === 0) {
                DB::table('activity_logs')->insert($logs);
                $logs = [];
                echo "   Progress: $i/100000\n";
            }
        }

        // 7. Create 250,000 Point Transactions
        echo "💰 Creating 250,000 Point Transactions...\n";
        $transactions = [];
        for ($i = 1; $i <= 250000; $i++) {
            $transactions[] = [
                'user_id' => $userIds->random(),
                'points' => rand(1, 50),
                'type' => 'credit',
                'source' => 'task_completed',
                'created_at' => $now->copy()->subMinutes(rand(1, 10000)),
                'updated_at' => $now,
            ];

            if ($i % 2500 === 0) {
                DB::table('point_transactions')->insert($transactions);
                $transactions = [];
                if ($i % 25000 === 0) echo "   Progress: $i/250000\n";
            }
        }

        echo "🚀 Scale Seeding Complete.\n";
    }
}
