import CRUDBase from '../CRUDBase.js'

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

        this.endpoint = '/api/v1/ldap/groups'
        this.required_fields = ['name', 'role']
        this.permission_base = 'v1:ldap:groups'

        this.item = 'LDAP Group'
        this.plural = 'LDAP Groups'

        this.listing_definition = {
            columns: [
                {
                    name: 'Group Name',
                    field: 'name',
                },
                {
                    name: 'Role',
                    field: 'role',
                },
                {
                    name: '# of Users',
                    field: 'user_ids',
                    renderer: (user_ids) => Array.isArray(user_ids) ? user_ids.length : 0,
                },
            ],
            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 = {
            // back_action: {
            //    text: 'Back',
            //    action: 'back',
            // },
            fields: [
                {
                    name: 'Group Name',
                    field: 'name',
                    placeholder: 'External App Users',
                    required: true,
                    type: 'text',
                },
                {
                    name: 'Role',
                    field: 'role',
                    placeholder: 'external_app',
                    required: true,
                    type: 'select.dynamic',
                    options: {
                        resource: 'auth/Role',
                        display: 'role',
                        value: 'role',
                    },
                    // options: [
                    //     { value: 1, display: 'One' },
                    //     { value: 2, display: 'Two' },
                    //     { value: 3, display: 'Three' },
                    // ],
                },
                {
                    name: 'Users',
                    field: 'user_ids',
                    placeholder: 'John Doe',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'auth/User',
                        display: (user) => `${user.last_name}, ${user.first_name} (${user.uid})`,
                        value: 'id',
                    },
                },
            ],
        }
    }
}

const ldap_group = new GroupResource()
export { ldap_group }
