<?php

$this->helper('license')->require();

// Register routes
$this->bindClass('Inbox\\Controller\\Forms', '/inbox/forms');
$this->bindClass('Inbox\\Controller\\Submissions', '/inbox/submissions');
$this->bindClass('Inbox\\Controller\\Inbox', '/inbox');

//events
$this->on('app.layout.init', function() {

    $acl = $this->helper('acl');

    if (!$acl->isAllowed('inbox/manage') && !$acl->isAllowed('inbox/submissions')) {

        // check for any per-form submission permission
        $hasAnyFormAccess = false;
        $forms = $this->dataStorage->find('inbox/forms', ['fields' => ['_id' => 1]])->toArray();

        foreach ($forms as $form) {
            if ($acl->isAllowed("inbox/{$form['_id']}/submissions")) {
                $hasAnyFormAccess = true;
                break;
            }
        }

        if (!$hasAnyFormAccess) return;
    }

    $this->helper('menus')->addLink('modules', [
        'label'  => 'Inbox',
        'icon'   => 'inbox:icon.svg',
        'route'  => '/inbox',
        'active' => false,
        'group'  => 'Addons',
        'prio'   => 1
    ]);
});

// events

$this->on('app.permissions.collect', function($permissions) {

    $permissions['Inbox'] = [
        'component' => 'InboxPermissions',
        'src' => 'inbox:assets/vue-components/inbox-permissions.js',
        'props' => [
            'forms' => $this->dataStorage->find('inbox/forms', [
                'sort' => ['name' => 1],
                'fields' => ['_id' => 1, 'name' => 1, 'color' => 1, 'group' => 1]
            ])->toArray()
        ]
    ];
});

$this->on('app.search', function($search, $findings) {

    $acl = $this->helper('acl');
    $globalAccess = $acl->isAllowed('inbox/submissions') || $acl->isAllowed('inbox/manage');

    if (!$globalAccess) {
        // check for any per-form access
        $hasAny = false;
        $allForms = $this->dataStorage->find('inbox/forms', ['fields' => ['_id' => 1]])->toArray();
        foreach ($allForms as $f) {
            if ($acl->isAllowed("inbox/{$f['_id']}/submissions")) { $hasAny = true; break; }
        }
        if (!$hasAny) return;
    }

    $forms = $this->dataStorage->find('inbox/forms', [
        'filter' => [
            'name' => ['$regex' => $search, '$options' => 'i']
        ],
        'limit' => 5
    ])->toArray();

    foreach ($forms as $form) {

        if (!$globalAccess && !$acl->isAllowed("inbox/{$form['_id']}/submissions")) {
            continue;
        }

        $findings[] = [
            'title' => $form['name'],
            'route' => $this->routeUrl("/inbox/submissions/form/{$form['_id']}"),
            'group' => 'Inbox',
            'icon' => 'inbox:icon.svg'
        ];
    }
});

$this->on('app.dashboard.widgets', function($widgets) {

    $acl = $this->helper('acl');
    $globalAccess = $acl->isAllowed('inbox/manage') || $acl->isAllowed('inbox/submissions');

    $forms = $this->dataStorage->find('inbox/forms', [
        'sort' => ['name' => 1]
    ])->toArray();

    $forms = array_values(array_filter($forms, function($form) use ($acl, $globalAccess) {

        if (!$globalAccess && !$acl->isAllowed("inbox/{$form['_id']}/submissions")) {
            return false;
        }

        $collection = "inbox/form_{$form['_id']}";

        return $this->dataStorage->count($collection);
    }));

    if (!count($forms)) {
        return;
    }

    $widgets[] = [
        'name' => 'dashboard-inbox-widget',
        'area' => 'secondary',
        'component' => 'inbox:assets/vue-components/dashboard-widget.js',
        'data' => compact('forms')
    ];

});
