#!/usr/bin/env php
<?php

/*
 * This file is part of php-restcord.
 *
 * (c) Aaron Scherer <aequasi@gmail.com>
 *
 * This source file is subject to the license that is bundled
 * with this source code in the file LICENSE
 */

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

use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

$loader = new Twig_Loader_Filesystem(__DIR__.'/../src/Resources/');
$twig   = new Twig_Environment($loader, ['debug' => true]);
$twig->addExtension(new Twig_Extension_Debug());

function recursiveRemoveDirectory($directory)
{
    foreach (glob("{$directory}/*") as $file) {
        if (is_dir($file)) {
            recursiveRemoveDirectory($file);
        } else {
            unlink($file);
        }
    }
    rmdir($directory);
}

/** @noinspection PhpUnhandledExceptionInspection */
(new Application('Build Docs', '1.0.0'))
    ->register('buildDocs')
    ->addArgument('version', InputArgument::REQUIRED, 'Version to build')
    ->setCode(
        function (InputInterface $input, OutputInterface $output) use ($twig) {
            $style = new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
            $style->title("Building Docs for: ".$input->getArgument('version'));

            $path = __DIR__.'/../docs/_docs';
            recursiveRemoveDirectory($path);
            mkdir($path, 02775, true);
            $path = realpath($path);

            $definition = \GuzzleHttp\json_decode(
                file_get_contents(
                    __DIR__.'/../src/Resources/service_description-v'.$input->getArgument('version').'.json'
                ),
                true
            );

            foreach ($definition['operations'] as $category => $operations) {
                $i = 1;
                foreach ($operations as $operation => $config) {
                    $filePath = $path.'/'.ucwords($category).'/';
                    if (!file_exists($filePath)) {
                        mkdir($filePath, 02775, true);
                    }

                    try {
                        $markdown = $twig->render(
                            'operation.md.twig',
                            [
                                'category'  => $category,
                                'operation' => $operation,
                                'config'    => $config,
                                'order'     => $i
                            ]
                        );
                    } catch (\Exception $e) {
                        throw $e;
                    }

                    file_put_contents($filePath.str_replace('/', ' or ', $config['name']).'.md', $markdown);
                    $i++;
                }
            }

            $style->success('Finished. Docs built in: '.realpath($path));
        }
    )
    ->getApplication()
    ->setDefaultCommand('buildDocs', true)
    ->run();
