<?php

namespace Database\Seeders;

use App\Models\Branch;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class UserSeeder extends Seeder
{
    public function run(): void
    {
        $main = Branch::where('code', 'MAIN')->first();
        $girls = Branch::where('code', 'GIRL')->first();

        // Super Admin — not pinned to any single branch (sees all).
        $this->make('Super Administrator', 'superadmin@school.test', User::SUPER_ADMIN, null, 'EMP-0001', 'System Owner');

        // One representative account per role so RBAC can be demoed.
        $this->make('School Admin', 'admin@school.test', 'School Admin', $main, 'EMP-0002', 'Administrator');
        $this->make('Principal Main', 'principal@school.test', 'Principal', $main, 'EMP-0003', 'Principal');
        $this->make('Vice Principal', 'viceprincipal@school.test', 'Vice Principal', $main, 'EMP-0004', 'Vice Principal');
        $this->make('Head Accountant', 'accountant@school.test', 'Accountant', $main, 'EMP-0005', 'Accountant');
        $this->make('Front Desk', 'reception@school.test', 'Receptionist', $main, 'EMP-0006', 'Receptionist');
        $this->make('HR Manager', 'hr@school.test', 'HR Manager', $main, 'EMP-0007', 'HR Manager');
        $this->make('Teacher One', 'teacher@school.test', 'Teacher', $girls, 'EMP-0008', 'Senior Teacher');
    }

    private function make(string $name, string $email, string $role, ?Branch $branch, string $code, string $designation): void
    {
        $user = User::firstOrCreate(
            ['email' => $email],
            [
                'name' => $name,
                'password' => Hash::make('password'),
                'branch_id' => $branch?->id,
                'employee_code' => $code,
                'designation' => $designation,
                'is_active' => true,
                'email_verified_at' => now(),
            ]
        );

        $user->syncRoles([$role]);
    }
}
