<?php

namespace App\Models;

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

class FinanceSalesEntry extends Model
{
    use HasFactory;

    protected $table = 'finance_sales_entries';

    public $incrementing = false;

    protected $keyType = 'string';

    protected $fillable = [
        'id',
        'user_id',
        'date',
        'kg_sold',
        'price_crc_per_kg',
        'total_crc',
        'customer_name',
        'scope_json',
        'notes',
        'created_at_client',
        'updated_at_client',
    ];

    protected $casts = [
        'date' => 'date:Y-m-d',
        'kg_sold' => 'float',
        'price_crc_per_kg' => 'float',
        'total_crc' => 'float',
        'scope_json' => 'array',
        'created_at_client' => 'datetime',
        'updated_at_client' => 'datetime',
    ];

    protected $hidden = [
        'scope_json',
    ];

    protected $appends = [
        'scope',
    ];

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