<?php

namespace AleshaCMS\CMS\Content;

use AleshaCMS\Models\CMS\ContentType;
use Illuminate\Support\Facades\Cache;

class ContentTypeManager
{
    public function all(): \Illuminate\Support\Collection
    {
        return Cache::remember('cms.content_types', 3600, function () {
            return ContentType::where('is_active', true)->get();
        });
    }

    public function find(string $slug): ?ContentType
    {
        return $this->all()->firstWhere('slug', $slug);
    }

    public function clearCache(): void
    {
        Cache::forget('cms.content_types');
    }

    public function getFields(string $slug): array
    {
        $type = $this->find($slug);
        return $type ? ($type->fields ?? []) : [];
    }
}
