<?php

namespace lib\Core;

use lib\Core\Logger;
use lib\Core\Http\Redirect;
use lib\Core\Http\Request;
use lib\Core\Http\Session;
use lib\Core\Http\Url;
use lib\Core\Http\Response;

abstract class Controller
{
    protected ?View $_view = null;
    protected ?Url $url = null;
    protected ?Request $request = null;
    protected ?Session $session = null;
    protected ?Redirect $redirect = null;
    protected ?Logger $logger = null;
    protected function __construct(array $params = [])
    {
        if (APP_DEBUG) {
            error_reporting(E_ALL);
            ini_set('display_errors', '1');
        } else {
            error_reporting(0);
            ini_set('display_errors', '0');
        }
        $this->url = new Url();
        $this->request = new Request($params);
        $this->session = new Session();
        $this->redirect = new Redirect($this->session);
        $this->logger = new Logger();
        $this->_view = new View();
    } //
    final public function __get($name): mixed
    {
        return $this->_view->$name;
    }
    final public function __set($name, $value): void
    {
        $this->_view->$name = $value;
    }
    final public function __call($name, $arguments)
    {
        if (method_exists($this, $name)) {
            return call_user_func_array([$this, $name], $arguments);
        }
        throw new \BadMethodCallException("Method {$name} does not exist.");
    } //
    final public function setLayout(string $file): void
    {
        $this->_view->setLayout($file);
    }
    final public function setPath(string $path): void
    {
        $this->_view->setPath($path);
    }
    public function render(string $file): Response
    {
        // if (empty($file)) {
        //     throw new \InvalidArgumentException("File name cannot be empty.");
        // }
        // $this->_view->setFile($file);
        // $this->_view->setLayout($this->layout);
        // $this->_view->setPath($this->path);
        // $this->_view->setParams($this->params);
        // $this->_view->setUrl($this->url);
        // $this->_view->setRequest($this->request);
        // $this->_view->setSession($this->session);
        // $this->_view->setRedirect($this->redirect);
        // $this->_view->setLogger($this->logger);
        return new Response($this->_view->render($file), 200, ['Content-Type' => 'text/html']);
    }
} //Class
