<?php

namespace App\Models;

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

class FormDaily48 extends Model
{
    use HasFactory;

    // Specify the table name
    protected $table = 'form_daily_4_8'; 

    // Mass assignable attributes
    protected $fillable = [
        'id_farm_section',
        'created_by',
        'nama_pekerja',
        'tarikh_lahir',
        'no_ic_passport',
        'jantina',
        'negara_asal',
        'rekod_latihan', // JSON encoded
        'rekod_kesihatan', // JSON encoded
        'nama_operator',
    ];

    // Define the relationship with the Farm Section model
    public function farmSection()
    {
        return $this->belongsTo(Farm_section::class, 'id_farm_section');
    }

    // Define the relationship with the User model (Login)
    public function createdBy()
    {
        return $this->belongsTo(Login::class, 'created_by');
    }

    // Optionally, define accessor for training records to decode JSON
    public function getRekodLatihanAttribute($value)
    {
        return json_decode($value, true);
    }

    // Optionally, define accessor for health records to decode JSON
    public function getRekodKesihatanAttribute($value)
    {
        return json_decode($value, true);
    }

    // Mutators can also be added if needed for encoding the records before saving
}
