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

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

        this.endpoint = '/api/v1/auth/groups'
        this.required_fields = ['name']
        this.permission_base = 'v1:auth:groups'

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

        this.listing_definition = {
            display: `
            In ${session.get('app.name')}, groups are simply a tool for organizing users and assigning permissions and access in bulk. After creating and assigning users to a group, you can manage permissions for that group, and its policies will be applied to all users in that group.
            `,
            columns: [
                {
                    name: 'Name',
                    field: 'name',
                },
                {
                    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 = {
            fields: [
                {
                    name: 'Name',
                    field: 'name',
                    placeholder: 'Some Cool Users',
                    required: true,
                    type: 'text',
                },
                /*{
                    name: 'Superuser equivalent?',
                    field: 'grants_sudo',
                    type: 'select',
                    options: [
                        {display: 'Yes', value: true},
                        {display: 'No', value: false},
                    ],
                },*/
                {
                    name: 'Users',
                    field: 'user_ids',
                    type: 'select.dynamic.multiple',
                    options: {
                        resource: 'auth/User',
                        display: (user) => `${user.last_name}, ${user.first_name} (${user.uid})`,
                        value: 'id',
                    },
                },
            ],
        }
    }
}

const auth_group = new GroupResource()
export { auth_group }
