<?php

namespace Database\Seeders;

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

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Seed roles first
        $this->call(RoleSeeder::class);

        // Users to seed
        $users = [
            ['name' => 'Super Admin', 'email' => 'superadmin@culinaryartsacademy.co.zw', 'role' => 'super_admin'],
            ['name' => 'System Admin', 'email' => 'admin@culinaryartsacademy.co.zw', 'role' => 'admin'],
            ['name' => 'Demo Student', 'email' => 'student@example.com', 'role' => 'student'],
            ['name' => 'Demo Lecturer', 'email' => 'lecturer@culinaryartsacademy.co.zw', 'role' => 'lecturer'],
            ['name' => 'Admissions Officer', 'email' => 'admissions@culinaryartsacademy.co.zw', 'role' => 'admissions'],
            ['name' => 'Bursar Officer', 'email' => 'bursar@culinaryartsacademy.co.zw', 'role' => 'bursar'],
            ['name' => 'Library Officer', 'email' => 'library@culinaryartsacademy.co.zw', 'role' => 'library'],
            ['name' => 'Registrar Officer', 'email' => 'registrar@culinaryartsacademy.co.zw', 'role' => 'registrar'],
        ];

        foreach ($users as $data) {
            if (!User::where('email', $data['email'])->exists()) {
                $defaultPassword = strtolower(str_replace('_', '', $data['role'])) . '@2020#';

                $user = User::create([
                    'name' => $data['name'],
                    'email' => $data['email'],
                    'password' => bcrypt($defaultPassword),
                ]);

                $user->assignRole($data['role']);
            }
        }

        // Seed programmes
        $this->call(ProgrammeSeeder::class);
    }
}
