<?php

declare(strict_types=1);

namespace App\Controllers;

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;
use App\Repositories\PostRepositoryInterface as PostRepository;
use App\Requests\PostRequest;
use App\Models\Post;

/**
 * Contrôleur pour les posts.
 */
class PostController
{
    private Twig $view;
    private PostRepository $repo;

    public function __construct(Twig $view, PostRepository $repo)
    {
        $this->view = $view;
        $this->repo  = $repo;
    }

    public function index(Request $req, Response $res): Response
    {
        $posts = $this->repo->allDesc(); // Post[]
        return $this->view->render($res, 'pages/home.twig', ['posts' => $posts]);
    }

    public function admin(Request $req, Response $res): Response
    {
        $posts = $this->repo->allDesc();
        return $this->view->render($res, 'pages/admin.twig', ['posts' => $posts]);
    }

    /**
     * Formulaire de création / édition.
     *
     * @param Request $req
     * @param Response $res
     * @param array $args
     * @return Response
     */
    public function form(Request $req, Response $res, array $args): Response
    {
        $id = (int)($args['id'] ?? 0);
        $post = $id ? $this->repo->find($id) : null;

        // Si id fourni mais post introuvable -> 404
        if ($id > 0 && $post === null) {
            $res->getBody()->write('Article non trouvé');
            return $res->withStatus(404);
        }

        // Twig peut accéder aux getters (post.title, post.content)
        $action = $id ? "/admin/edit/{$id}" : "/admin/create";
        return $this->view->render($res, 'pages/post_form.twig', ['post' => $post, 'action' => $action]);
    }

    public function create(Request $req, Response $res): Response
    {
        $postRequest = PostRequest::fromArray($req->getParsedBody());
        if (! $postRequest->isValid()) {
            return $res->withHeader('Location', '/admin')->withStatus(302);
        }

        // Utilisation du helper toModel() pour construire l'entité Post
        $post = $postRequest->toModel(0);
        $this->repo->create($post);
        return $res->withHeader('Location', '/admin')->withStatus(302);
    }

    public function update(Request $req, Response $res, array $args): Response
    {
        $id = (int)$args['id'];
        $existing = $this->repo->find($id);
        if ($existing === null) {
            $res->getBody()->write('Article non trouvé');
            return $res->withStatus(404);
        }

        $postRequest = PostRequest::fromArray($req->getParsedBody());
        if (! $postRequest->isValid()) {
            return $res->withHeader('Location', '/admin')->withStatus(302);
        }

        $post = $postRequest->toModel($id);
        $this->repo->update($id, $post);
        return $res->withHeader('Location', '/admin')->withStatus(302);
    }

    public function delete(Request $req, Response $res, array $args): Response
    {
        $id = (int)$args['id'];
        $existing = $this->repo->find($id);
        if ($existing === null) {
            $res->getBody()->write('Article non trouvé');
            return $res->withStatus(404);
        }

        $this->repo->delete($id);
        return $res->withHeader('Location', '/admin')->withStatus(302);
    }
}
