<?php
namespace App\Controllers;

use App\Core\View;
use Throwable;

class BaseController
{
    public function __construct(protected array $config)
    {
    }

    protected function view(string $layout, string $view, array $data = []): string
    {
        return View::page($layout, $view, $data + ['config' => $this->config]);
    }

    protected function samples(): array
    {
        return require BASE_PATH . '/database/sample-data.php';
    }

    protected function logException(Throwable $exception, string $context = 'app'): void
    {
        $logDir = $this->config['paths']['logs'] ?? BASE_PATH . '/storage/logs';

        if (!is_dir($logDir)) {
            @mkdir($logDir, 0775, true);
        }

        if (is_dir($logDir)) {
            $message = '[' . date('c') . '] [' . $context . '] ' . $exception . PHP_EOL;
            error_log($message, 3, $logDir . '/app.log');
        }
    }
}
