import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { AuthGuard } from '@shared/auth/auth.guard';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { OperationsResolver } from '@fleio-api/utils/operations/operations.resolver';
import { OperationsListComponent } from './operations-list/operations-list.component';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { OperationsDetailsComponent } from './operations-details/operations-details.component';
import { OperationResolver } from '@fleio-api/utils/operations/operation.resolver';

const routes: Routes = [
  {
    path: '',
    component: OperationsListComponent,
    resolve: {
      operations: OperationsResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'utils.operations',
        search: {
          show: true,
          placeholder: $localize`Search operations ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.operations;
          },
          objectName: $localize`operation`,
          objectNamePlural: $localize`operations`,
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Created at`,
              field: 'created_at',
            },
            {
              display: $localize`Status`,
              field: 'status',
            }
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'created_at',
              display: $localize`Created at`,
              type: FilterTypes.Date
            },
            {
              field: 'completed',
              display: $localize`Completed`,
              type: FilterTypes.Boolean
            },
            {
              field: 'seen_by_users',
              display: $localize`Viewed by me`,
              type: FilterTypes.CurrentUser
            },
            {
              field: 'operation_type',
              display: $localize`Operation type`,
              type: FilterTypes.Choices
            },
            {
              field: 'status',
              display: $localize`Status`,
              type: FilterTypes.Choices
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: OperationsDetailsComponent,
    resolve: {
      operation: OperationResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Operation ${data.operation.id}:operation_id:`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: OperationsDetailsComponent,
    resolve: {
      operation: OperationResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Operation ${data.operation.id}:operation_id:`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
];

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