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 { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { NgModule } from '@angular/core';
import { RoleListResolver } from '@fleio-api/client-user/roles/role-list.resolver';
import { RolesListComponent } from './roles-list/roles-list.component';
import { RoleCreateComponent } from './role-create/role-create.component';
import { RoleEditComponent } from './role-edit/role-edit.component';
import { RoleResolver } from '@fleio-api/client-user/roles/role.resolver';
import { RoleDetailsComponent } from './role-details/role-details.component';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';

const routes: Routes = [
  {
    path: '',
    component: RolesListComponent,
    resolve: {
      roles: RoleListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.roles',
        search: {
          show: true,
          placeholder: $localize`Search roles ...`,
        },
        subheader: {
          objectList: data => data.roles as FleioObjectsList<IBaseFleioObjectModel>,
          objectName: $localize`role`,
          objectNamePlural: $localize`roles`,
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Name`,
              field: 'name',
            },
            {
              display: $localize`Created at`,
              field: 'created_at',
            },
            {
              display: $localize`Updated at`,
              field: 'updated_at',
            },
            {
              display: $localize`Owner`,
              field: 'owner',
            },
            {
              display: $localize`Parent`,
              field: 'parent',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'created_at',
              display: $localize`Created at`,
              type: FilterTypes.Date
            },
            {
              field: 'updated_at',
              display: $localize`Updated at`,
              type: FilterTypes.Date
            },
            {
              display: $localize`Owner`,
              field: 'owner',
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'name',
            },
            {
              display: $localize`Parent`,
              field: 'parent',
              type: FilterTypes.CustomModel,
              items: 'roles',
              itemsDisplayField: 'name',
            },
            {
              field: 'public',
              display: $localize`Public`,
              type: FilterTypes.Boolean
            },
            {
              field: 'default',
              display: $localize`Default`,
              type: FilterTypes.Boolean
            },
          ]
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: RoleCreateComponent,
    resolve: {},
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create role`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/edit',
    component: RoleEditComponent,
    resolve: {
      role: RoleResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit role ${data.role.name}`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: RoleDetailsComponent,
    resolve: {
      role: RoleResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Role ${data.role.name}`,
      } as IRouteConfig,
    }
  },
];

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