<?php

require_once __DIR__ . '/vendor/autoload.php';

use Aoc2025\Days\Day1;
use Aoc2025\Days\Day2;
use Aoc2025\Services\Template;


// First check if this is solution page request.
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (str_starts_with($path, '/solutions')) {
  $path_exploded = explode('/', $path);
  if (count($path_exploded) < 3) {
    header('Location: /');
  }
  $day = str_replace("-", '', $path_exploded[2]);
  $day = ucfirst($day);
  try {
    $code = @file_get_contents(__DIR__ . '/src/Days/' . $day . '.php');
    if (!$code) {
      throw new Exception('File not found');
    }
    else {
      echo "<pre><code class='language-php'>" . htmlspecialchars($code) . "</code></pre>";
    }
  }
  catch (Exception $e) {
    header('Location: /');
  }
  return;
}

if ($path != '/') {
  header('Location: /');
}

// Day 1.
$day_1 = new Day1();
$day_1_answer = $day_1->run();

// Day 2.
$day_2 = new Day2();
$day_2_answer = $day_2->run();


// Render the template.
$template = new Template('main.html');
$substitutions = [
  'day_1_answer' => $day_1_answer['part_1'],
  'day_1_answer_2' => $day_1_answer['part_2'],
  'day_2_answer' => $day_2_answer['part_1'],
  'day_2_answer_2' => $day_2_answer['part_2'],
];
$response = $template->render($substitutions);

echo $response;
