<?php

namespace Database\Seeders;

use App\Models\User;
use App\Models\Project;
use App\Models\UserOperation;
use Illuminate\Database\Seeder;

class UserOperationSeeder extends Seeder
{
    public function run(): void
    {
        UserOperation::truncate();

        $userIds = User::pluck('id');
        $projectIds = Project::pluck('id');
        $now = now();

        $userOperations = $userIds->flatMap(function ($userId) use ($projectIds, $now) {
            return $projectIds->map(function ($projectId) use ($userId, $now) {
                return [
                    'user_id' => $userId,
                    'project_id' => $projectId,
                    'designation' => $this->randomScore(),
                    'tech_skills' => $this->randomScore(),
                    'experience' => $this->randomScore(),
                    'interpersonal_relationship' => $this->randomScore(),
                    'power' => $this->randomScore(),
                    'influence' => $this->randomScore(),
                    'created_at' => $now,
                    'updated_at' => $now,
                ];
            });
        });

        // Bulk insert (Laravel will handle big arrays fine, or you can chunk if massive)
        UserOperation::insert($userOperations->toArray());

        $totalRecords = $userIds->count() * $projectIds->count();
        $this->command->info("✅ User operations seeded successfully ({$totalRecords} records).");
    }

    private function randomScore(): float
    {
        return round(mt_rand(100, 500) / 100, 2);
    }
}
