<?php

namespace App\Http\Controllers;

use App\Models\SupportCategory;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class SupportCategoryController extends Controller
{
    public function index()
    {
        if (!Schema::hasTable('support_categories')) {
            return collect(SupportCategory::defaultCategories())
                ->map(fn ($category, $index) => [
                    'id' => 'default-' . ($index + 1),
                    'name' => $category['name'],
                    'slug' => Str::slug($category['name']),
                ])
                ->values();
        }

        SupportCategory::ensureDefaults();

        return SupportCategory::query()
            ->where('is_active', true)
            ->orderBy('sort_order')
            ->orderBy('name')
            ->get(['id', 'name', 'slug']);
    }
}
