<?php

namespace App\Listeners;

use App\Events\UserCreated;
use App\Models\Guru;
use App\Models\Siswa;
use Illuminate\Support\Facades\Log;

class LogUserCreated
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(UserCreated $event): void
    {
        $user = $event->user;
        $formData = $event->formData;

        if ($user->role === 'guru') {
            Guru::create([
                'user_id' => $user->id,
                'nama' => $user->name,
                'email' => $user->email,
                'nip' => $formData['nip']
            ]);

            Log::info('Guru created', [
                'user_id' => $user->id,
                'email' => $user->email,
                'nip' => $formData['nip']
            ]);
        }

        if ($user->role === 'siswa') {
            Siswa::create([
                'user_id' => $user->id,
                'nama' => $user->name,
                'email' => $user->email,
                'nis' => $formData['nis'],
                'kelas_id' => $formData['kelas_id']
            ]);

            Log::info('Siswa created', [
                'user_id' => $user->id,
                'email' => $user->email,
                'nis' => $formData['nis'],
                'kelas_id' => $formData['kelas_id']
            ]);
        }
    }
}
