import CRUDBase from './CRUDBase.js'
import { session } from '../service/Session.service.js'

class AppResource extends CRUDBase {
    constructor() {
        super()

        this.endpoint = '/api/v1/applications'
        this.required_fields = ['name', 'identifier']
        this.permission_base = 'v1:applications'

        this.item = 'Application'
        this.plural = 'Applications'

        this.listing_definition = {
            display: `
        An application is anything that can authenticate users against ${session.get('app.name')}. Applications can have any number of associated LDAP clients, SAML service providers, and OAuth2 clients.
        `,
            columns: [
                {
                    name: 'Name',
                    field: 'name',
                },
                {
                    name: 'Identifier',
                    field: 'identifier',
                },
                {
                    name: 'Description',
                    field: 'description',
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Manual Setup',
                    color: 'outline-success',
                },
                {
                    position: 'main',
                    action: 'navigate',
                    text: 'Setup Wizard',
                    color: 'success',
                    page: 'app.setup',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'update',
                    icon: 'fa fa-edit',
                    color: 'primary',
                },
                {
                    type: 'resource',
                    position: 'row',
                    action: 'delete',
                    icon: 'fa fa-times',
                    color: 'danger',
                    confirm: true,
                },
            ],
        }

        this.form_definition = {
            fields: [
                {
                    name: 'Name',
                    field: 'name',
                    placeholder: 'Awesome App',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Identifier',
                    field: 'identifier',
                    placeholder: 'awesome_app',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Description',
                    field: 'description',
                    type: 'textarea',
                },
                {
                    name: 'IAM Target',
                    field: 'id',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                    help: `(LDAP use) Allows restricting users to only those that can access this application. (filter: iamTarget)`,
                },
                {
                    name: 'Associated LDAP Clients',
                    field: 'ldap_client_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'ldap/Client',
                        display: 'name',
                        value: 'id',
                    },
                },
                {
                    name: 'Associated OAuth2 Clients',
                    field: 'oauth_client_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'oauth/Client',
                        display: 'name',
                        value: 'id',
                    },
                },
                {
                    name: 'Associated OpenID Connect Clients',
                    field: 'openid_client_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'openid/Client',
                        display: 'client_name',
                        value: 'id',
                    },
                },
                {
                    name: 'Associated SAML Service Providers',
                    field: 'saml_service_provider_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'saml/Provider',
                        display: 'name',
                        value: 'id',
                    },
                },
            ],
        }
    }
}

const app = new AppResource()
export { app }
