import { CompositeRouteGuard, IqserPermissionsGuard, IqserRoutes } from '@iqser/common-ui';
import { RedRoleGuard } from '@users/red-role.guard';
import { EntitiesListingScreenComponent } from './screens/entities-listing/entities-listing-screen.component';
import { PendingChangesGuard } from '@guards/can-deactivate.guard';
import { DefaultColorsScreenComponent } from './screens/default-colors/default-colors-screen.component';
import { UserListingScreenComponent } from './screens/user-listing/user-listing-screen.component';
import { DigitalSignatureScreenComponent } from './screens/digital-signature/digital-signature-screen.component';
import { AuditScreenComponent } from './screens/audit/audit-screen.component';
import { GeneralConfigScreenComponent } from './screens/general-config/general-config-screen.component';
import { BaseAdminScreenComponent } from './base-admin-screen/base-admin-screen.component';
import { BaseDossierTemplateScreenComponent } from './base-dossier-templates-screen/base-dossier-template-screen.component';
import { DossierTemplatesGuard } from '@guards/dossier-templates.guard';
import { DOSSIER_TEMPLATE_ID, ENTITY_TYPE } from '@red/domain';
import { templateExistsWhenEnteringAdmin } from '@guards/dossier-template-exists.guard';
import { entityExistsGuard } from '@guards/entity-exists-guard.service';
import { BaseEntityScreenComponent } from './base-entity-screen/base-entity-screen.component';
import { PermissionsGuard } from '@guards/permissions-guard';
import { Roles } from '@users/roles';
import { IqserAuthGuard } from '@iqser/common-ui/lib/users';
import { AdminDialogService } from './services/admin-dialog.service';
import { AuditService } from './services/audit.service';
import { DigitalSignatureService } from './services/digital-signature.service';
import { RulesService } from './services/rules.service';
import { SmtpConfigService } from './services/smtp-config.service';

const entityRoutes: IqserRoutes = [
    {
        path: '',
        component: BaseDossierTemplateScreenComponent,
        children: [{ path: '', component: EntitiesListingScreenComponent }],
        canActivate: [CompositeRouteGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
    },
    {
        path: `:${ENTITY_TYPE}`,
        component: BaseEntityScreenComponent,
        canActivate: [CompositeRouteGuard, entityExistsGuard()],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard],
        },
        loadChildren: () => import('./screens/entities/entities.routes'),
    },
];

const dossierTemplateIdRoutes: IqserRoutes = [
    { path: '', redirectTo: 'info', pathMatch: 'full' },
    {
        path: 'info',
        component: BaseDossierTemplateScreenComponent,
        children: [
            {
                path: '',
                canDeactivate: [PendingChangesGuard],
                loadComponent: () => import('./screens/info/dossier-template-info-screen/dossier-template-info-screen.component'),
            },
        ],
    },
    {
        path: 'entities',
        children: entityRoutes,
    },
    {
        path: '',
        component: BaseDossierTemplateScreenComponent,
        children: [
            {
                path: 'entity-rules',
                loadComponent: () => import('./screens/rules/rules-screen/rules-screen.component'),
                canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
                canDeactivate: [PendingChangesGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                    permissions: {
                        allow: [Roles.rules.read],
                        redirectTo: 'info',
                    },
                    type: 'ENTITY',
                },
                providers: [RulesService],
            },
            {
                path: 'component-rules',
                loadComponent: () => import('./screens/rules/rules-screen/rules-screen.component'),
                canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
                canDeactivate: [PendingChangesGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                    permissions: {
                        allow: [Roles.rules.read],
                        redirectTo: 'info',
                    },
                    type: 'COMPONENT',
                },
                providers: [RulesService],
            },
            {
                path: 'component-mappings',
                loadComponent: () => import('./screens/component-mappings/component-mappings-screen.component'),
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'components',
                loadComponent: () => import('./screens/component-definitions/component-definitions.component'),
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'file-attributes',
                loadComponent: () => import('./screens/file-attributes-listing/file-attributes-listing-screen.component'),
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'watermarks',
                loadChildren: () => import('./screens/watermark/watermark.routes'),
            },
            {
                path: 'reports',
                loadComponent: () => import('./screens/reports/reports-screen/reports-screen.component'),
                canActivate: [IqserAuthGuard, IqserPermissionsGuard],
                data: {
                    permissions: {
                        allow: [Roles.reportTemplates.read],
                        redirectTo: '/auth-error',
                    },
                },
            },
            {
                path: 'dossier-attributes',
                loadComponent: () => import('./screens/dossier-attributes-listing/dossier-attributes-listing-screen.component'),
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'dossier-states',
                loadComponent: () =>
                    import('./screens/dossier-states-listing/dossier-states-listing-screen/dossier-states-listing-screen.component'),
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'default-colors',
                component: DefaultColorsScreenComponent,
                canActivate: [CompositeRouteGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                },
            },
            {
                path: 'justifications',
                loadComponent: () => import('./screens/justifications/justifications-screen/justifications-screen.component'),
                canActivate: [IqserPermissionsGuard],
                data: {
                    permissions: {
                        allow: [Roles.legalBasis.read],
                        redirectTo: '/auth-error',
                    },
                },
            },
        ],
    },
];

const dossierTemplatesRoutes: IqserRoutes = [
    {
        path: '',
        component: BaseAdminScreenComponent,
        children: [
            {
                path: '',
                loadComponent: () =>
                    import(
                        './screens/dossier-templates-listing/dossier-templates-listing-screen/dossier-templates-listing-screen.component'
                    ),
            },
        ],
        canActivate: [IqserAuthGuard],
    },
    {
        path: `:${DOSSIER_TEMPLATE_ID}`,
        children: dossierTemplateIdRoutes,
        canActivate: [templateExistsWhenEnteringAdmin()],
    },
];

export default [
    { path: '', redirectTo: 'dossier-templates', pathMatch: 'full' },
    {
        path: 'dossier-templates',
        children: dossierTemplatesRoutes,
        canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
        data: {
            routeGuards: [IqserAuthGuard, RedRoleGuard, DossierTemplatesGuard],
            requiredRoles: ['RED_MANAGER', 'RED_ADMIN'],
            permissions: {
                allow: [Roles.templates.read],
                redirectTo: '/',
            },
        },
    },
    {
        path: '',
        component: BaseAdminScreenComponent,
        providers: [AdminDialogService],
        children: [
            {
                path: 'users',
                component: UserListingScreenComponent,
                canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                    permissions: {
                        allow: [Roles.users.read, 'RED_USER_ADMIN'],
                        redirectTo: '/',
                    },
                },
            },
            {
                path: 'dossier-permissions',
                loadComponent: () => import('./screens/permissions/permissions-screen/permissions-screen.component'),
                canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard, PermissionsGuard],
                    permissionsObject: 'Dossier',
                    permissions: {
                        allow: [Roles.manageAclPermissions, 'RED_ADMIN'],
                        redirectTo: '/',
                    },
                },
            },
            {
                path: 'license-info',
                loadChildren: () => import('./screens/license/license.routes'),
                canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
                data: {
                    permissions: {
                        allow: [Roles.license.readReport, 'RED_ADMIN'],
                        redirectTo: '/',
                    },
                },
            },
            {
                path: 'digital-signature',
                component: DigitalSignatureScreenComponent,
                canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
                data: {
                    permissions: {
                        allow: [Roles.digitalSignature.read, 'RED_ADMIN'],
                        redirectTo: '/',
                    },
                },
                providers: [DigitalSignatureService],
            },
            {
                path: 'audit',
                component: AuditScreenComponent,
                canActivate: [IqserAuthGuard, IqserPermissionsGuard, RedRoleGuard],
                data: {
                    permissions: {
                        allow: [Roles.searchAudit, 'RED_ADMIN'],
                        redirectTo: '/',
                    },
                },
                providers: [AuditService],
            },
            {
                path: 'general-config',
                component: GeneralConfigScreenComponent,
                canActivate: [CompositeRouteGuard, IqserPermissionsGuard],
                canDeactivate: [PendingChangesGuard],
                data: {
                    routeGuards: [IqserAuthGuard, RedRoleGuard],
                    permissions: {
                        allow: [Roles.generalConfiguration.read, Roles.smtp.read, 'RED_ADMIN'],
                        redirectTo: '/',
                    },
                },
                providers: [SmtpConfigService],
            },
        ],
    },
] satisfies IqserRoutes;
