<?php

namespace Licenzo\Framework\Http;

/**
 * Request Context
 * 
 * Stores request, response, and middleware instances for the current request lifecycle.
 * Used for termination handling and cleanup tasks.
 * 
 * @package Licenzo\Framework\Http
 */
class RequestContext
{
    /**
     * Current request instance
     * 
     * @var Request|null
     */
    protected $request = null;

    /**
     * Current response instance
     * 
     * @var Response|mixed|null
     */
    protected $response = null;

    /**
     * Tracked middleware instances for termination
     * 
     * @var array
     */
    protected array $middleware = [];

    /**
     * Matched route for the current request.
     *
     * Populated by the RouteResolver before middleware runs, so any
     * middleware can read the matched route template (vs. the raw
     * URI, which contains attacker-controlled path segments).
     *
     * @var \Licenzo\Framework\Http\Router\Route|mixed|null
     */
    protected $route = null;

    /**
     * Set the current request
     * 
     * @param Request|mixed $request
     * @return $this
     */
    public function setRequest($request)
    {
        $this->request = $request;
        return $this;
    }

    /**
     * Set the current response
     * 
     * @param Response|mixed $response
     * @return $this
     */
    public function setResponse($response)
    {
        $this->response = $response;
        return $this;
    }

    /**
     * Add a middleware instance to track for termination
     * 
     * @param object $middleware
     * @return $this
     */
    public function addMiddleware($middleware)
    {
        $this->middleware[] = $middleware;
        return $this;
    }

    /**
     * Get the current request
     * 
     * @return Request|mixed|null
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * Get the current response
     * 
     * @return Response|mixed|null
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * Set the matched route for the current request.
     *
     * Called by the RouteResolver before middleware execution begins.
     *
     * @param \Licenzo\Framework\Http\Router\Route|mixed $route
     * @return $this
     */
    public function setRoute($route)
    {
        $this->route = $route;
        return $this;
    }

    /**
     * Get the matched route for the current request.
     *
     * Returns null if called before the resolver has matched a route
     * (e.g. in a non-REST context or during early bootstrap).
     *
     * @return \Licenzo\Framework\Http\Router\Route|mixed|null
     */
    public function getRoute()
    {
        return $this->route;
    }

    /**
     * Get all tracked middleware instances
     * 
     * @return array
     */
    public function getMiddleware(): array
    {
        return $this->middleware;
    }

    /**
     * Check if context has both request and response
     * 
     * @return bool
     */
    public function hasContext(): bool
    {
        return $this->request !== null && $this->response !== null;
    }

    /**
     * Clear all context data (called after termination)
     * 
     * @return void
     */
    public function clear()
    {
        $this->request = null;
        $this->response = null;
        $this->middleware = [];
        $this->route = null;
    }
}

