<?php

namespace Signteer\Web\Install\Page;

use Plainwork\Core\App;
use Plainwork\Database\Migrator;
use Plainwork\Http\Request;
use Plainwork\Http\Redirect;
use Plainwork\Ui\Html;

class InstallPage
{
    public $app = App::class;
    public $migrator = Migrator::class;

    public function __invoke(Request $request)
    {
        $html = new Html();

        $info = $html->dl();
        $info->add('__Plugin name__', $this->app->name());
        $info->add('__Version__', $this->app->version());

        $form = $html->form();
        $form->addButton($html->button('__Click to install__'));

        $vs = $form->grab($request);
        if ($vs && $form->isValid()) {
            $this->migrator->up();

            return new Redirect('');
        }

        $ret = $html->blockList()
            ->add($html->el('h1', '__Installation__'))
            ->add($info)
            ->add($form)
            ;

        $ret = $html->main($ret);

        return $ret;
    }
}
