<?php

namespace Database\Seeders;

use App\Models\Department;
use App\Models\DepartmentLeadership;
use Illuminate\Database\Seeder;

class DepartmentLeadershipSeeder extends Seeder
{
    public function run(): void
    {
        Department::all()->each(function (Department $department) {
            $lecturer = $department->lecturers()
                ->whereHas('user.roles', fn ($q) => $q->where('name', 'kaprodi'))
                ->first();

            if (! $lecturer) {
                return;
            }

            DepartmentLeadership::create([
                'department_id' => $department->id,
                'lecturer_id' => $lecturer->id,
                'started_at' => now()->subYear(),
                'ended_at' => null,
            ]);
        });
    }
}
