<?php

namespace App\Models;

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

class Diagnosa extends Model
{
    use HasFactory;
    protected $primaryKey = 'diagnosaId';
    protected $keyType = 'string';
    protected $fillable = [
        'diagnosaId',
        'userId',
        'penyakitId',
        'probabilitas',
    ];

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

    public function penyakit()
    {
        return $this->belongsTo(Penyakit::class, 'penyakitId');
    }

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