<?php

declare(strict_types=1);

namespace MauticPlugin\DealFlowBundle\Security\Permissions;

use Mautic\CoreBundle\Helper\CoreParametersHelper;
use Mautic\CoreBundle\Security\Permissions\AbstractPermissions;
use Symfony\Component\Form\FormBuilderInterface;

final class DealFlowPermissions extends AbstractPermissions
{
    public const DEALS_VIEW_OWN = 'dealflow:deals:viewown';
    public const DEALS_VIEW_OTHER = 'dealflow:deals:viewother';
    public const DEALS_CREATE = 'dealflow:deals:create';
    public const DEALS_EDIT_OWN = 'dealflow:deals:editown';
    public const DEALS_EDIT_OTHER = 'dealflow:deals:editother';
    public const DEALS_DELETE_OWN = 'dealflow:deals:deleteown';
    public const DEALS_DELETE_OTHER = 'dealflow:deals:deleteother';
    public const REPORTS_VIEW_OWN = 'dealflow:reports:viewown';
    public const REPORTS_VIEW_OTHER = 'dealflow:reports:viewother';
    public const SETTINGS_VIEW_OWN = 'dealflow:settings:viewown';
    public const SETTINGS_VIEW_OTHER = 'dealflow:settings:viewother';
    public const SETTINGS_EDIT_OWN = 'dealflow:settings:editown';
    public const SETTINGS_EDIT_OTHER = 'dealflow:settings:editother';

    public function __construct(CoreParametersHelper $coreParametersHelper)
    {
        parent::__construct($coreParametersHelper->all());
    }

    public function definePermissions(): void
    {
        $this->addExtendedPermissions(['deals', 'reports', 'settings'], false);
        $this->addCustomPermission('notes', ['create' => 32, 'full' => 1024]);
        $this->addCustomPermission('tasks', ['create' => 32, 'full' => 1024]);
        $this->addCustomPermission('attachments', ['upload' => 32, 'full' => 1024]);
    }

    public function getName(): string
    {
        return 'dealflow';
    }

    public function buildForm(FormBuilderInterface &$builder, array $options, array $data): void
    {
        $this->addExtendedFormFields('dealflow', 'deals', $builder, $data, false);
        $this->addExtendedFormFields('dealflow', 'reports', $builder, $data, false);
        $this->addExtendedFormFields('dealflow', 'settings', $builder, $data, false);
        $this->addCustomFormFields(
            'dealflow',
            'notes',
            $builder,
            'mautic.dealflow.permissions.notes',
            [
                'mautic.core.permissions.create' => 'create',
                'mautic.core.permissions.full' => 'full',
            ],
            $data
        );
        $this->addCustomFormFields(
            'dealflow',
            'tasks',
            $builder,
            'mautic.dealflow.permissions.tasks',
            [
                'mautic.core.permissions.create' => 'create',
                'mautic.core.permissions.full' => 'full',
            ],
            $data
        );
        $this->addCustomFormFields(
            'dealflow',
            'attachments',
            $builder,
            'mautic.dealflow.permissions.attachments',
            [
                'mautic.dealflow.permissions.upload' => 'upload',
                'mautic.core.permissions.full' => 'full',
            ],
            $data
        );
    }
}
