<?php

namespace Database\Seeders;

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

class ProjectAssignmentSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $admin = User::where('role', 'admin')->first();
        $moderator = User::where('role', 'moderator')->first();
        $onSite = User::where('role', 'on_site_team')->first();
        $design = User::where('role', 'design_team')->first();
        $consultant = User::where('role', 'project_consultant')->first();
        $client = User::where('role', 'client_team')->first();

        $projects = Project::orderBy('id')->get();

        foreach ($projects as $index => $project) {
            $assignmentPool = [
                [$admin, 'admin_owner'],
                [$moderator, 'coordinator'],
            ];

            if ($index % 2 === 0) {
                $assignmentPool[] = [$onSite, 'site_supervisor'];
                $assignmentPool[] = [$consultant, 'consultant'];
            } else {
                $assignmentPool[] = [$design, 'design_lead'];
                $assignmentPool[] = [$client, 'client_rep'];
            }

            foreach ($assignmentPool as [$user, $assignedRole]) {
                if (!$user) {
                    continue;
                }

                $project->assignedUsers()->syncWithoutDetaching([
                    $user->id => [
                        'assigned_role' => $assignedRole,
                        'assigned_by_id' => $admin?->id,
                    ],
                ]);
            }
        }
    }
}
