import CRUDBase from '../CRUDBase.js'

class TokenResource extends CRUDBase {
    constructor() {
        super()
        this.endpoint = '/api/v1/reflect/tokens'
        this.required_fields = ['client_id']
        this.permission_base = 'v1:reflect:tokens'

        this.item = 'API Token'
        this.plural = 'API Tokens'

        this.listing_definition = {
            display: `
        This allows you to create bearer tokens manually to allow for easier testing of the API. Notably, this is meant as a measure for testing and development, not for long term use.
        <br><br>
        If you have an application that needs to regularly interact with the API, set it up as an <a href="/dash/c/listing/oauth/Client">OAuth2 Client</a>. Manually-created tokens expire 7 days after their creation.
        `,
            columns: [
                {
                    name: 'Token',
                    field: 'token',
                },
                {
                    name: 'Client',
                    field: 'client_display',
                },
                {
                    name: 'Expires',
                    field: 'expires',
                },
            ],
            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: 'Client',
                    field: 'client_id',
                    required: true,
                    type: 'select.dynamic',
                    options: {
                        resource: 'oauth/Client',
                        display: 'name',
                        value: 'uuid',
                    },
                },
                {
                    name: 'Bearer Token',
                    field: 'token',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                },
                {
                    name: 'Expires',
                    field: 'expires',
                    type: 'text',
                    readonly: true,
                    hidden: ['insert'],
                },
            ],
        }
    }
}

const reflect_token = new TokenResource()
export { reflect_token }
