<?php

namespace App\Domain\Website\Models;

use App\Domain\Organization\Models\Company;
use App\Support\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/** An editable block of public content, addressed by key (§150). */
class SiteContent extends Model
{
    use HasFactory, HasUuid;

    protected $guarded = ['id', 'uuid'];

    protected function casts(): array
    {
        return ['is_published' => 'boolean'];
    }

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    /** JSON blocks decode; everything else is returned as stored. */
    public function resolvedValue(): mixed
    {
        if ($this->type !== 'json') {
            return $this->value;
        }

        return json_decode((string) $this->value, true) ?? [];
    }
}
