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

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

        this.endpoint = '/api/v1/saml/providers'
        this.required_fields = ['name', 'acs_url', 'entity_id']
        this.permission_base = 'v1:saml:providers'

        this.item = 'SAML Service Provider'
        this.plural = 'SAML Service Providers'

        this.listing_definition = {
            display: `SAML Service Providers are applications that support external authentication to a SAML Identity Provider. In this case, ${session.get('app.name')} is the identity provider, so these external applications can authenticate against it.
        <br><br>
        To do this, you need to know the SAML service provider's entity ID, assertion consumer service URL, and single-logout URL (if supported).`,
            columns: [
                {
                    name: 'Provider Name',
                    field: 'name',
                },
                {
                    name: 'Entity ID',
                    field: 'entity_id',
                },
                {
                    name: 'Has SLO?',
                    field: 'slo_url',
                    renderer: 'boolean',
                },
                {
                    name: 'ACS URL',
                    field: 'acs_url',
                },
            ],
            actions: [
                {
                    type: 'resource',
                    position: 'main',
                    action: 'insert',
                    text: 'Create New',
                    color: 'success',
                },
                {
                    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: 'Provider Name',
                    field: 'name',
                    placeholder: 'Awesome External App',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Entity ID',
                    field: 'entity_id',
                    placeholder: 'https://my.awesome.app/saml/metadata.xml',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Assertion Consumer Service URL',
                    field: 'acs_url',
                    placeholder: 'https://my.awesome.app/saml/acs',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Single-Logout URL',
                    field: 'slo_url',
                    placeholder: 'https://my.awesome.app/saml/logout',
                    type: 'text',
                },
            ],
        }
    }
}

const saml_provider = new ProviderResource()
export { saml_provider }
