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

use Garden\Cli\Cli;
use Aivec\WordPress\CodeceptDocker\Config;
use Aivec\WordPress\CodeceptDocker\Logger;
use Aivec\WordPress\CodeceptDocker\CLI\Client;
use Aivec\WordPress\CodeceptDocker\CLI\Commands;

require_once 'vendor/autoload.php';

// Define a cli with commands.
$cli = Cli::create()
    ->command('create-config')
        ->description('Creates codecept-docker.json config file in project directory')
        ->opt('namespace:n', 'Prefix for Docker containers', false, 'string')
        ->opt('type:t', 'Project type. Must be one of "plugin", "theme", or "other"', true, 'string')
    ->command('bootstrap')
        ->description(
            "Create Codeception scaffolding if it does not exist already (includes yml config files, test folders, etc.). 
            This command is idempotent and will not overwrite files/folders. It can safely be invoked any number of times"
        )
        ->opt('with-helpers:wh', 'If provided, Helper classes will be generated for each suite', false, 'boolean')
        ->opt('with-sample-tests:wst', 'If provided, sample tests will be generated for each suite', false, 'boolean')
    ->command('up')
        ->description('Creates and/or starts Docker containers.')
    ->command('down')
        ->description('Stops and removes containers')
    ->command('save-snapshot')
        ->description('Saves the WordPress container as an image and exports it as a TAR archive to `tests/_data`')
    ->command('codecept')
        ->description('Calls and passes args to codecept script from within the WordPress Docker container')
    ->command('wp-cli')
        ->description('Runs and passes command/arguments to wp-cli from within the WordPress Docker container');

// Parse and return cli args.
$args = $cli->parse($argv, true);

$conf = null;
if (!in_array($argv[1], ['create-config', 'update'], true)) {
    if (!file_exists(Client::getAbsPath() . '/codecept-docker.json')) {
        Logger::error('codecept-docker.json does not exist in project directory. Create one with "aivec-codecept create-config --type=<type>"');
        exit(1);
    }
    $conf = json_decode(file_get_contents(Client::getAbsPath() . '/codecept-docker.json'), true);
}

switch ($argv[1]) {
    case 'create-config':
        (new Commands\CreateConfig($args))->run();
        break;
    case 'bootstrap':
        (new Commands\Bootstrap($conf, $args))->run();
        break;
    case 'init':
        (new Commands\Init($conf))->run();
        break;
    case 'up':
        (new Commands\Up($conf))->run();
        break;
    case 'start':
        (new Commands\Start($conf))->run();
        break;
    case 'stop':
        (new Commands\Stop($conf))->run();
        break;
    case 'down':
        (new Commands\Down($conf))->run();
        break;
    case 'save-snapshot':
        (new Commands\SaveSnapshot($conf))->run();
        break;
    case 'codecept':
        (new Client(new Config($conf)))->codecept(isset($argv[2]) ? join(' ', array_slice($argv, 2)) : '');
        break;
    case 'wp-cli':
        (new Client(new Config($conf)))->wpcli($argv);
        break;
}
