import { AXPWidgetsList } from '@acorex/modules/common';
import { createAllQueryView, AXPEntityCommandScope } from '@acorex/platform/common';
import { entityMasterCrudActions, entityMasterRecordActions } from '@acorex/platform/layout/entity';
import { RootConfig } from './acorex-modules-learning-management.mjs';

//#region ----   Imports   ----
//#endregion
//#region ----   Entity Factory   ----
async function factory(injector) {
    const entityDef = {
        module: RootConfig.module.name,
        name: RootConfig.entities.training.name,
        title: RootConfig.entities.training.title,
        icon: RootConfig.entities.training.icon,
        plugins: [
            { name: 'history' },
            { name: 'lock' },
            { name: 'compare' },
            { name: 'clone' },
            { name: 'archive' },
            { name: 'attachments' },
        ],
        formats: {
            individual: RootConfig.entities.training.title,
            plural: RootConfig.entities.training.titlePlural,
            searchResult: {
                title: '{{ course.title }}',
                description: '{{ status }} · {{ startDate | date:"shortDate" }}',
            },
        },
        relatedEntities: [
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.trainingDefinition.name}`,
                title: RootConfig.entities.trainingDefinition.title,
                columns: ['title', 'trainingTypeId', 'courseId'],
                conditions: [
                    {
                        name: 'id',
                        operator: { type: 'equal' },
                        value: '{{ trainingDefinitionId }}',
                    },
                ],
                layout: { type: 'page-detail', position: 'before', order: 0 },
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.enrollment.name}`,
                title: RootConfig.entities.enrollment.titlePlural,
                columns: ['employeeId', 'status'],
                conditions: [
                    {
                        name: 'trainingId',
                        operator: { type: 'equal' },
                        value: '{{ id }}',
                    },
                ],
                actions: [
                    {
                        title: '@learning-management:enrollments.entities.enrollment.actions.create.title',
                        priority: 'primary',
                        type: 'create',
                        scope: AXPEntityCommandScope.TypeLevel,
                        command: {
                            name: 'create-entity',
                            options: {
                                process: {
                                    data: {
                                        trainingId: '{{ context.eval("id") }}',
                                    },
                                },
                            },
                        },
                    },
                ],
            },
            {
                entity: `${RootConfig.module.name}.${RootConfig.entities.attendance.name}`,
                title: RootConfig.entities.attendance.titlePlural,
                columns: ['sessionDate', 'status'],
                conditions: [
                    {
                        name: 'trainingId',
                        operator: { type: 'equal' },
                        value: '{{ id }}',
                    },
                ],
            },
        ],
        groups: [
            { id: 'basic-info', title: '@learning-management:trainings.entities.training.groups.basic-info' },
            { id: 'schedule', title: '@learning-management:trainings.entities.training.groups.schedule' },
            { id: 'logistics', title: '@learning-management:trainings.entities.training.groups.logistics' },
        ],
        properties: [
            {
                name: 'id',
                title: 'ID',
                groupId: 'basic-info',
                schema: {
                    dataType: 'uuid',
                    visible: false,
                    nullable: false,
                    readonly: true,
                    unique: { enabled: true },
                },
            },
            {
                name: 'trainingDefinitionId',
                title: '@learning-management:trainings.entities.training.fields.training-definition-id.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig.module.name}.${RootConfig.entities.trainingDefinition.name}`,
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'trainingDefinition.id' },
                                { source: 'title', target: 'trainingDefinition.title' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'courseId',
                title: '@learning-management:trainings.entities.training.fields.course-id.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig.module.name}.${RootConfig.entities.course.name}`,
                            expose: [
                                { source: 'id', target: 'course.id' },
                                { source: 'title', target: 'course.title' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'trainingTypeId',
                title: '@learning-management:trainings.entities.training.fields.training-type-id.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: `${RootConfig.module.name}.${RootConfig.entities.trainingType.name}`,
                            look: 'select',
                            expose: [
                                { source: 'id', target: 'trainingType.id' },
                                { source: 'name', target: 'trainingType.name' },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'status',
                title: '@learning-management:trainings.entities.training.fields.status.title',
                groupId: 'basic-info',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'string',
                    defaultValue: 'Draft',
                    interface: {
                        type: AXPWidgetsList.Editors.SelectBox,
                        options: {
                            valueField: 'id',
                            dataSource: [
                                { id: 'Draft', title: '@learning-management:trainings.entities.training.fields.status.options.draft' },
                                { id: 'Open', title: '@learning-management:trainings.entities.training.fields.status.options.open' },
                                {
                                    id: 'Closed',
                                    title: '@learning-management:trainings.entities.training.fields.status.options.closed',
                                },
                                {
                                    id: 'Cancelled',
                                    title: '@learning-management:trainings.entities.training.fields.status.options.cancelled',
                                },
                            ],
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'startDate',
                title: '@learning-management:trainings.entities.training.fields.start-date.title',
                groupId: 'schedule',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsList.Editors.DateTimeBox,
                        options: {
                            displayFormat: 'datetime',
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'endDate',
                title: '@learning-management:trainings.entities.training.fields.end-date.title',
                groupId: 'schedule',
                options: {
                    sort: { enabled: true },
                    filter: { advance: { enabled: true }, inline: { enabled: true } },
                },
                schema: {
                    dataType: 'date',
                    interface: {
                        type: AXPWidgetsList.Editors.DateTimeBox,
                        options: {
                            displayFormat: 'datetime',
                        },
                    },
                },
                validations: [{ rule: 'required' }],
            },
            {
                name: 'capacity',
                title: '@learning-management:trainings.entities.training.fields.capacity.title',
                groupId: 'schedule',
                schema: {
                    dataType: 'number',
                    interface: {
                        type: AXPWidgetsList.Editors.NumberBox,
                        options: {
                            minValue: 0,
                            step: 1,
                            placeholder: '@learning-management:trainings.entities.training.fields.capacity.placeholder',
                        },
                    },
                },
            },
            {
                name: 'locationId',
                title: '@learning-management:trainings.entities.training.fields.location-id.title',
                groupId: 'logistics',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'LocationManagement.Location',
                            // displayFormat: '{ title } - { city.title }',
                            expose: [
                                { source: 'id', target: 'location.id' },
                                { source: 'title', target: 'location.title' },
                            ],
                        },
                    },
                },
            },
            {
                name: 'instructorId',
                title: '@learning-management:trainings.entities.training.fields.instructor-id.title',
                groupId: 'logistics',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LookupBox,
                        options: {
                            entity: 'HumanCapitalManagement.Employee',
                            expose: [
                                { source: 'id', target: 'instructor.id' },
                                { source: 'person.displayName', target: 'instructor.displayName' },
                            ],
                        },
                    },
                },
            },
            {
                name: 'notes',
                title: '@learning-management:trainings.entities.training.fields.notes.title',
                groupId: 'logistics',
                schema: {
                    dataType: 'string',
                    interface: {
                        type: AXPWidgetsList.Editors.LargeTextBox,
                        options: {
                            rows: 3,
                            placeholder: '@learning-management:trainings.entities.training.fields.notes.placeholder',
                        },
                    },
                },
            },
        ],
        columns: [
            {
                name: 'trainingDefinitionId',
                title: '@learning-management:trainings.entities.training.fields.training-definition-id.title',
                options: { dataPath: 'trainingDefinition.title' },
            },
            { name: 'courseId', options: { dataPath: 'course.title' } },
            { name: 'trainingTypeId', options: { dataPath: 'trainingType.name' } },
            { name: 'startDate' },
            { name: 'endDate' },
            { name: 'status' },
            { name: 'capacity' },
        ],
        interfaces: {
            master: {
                create: {
                    sections: [
                        { id: 'basic-info', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'schedule', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'logistics', layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'courseId', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'trainingTypeId', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'trainingDefinitionId', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 4, order: 4 } } } },
                        { name: 'startDate', layout: { positions: { lg: { colSpan: 6, order: 5 } } } },
                        { name: 'endDate', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'capacity', layout: { positions: { lg: { colSpan: 4, order: 7 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'instructorId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'notes', layout: { positions: { lg: { colSpan: 12, order: 10 } } } },
                    ],
                },
                update: {
                    sections: [
                        { id: 'basic-info', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'schedule', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'logistics', layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'courseId', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'trainingTypeId', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'trainingDefinitionId', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 4, order: 4 } } } },
                        { name: 'startDate', layout: { positions: { lg: { colSpan: 6, order: 5 } } } },
                        { name: 'endDate', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'capacity', layout: { positions: { lg: { colSpan: 4, order: 7 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'instructorId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'notes', layout: { positions: { lg: { colSpan: 12, order: 10 } } } },
                    ],
                },
                single: {
                    title: '{{ course.title }}',
                    sections: [
                        { id: 'basic-info', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'schedule', layout: { positions: { lg: { colSpan: 12 } } } },
                        { id: 'logistics', layout: { positions: { lg: { colSpan: 12 } } } },
                    ],
                    properties: [
                        { name: 'courseId', layout: { positions: { lg: { colSpan: 6, order: 1 } } } },
                        { name: 'trainingTypeId', layout: { positions: { lg: { colSpan: 6, order: 2 } } } },
                        { name: 'trainingDefinitionId', layout: { positions: { lg: { colSpan: 6, order: 3 } } } },
                        { name: 'status', layout: { positions: { lg: { colSpan: 4, order: 4 } } } },
                        { name: 'startDate', layout: { positions: { lg: { colSpan: 6, order: 5 } } } },
                        { name: 'endDate', layout: { positions: { lg: { colSpan: 6, order: 6 } } } },
                        { name: 'capacity', layout: { positions: { lg: { colSpan: 4, order: 7 } } } },
                        { name: 'locationId', layout: { positions: { lg: { colSpan: 6, order: 8 } } } },
                        { name: 'instructorId', layout: { positions: { lg: { colSpan: 6, order: 9 } } } },
                        { name: 'notes', layout: { positions: { lg: { colSpan: 12, order: 10 } } } },
                    ],
                    actions: [...entityMasterRecordActions()],
                },
                list: {
                    actions: [...entityMasterCrudActions()],
                    views: [createAllQueryView({ sorts: [{ name: 'startDate', dir: 'desc' }] })],
                },
            },
        },
    };
    return entityDef;
}
//#endregion

export { factory };
//# sourceMappingURL=acorex-modules-learning-management-training.entity-DHGSlVR3.mjs.map
