<?php
/// file: app/Controllers/ProjectController.php

declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Request;
use App\Core\Response;
use App\Helpers\Seo;
use App\Models\Project;
use App\Models\ProjectMedia;

final class ProjectController extends Controller
{
    public function show(Request $request): Response
    {
        $slug = $request->param('slug') ?? '';
        $project = Project::findBySlug($slug);
        if ($project === null) {
            return $this->app->make(\App\Controllers\ErrorController::class)->notFound($request);
        }
        $media = ProjectMedia::forProject((int) $project['id']);
        $related = Project::related((int) $project['id'], (string) ($project['category'] ?? ''));
        $next = Project::nextProject((int) $project['id']);
        $title = (string) $project['title'] . ' — ' . $this->app->config('app.name');
        $html = $this->view('work/show', [
            'meta' => Seo::page('work', [
                'title' => $title,
                'description' => (string) ($project['summary'] ?? ''),
                'og_title' => (string) $project['title'],
                'og_description' => (string) ($project['summary'] ?? ''),
                'og_image' => (string) ($project['cover_path'] ?? ''),
            ]),
            'page' => 'work',
            'project' => $project,
            'media' => $media,
            'related' => $related,
            'nextProject' => $next,
        ], 'layouts/base');
        return Response::html($html);
    }
}
