<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\Storage;


class Service extends Model
{
    use HasFactory;

    /**
     * Casting otomatis untuk kolom JSON/tipe lain
     */
    protected $casts = [
        'features'   => 'array',
        'is_active'  => 'boolean',
        'order'      => 'integer',
    ];

    /**
     * Kolom yang bisa diisi mass-assignment
     */
    protected $fillable = [
        'title',
        'description',
        'details',
        'image',
        'icon',
        'order',
        'is_active',
        'features',
    ];

    /**
     * Accessor untuk fitur (repeater)
     * Pastikan selalu balikkan array yang aman
     */
    public function getFeaturesAttribute($value): array
    {
        if (is_array($value)) {
            return $value;
        }

        if (is_string($value) && !empty(trim($value))) {
            $decoded = json_decode($value, true);
            if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
                return $decoded;
            }
        }

        return [];
    }

public function getImageUrlAttribute()
{
    return $this->image 
        ? asset('storage/' . $this->image) 
        : null;
}



}
