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 { DbaasInstanceListComponent } from './dbaas-instance-list/dbaas-instance-list.component';
import { DbaasInstanceListResolver } from '@fleio-api/openstack/dbaas/dbaas-instance-list.resolver';
import { DbaasInstanceCreateComponent } from './dbaas-instance-create/dbaas-instance-create.component';
import { DbaasInstanceCreateOptionsResolver } from '@fleio-api/openstack/dbaas/dbaas-instance-create-options.resolver';
import { DbaasInstanceDetailsComponent } from './dbaas-instance-details/dbaas-instance-details.component';
import { DbaasInstanceResolver } from '@fleio-api/openstack/dbaas/dbaas-instance.resolver';
import { DbaasInstancePermissionsResolver } from '@fleio-api/openstack/dbaas/dbaas-instance-permissions.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { DbaasInstanceResizeOptionsResolver } from '@fleio-api/openstack/dbaas/dbaas-instance-resize-options.resolver';
import { DbaasInstanceResizeFlavorComponent } from './dbaas-instance-resize-flavor/dbaas-instance-resize-flavor.component';

const routes: Routes = [
  {
    path: '',
    component: DbaasInstanceListComponent,
    resolve: {
      instances: DbaasInstanceListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'openstack.dbaas',
        getBreadCrumbDetail: () => $localize`Database instances`,
        search: {
          show: true,
          placeholder: $localize`Search database instances ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.ports;
          },
          objectName: $localize`instance`,
          objectNamePlural: $localize`instances`,
        },
        ordering: {
          default: {
            display: $localize`Created at`,
            field: 'created',
            direction: OrderingDirection.Descending,
          },
          options: [
            {
              display: $localize`Created at`,
              field: 'created',
            },
            {
              display: $localize`Updated`,
              field: 'updated',
            },
            {
              display: $localize`Status`,
              field: 'status',
            },
            {
              display: $localize`Name`,
              field: 'name',
            },
            {
              display: $localize`Operating status`,
              field: 'operating_status',
            },
            {
              display: $localize`Region`,
              field: 'region',
            },
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Created at`,
              field: 'created',
              type: FilterTypes.Date
            },
            {
              display: $localize`Updated at`,
              field: 'updated',
              type: FilterTypes.Date
            },
            {
              field: 'server__availability_zone',
              display: $localize`Availability zone`,
              type: FilterTypes.Choices
            },
            {
              display: $localize`Region`,
              field: 'region',
              type: FilterTypes.CustomModel,
              items: 'regions',
              itemsDisplayField: 'id'
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices,
            },
            {
              display: $localize`Operating status`,
              field: 'operating_status',
              type: FilterTypes.Choices,
            },
            {
              field: 'datastore_ref__name',
              display: $localize`Datastore`,
              type: FilterTypes.Choices
            },
            {
              display: $localize`Locality`,
              field: 'locality',
              type: FilterTypes.Choices,
            },
            {
              display: $localize`Replica of`,
              field: 'replica_of',
              type: FilterTypes.CustomModel,
              items: 'dbaasinstances',
              itemsDisplayField: 'name',
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: DbaasInstanceCreateComponent,
    resolve: {
      createOptions: DbaasInstanceCreateOptionsResolver,
    },
    data: {
      config: {
        feature: 'openstack.dbaas',
        getBreadCrumbDetail: () => $localize`Create database instance`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: DbaasInstanceDetailsComponent,
    resolve: {
      database_instance: DbaasInstanceResolver,
      permissions: DbaasInstancePermissionsResolver,
    },
    runGuardsAndResolvers: 'always',
    data: {
      feature: 'openstack.dbaas',
      config: {
        getBreadCrumbDetail: (data) => $localize`Database instance ${data.database_instance.name || data.database_instance.id}`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/resize',
    component: DbaasInstanceResizeFlavorComponent,
    resolve: {
      database_instance: DbaasInstanceResolver,
      resizeOptions: DbaasInstanceResizeOptionsResolver,
      permissions: DbaasInstancePermissionsResolver,
    },
    runGuardsAndResolvers: 'always',
    data: {
      feature: 'openstack.dbaas',
      config: {
        getBreadCrumbDetail: (data) => $localize`Resize database instance ${data.database_instance.name || data.database_instance.id}`,
      } as IRouteConfig,
    }
  },
];

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