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 { ClientListResolver } from '@fleio-api/client-user/client/client-list.resolver';
import { ClientResolver } from '@fleio-api/client-user/client/client.resolver';
import { ClientCreateOptionsResolver } from '@fleio-api/client-user/client/client-create-options.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { AuthGuard } from '@shared/auth/auth.guard';
import { ClientListComponent } from './client-list/client-list.component';
import { ClientCreateComponent } from './client-create/client-create.component';
import { ClientEditComponent } from './client-edit/client-edit.component';
import { ConfigureAutoInvoicingComponent } from './configure-auto-invoicing/configure-auto-invoicing.component';
import {
  ClientAutoInvoicingSettingsResolver
} from '@fleio-api/client-user/client/client-auto-invoicing-settings.resolver';
import { ClientDetailComponent } from './client-detail/client-detail.component';
import {
  UserClientNotificationsSettingsResolver
} from '@fleio-api/client-user/client/user-client-notifications-settings.resolver';

const routes: Routes = [
  {
    path: '',
    component: ClientListComponent,
    resolve: {
      clients: ClientListResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.clients',
        search: {
          show: true,
          placeholder: $localize`Search clients ...`,
        },
        getBreadCrumbDetail: () => $localize`Clients`,
        subheader: {
          objectName: $localize`client`,
          objectNamePlural: $localize`clients`,
          objectList: data => data.clients as FleioObjectsList<IBaseFleioObjectModel>
        },
        ordering: {
          default: {
            field: 'date_created',
            direction: OrderingDirection.Descending,
            display: $localize`Date created`,
          },
          options: [
            {display: $localize`Company`, field: 'company'},
            {display: $localize`Country`, field: 'country'},
            {display: $localize`State`, field: 'state'},
            {display: $localize`Date created`, field: 'date_created'},
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'date_created',
              display: $localize`Date created`,
              type: FilterTypes.Date
            }
          ]
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'create',
    component: ClientCreateComponent,
    resolve: {
      createOptions: ClientCreateOptionsResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Create client`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id/auto-invoicing',
    component: ConfigureAutoInvoicingComponent,
    resolve: {
      client: ClientResolver,
      autoInvoicingSettings: ClientAutoInvoicingSettingsResolver
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Client ${data.client.long_name} credit auto invoicing`,
      } as IRouteConfig,
    }
  },
  {
    path: ':id',
    component: ClientDetailComponent,
    resolve: {
      client: ClientResolver,
      notificationsSettings: UserClientNotificationsSettingsResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.clients',
        getBreadCrumbDetail: (data) => `${data.client.long_name}`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id/edit',
    component: ClientEditComponent,
    resolve: {
      client: ClientResolver,
      createOptions: ClientCreateOptionsResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'clients&users.clients',
        getBreadCrumbDetail: (data) => $localize`Edit ${data.client.long_name}`,
      } as IRouteConfig,
    }
  },
];

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