<?php

namespace App\Models;

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

class FinanceJournalEntry extends Model
{
    use HasFactory;

    protected $table = 'finance_journal_entries';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $fillable = [
        'id',
        'user_id',
        'date',
        'memo',
        'status',
        'lines_json',
        'ref_json',
        'created_at_client',
        'updated_at_client',
    ];

    protected $casts = [
        'date' => 'date:Y-m-d',
        'lines_json' => 'array',
        'ref_json' => 'array',
        'created_at_client' => 'datetime',
        'updated_at_client' => 'datetime',
    ];

    protected $hidden = [
        'lines_json',
        'ref_json',
    ];

    protected $appends = [
        'lines',
        'ref',
    ];

    public function getLinesAttribute(): array
    {
        return is_array($this->lines_json) ? $this->lines_json : [];
    }

    public function getRefAttribute(): array
    {
        return is_array($this->ref_json) ? $this->ref_json : [];
    }
}
