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

class GroupResource extends CRUDBase {
    endpoint = '/api/v1/auth/groups'
    required_fields = ['name']
    permission_base = 'v1:auth:groups'

    item = 'Group'
    plural = 'Groups'

    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,
            },
        ],
    }

    form_definition = {
        fields: [
            {
                name: 'Name',
                field: 'name',
                placeholder: 'Some Cool Users',
                required: true,
                type: 'text',
            },
            {
                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 }
