<?php
/// file: app/Controllers/Api/ProjectFilterController.php

declare(strict_types=1);

namespace App\Controllers\Api;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Models\Project;

final class ProjectFilterController extends Controller
{
    public function filter(Request $request): Response
    {
        $cat = $request->input('category');
        if ($cat === null && $request->method() === 'POST') {
            $body = $request->jsonBody();
            if (is_array($body) && isset($body['category'])) {
                $cat = is_string($body['category']) ? $body['category'] : null;
            }
        }
        $projects = Project::filterByCategory($cat);
        $out = [];
        foreach ($projects as $p) {
            $out[] = [
                'id' => (int) $p['id'],
                'title' => $p['title'],
                'slug' => $p['slug'],
                'category' => $p['category'],
                'year' => (int) $p['year'],
                'cover_path' => $p['cover_path'],
                'cover_type' => $p['cover_type'],
                'summary' => $p['summary'],
            ];
        }
        return $this->json(['ok' => true, 'projects' => $out]);
    }
}
