<?php

namespace Modules\PrintSuppliers\Models;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    protected $table = 'printing_profile';
    public $timestamps = true;

    protected $fillable = [
        'product_id',
        'is_template_product',
        'product_sku_id',
        'blueprint_id',
        'print_provider_id',
        'variant_id',
        'print_areas',
        'options',
        'position',
        'type'
    ];

    public function getPrintAreasAttribute() {
        return $this->_decorJSONField($this->attributes['print_areas']);
    }

    public function getOptionsAttribute() {
        return $this->_decorJSONField($this->attributes['options']);
    }

    public function getPositionAttribute() {
        return $this->_decorJSONField($this->attributes['position']);
    }

    private function _decorJSONField($string) {
        if (!empty($string)) {
            try {
                return json_decode($string, true);
            } catch (\Exception $ex) {
                return $string;
            }
        }  
        return null;
    }
}
