<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class Guru extends Model
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'user_id',
        'nama',
        'code_guru',
        'alamat',
        'tanggal_lahir',
        'jenis_kelamin',
        'foto'
    ];

    protected $appends = ['foto_url'];

    public function getFotoUrlAttribute()
    {
        if ($this->foto) {
            return asset('storage/' . $this->foto);
        }
        return null;
    }

    // app/Models/Guru.php
    public static function generateCodeGuru()
    {
        $lastGuru = self::orderBy('id', 'desc')->first();

        if ($lastGuru) {
            $lastKode = intval($lastGuru->code_guru); // ganti jadi code_guru
            return str_pad($lastKode + 1, 4, '0', STR_PAD_LEFT);
        }

        return '0001'; // mulai dari 0001
    }


    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function kelas()
    {
        return $this->hasMany(Kelas::class, 'wali_kelas_id');
    }
}
