<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Collection;
use Illuminate\Http\Request;

class CollectionController extends Controller
{
    /**
     * List collections for the current project with minimal meta.
     * Route: GET /api/collections
     */
    public function index(Request $request)
    {
        $project = $request->attributes->get('project');

        $collections = Collection::where('project_id', $project->id)
            ->orderBy('order')
            ->get();

        return \App\Http\Resources\CollectionResource::collection($collections);
    }

    /**
     * Return a single collection definition (including its fields).
     * Route: GET /api/collections/{collection}
     */
    public function show(Request $request, string $collection)
    {
        $project = $request->attributes->get('project');

        $collection = Collection::with('fields')
            ->where('project_id', $project->id)
            ->where('slug', $collection)
            ->first();

        if (!$collection) {
            return response()->json(['message' => 'Collection not found.'], 404);
        }

        return new \App\Http\Resources\CollectionResource($collection);
    }
} 