<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Penyakit extends Model
{
    use HasFactory;
    protected $primaryKey = 'penyakitId';
    protected $keyType = 'string';
    protected $fillable = [
        'penyakitId',
        'penyakit',
        'deskripsi',
        'solusi',
    ];

    public function rule()
    {
        return $this->hasMany(Rule::class, 'penyakitId');
    }

    public function diagnosa()
    {
        return $this->hasMany(Diagnosa::class, 'penyakitId');
    }

    public function informasi()
    {
        return $this->hasMany(Informasi::class, 'penyakitId', 'penyakitId');
    }

    public function generateId()
    {
        $maxId = DB::table('penyakits')->max(DB::raw('CAST(SUBSTRING(penyakitId, 6) AS UNSIGNED)'));
        $maxId = $maxId ?: 0;
        $newId = $maxId + 1;
        $formattedId = sprintf("%02d", $newId);
        return 'PKID-' . $formattedId;
    }
}
