<?php

namespace App\Scopes;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\Json\JsonResource;

abstract class InstancedModel extends Model
{
    protected static function booted()
    {
        static::addGlobalScope(new SiteScope());
    }

    public function toResource(?string $resourceClass = null)  : JsonResource {
        $modelClass = class_basename($this); // z.B. "Event"
        $resourceClass = "App\\Resources\\{$modelClass}Resource";

        if (!class_exists($resourceClass)) {
            throw new \RuntimeException("Resource {$resourceClass} not found.");
        }

        return new $resourceClass($this);
    }
}
