<?php

namespace App\Models;

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

class Gejala extends Model
{
    use HasFactory;
    protected $primaryKey = 'gejalaId';
    protected $keyType = 'string';
    protected $fillable = [
        'gejalaId',
        'gejala',
    ];

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

    public function diagnosaDetail()
    {
        return $this->hasMany(DiagnosaDetail::class, 'gejalaId', 'gejalaId');
    }

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