import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from '@shared/auth/auth.guard';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { NgModule } from '@angular/core';
import { RbacPolicyEditComponent } from './rbac-policy-edit/rbac-policy-edit.component';
import { RbacPolicyResolver } from '@fleio-api/openstack/rbac-policy/rbac-policy.resolver';
import { RbacPolicyCreateOptionsResolver } from '@fleio-api/openstack/rbac-policy/rbac-policy-create-options.resolver';
import { RbacPolicyPermissionsResolver } from '@fleio-api/openstack/rbac-policy/rbac-policy-permissions.resolver';
import { RbacPolicyDetailsComponent } from './rbac-policy-details/rbac-policy-details.component';
import { RbacPolicyCreateComponent } from './rbac-policy-create/rbac-policy-create.component';
import { RbacPolicyListComponent } from './rbac-policy-list/rbac-policy-list.component';
import { RbacPoliciesListResolver } from '@fleio-api/openstack/rbac-policy/rbac-policies-list.resolver';

const routes: Routes = [
  {
    path: '',
    component: RbacPolicyListComponent,
    resolve: {
      rbac_policies: RbacPoliciesListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.rbac_policies',
        getTitleDisplay: () => $localize`RBAC policies`,
        search: {
          show: true,
          placeholder: $localize`Search RBAC policies ...`,
        },
        subheader: {
          objectNamePlural: $localize`RBAC policies`,
          objectName: $localize`RBAC policy`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.rbac_policies;
          }
        },
        ordering: {
          default: {
            display: $localize`ID`,
            field: 'id',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`ID`,
              field: 'id',
            },
            {
              display: $localize`Project`,
              field: 'project',
            },
            {
              display: $localize`Target project`,
              field: 'target_project',
            },
            {
              display: $localize`Object type`,
              field: 'object_type',
            },
            {
              display: $localize`Object ID`,
              field: 'object_id',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'project__project_id',
              display: $localize`Project`,
              type: FilterTypes.CustomModel,
              items: 'projects',
              itemsDisplayField: 'project_id',
              itemsDisplaySecondaryField: 'name',
            },
            {
              display: $localize`Client`,
              field: 'project__service__client',
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'display'
            },
            {
              field: 'target_project',
              display: $localize`Target project`,
              type: FilterTypes.CustomModel,
              items: 'projects',
              itemsDisplayField: 'project_id',
              itemsDisplaySecondaryField: 'name',
            },
            {
              display: $localize`Object type`,
              field: 'object_type',
              type: FilterTypes.Choices,
            },
            {
              display: $localize`Action`,
              field: 'action',
              type: FilterTypes.Choices,
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: 'create',
    component: RbacPolicyCreateComponent,
    resolve: {
      createOptions: RbacPolicyCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create RBAC policy`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: RbacPolicyDetailsComponent,
    resolve: {
      rbac_policy: RbacPolicyResolver,
      permissions: RbacPolicyPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.rbac_policy.id,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: RbacPolicyEditComponent,
    resolve: {
      rbac_policy: RbacPolicyResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit RBAC policy ${data.rbac_policy.id}`,
      } as IRouteConfig,
    }
  },

];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class RbacPoliciesRoutingModule {
}
