<?php

namespace App\Http\Controllers\Api;

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

class ProjectController extends Controller
{
    public function index(Request $request)
    {
        $query = Project::where('is_active', true);

        // Featured only
        if ($request->has('featured')) {
            $query->where('is_featured', true);
        }

        // Paginate
        $perPage = $request->get('per_page', 9);
        $projects = $query->orderBy('order')
            ->orderBy('completion_date', 'desc')
            ->paginate($perPage);

        return response()->json([
            'success' => true,
            'data' => $projects
        ]);
    }

    public function show($slug)
    {
        $project = Project::where('slug', $slug)
            ->where('is_active', true)
            ->firstOrFail();

        return response()->json([
            'success' => true,
            'data' => $project
        ]);
    }

    public function featured()
    {
        $projects = Project::featured()
            ->active()
            ->orderBy('order')
            ->limit(6)
            ->get();

        return response()->json([
            'success' => true,
            'data' => $projects
        ]);
    }
}