<?php

namespace App\Models;

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

class FinanceCostEntry extends Model
{
    use HasFactory;

    protected $table = 'finance_cost_entries';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $fillable = [
        'id',
        'user_id',
        'date',
        'category',
        'subcategory',
        'amount_crc',
        'quantity',
        'unit',
        'unit_cost_crc',
        'source',
        'ref_json',
        'scope_json',
        'notes',
        'created_at_client',
    ];

    protected $casts = [
        'date' => 'date:Y-m-d',
        'amount_crc' => 'float',
        'quantity' => 'float',
        'unit_cost_crc' => 'float',
        'ref_json' => 'array',
        'scope_json' => 'array',
        'created_at_client' => 'datetime',
    ];

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

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

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

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