<?php
namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Group;
use App\Models\Role;
use App\Models\Company;
class GroupSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $company = Company::first();
        // Create groups
        $group1 = Group::create(['name' => 'Group 1', 'company_id' => $company->id]);
        $group2 = Group::create(['name' => 'Group 2', 'company_id' => $company->id]);
        $group3 = Group::create(['name' => 'Group 3', 'company_id' => $company->id]);

        // Assign roles to groups
        $this->assignRoles($group1, ['Admin', 'Manager']);
        $this->assignRoles($group2, ['Manager', 'User']);
        $this->assignRoles($group3, ['User']);
    }

    private function assignRoles($group, $roles)
    {
        foreach ($roles as $roleName) {
            $role = Role::where('name', $roleName)->first();
            if ($role) {
                $group->roles()->attach($role);
            }
        }
    }
}
