import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { UserListResolver } from '@fleio-api/client-user/user/user-list.resolver';
import { UserListComponent } from './user-list/user-list.component';
import { UserCreateComponent } from './user-create/user-create.component';
import { UserDetailsComponent } from './user-details/user-details.component';
import { UserResolver } from '@fleio-api/client-user/user/user.resolver';
import { UserEditComponent } from './user-edit/user-edit.component';
import { UserImpersonateComponent } from './user-impersonate/user-impersonate.component';
import { AuthGuard } from '@shared/auth/auth.guard';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { UserPermissionsResolver } from '@fleio-api/client-user/user/user-permissions.resolver';
import { UserCreateOptionsResolver } from '@fleio-api/client-user/user/user-create-options.resolver';

const routes: Routes = [
  {
    path: '',
    component: UserListComponent,
    resolve: {
      users: UserListResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.users',
        search: {
          show: true,
          placeholder: $localize`Search users ...`,
        },
        subheader: {
          objectName: $localize`user`,
          objectNamePlural: $localize`users`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.users;
          }
        },
        ordering: {
          default: {
            display: $localize`Sign-up date`,
            field: 'date_joined',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Last login`,
              field: 'last_login',
            },
            {
              display: $localize`Sign-up date`,
              field: 'date_joined',
            },
            {
              display: $localize`Email`,
              field: 'email',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Date joined`,
              field: 'date_joined',
              type: FilterTypes.Date
            },
            {
              display: $localize`Last login`,
              field: 'last_login',
              type: FilterTypes.Date
            },
            {
              display: $localize`Is active`,
              field: 'is_active',
              type: FilterTypes.Boolean
            },
            {
              display: $localize`Is staff`,
              field: 'is_staff',
              type: FilterTypes.Boolean
            },
            {
              display: $localize`Is superuser`,
              field: 'is_superuser',
              type: FilterTypes.Boolean
            },
            {
              display: $localize`User group`,
              field: 'user_groups',
              type: FilterTypes.CustomModel,
              items: 'usergroups',
              itemsDisplayField: 'name'
            }
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: UserCreateComponent,
    resolve: {
      createOptions: UserCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create user`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: UserDetailsComponent,
    resolve: {
      user: UserResolver,
      permissions: UserPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.user.email,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: UserEditComponent,
    resolve: {
      user: UserResolver,
      createOptions: UserCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit ${data.user.email}`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/impersonate',
    component: UserImpersonateComponent,
    resolve: {
      user: UserResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Impersonating ${data.user.email}`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/impersonate/:clientId',
    component: UserImpersonateComponent,
    resolve: {
      user: UserResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Impersonating ${data.user.email}`,
      } as IRouteConfig,
    }
  },
];

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