<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\ContentResource;
use App\Models\Content;
use Illuminate\Http\Request;
use OpenApi\Attributes as OA;

class ContentController extends Controller
{
    #[OA\Get(
        path: "/api/contents",
        tags: ["Content"],
        summary: "List contents",
        parameters: [
            new OA\Parameter(
                name: "category_id",
                in: "query",
                description: "Filter by category ID",
                required: false,
                schema: new OA\Schema(type: "integer")
            ),
            new OA\Parameter(
                name: "is_premium",
                in: "query",
                description: "Filter by premium status (0 or 1)",
                required: false,
                schema: new OA\Schema(type: "integer")
            ),
            new OA\Parameter(
                name: "page",
                in: "query",
                description: "Page number",
                required: false,
                schema: new OA\Schema(type: "integer")
            )
        ]
    )]
    #[OA\Response(
        response: 200,
        description: "Paginated list of contents",
        content: new OA\JsonContent(
            properties: [
                new OA\Property(property: "data", type: "array", items: new OA\Items(type: "object")),
                new OA\Property(property: "meta", type: "object"),
                new OA\Property(property: "links", type: "object")
            ]
        )
    )]
    public function index(Request $request)
    {
        $query = Content::with('category')->where('status', 'published');

        if ($request->has('category_id')) {
            $query->where('category_id', $request->category_id);
        }

        if ($request->has('is_premium')) {
            $query->where('is_premium', $request->boolean('is_premium'));
        }

        $contents = $query->orderBy('published_at', 'desc')->paginate(15);
        return ContentResource::collection($contents);
    }

    #[OA\Get(
        path: "/api/contents/{id}",
        tags: ["Content"],
        summary: "Get content details",
        parameters: [
            new OA\Parameter(
                name: "id",
                in: "path",
                description: "Content ID",
                required: true,
                schema: new OA\Schema(type: "integer")
            )
        ]
    )]
    #[OA\Response(
        response: 200,
        description: "Content details",
        content: new OA\JsonContent(
            properties: [
                new OA\Property(property: "data", type: "object")
            ]
        )
    )]
    #[OA\Response(
        response: 404,
        description: "Content not found"
    )]
    public function show($id)
    {
        $content = Content::with(['category', 'files'])->where('status', 'published')->findOrFail($id);

        // Increment views
        $content->increment('views_count');

        return new ContentResource($content);
    }
}
