import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
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 { AuthGuard } from '@shared/auth/auth.guard';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { EmailMessageLogListComponent } from './email-message-log-list/email-message-log-list.component';
import { EmailMessageLogListResolver } from '@fleio-api/logger/email-message-log-list.resolver';
import { EmailMessageLogDetailsComponent } from './email-message-log-details/email-message-log-details.component';
import { EmailMessageLogResolver } from '@fleio-api/logger/email-message-log.resolver';

const routes: Routes = [
  {
    path: '',
    component: EmailMessageLogListComponent,
    resolve: {
      emailMessageLogEntries: EmailMessageLogListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'logger.email-message-log',
        search: {
          show: true,
          placeholder: $localize`Search email message log ...`,
        },
        subheader: {
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.emailMessageLogEntries;
          },
          objectName: $localize`email message log entry`,
          objectNamePlural: $localize`email message log entries`
        },
        ordering: {
          default: {
            display: $localize`Timestamp`,
            field: 'created_at',
            direction: OrderingDirection.Descending,
          },
          options: [
            {display: $localize`Timestamp`, field: 'created_at'},
            {display: $localize`Subject`, field: 'subject'},
            {display: $localize`Sent to`, field: 'to'},
            {display: $localize`Client`, field: 'client'},
            {display: $localize`User`, field: 'user'},
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Timestamp`,
              field: 'created_at',
              type: FilterTypes.Date
            },
            {
              field: 'client',
              display: $localize`Client`,
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'display',
            },
            {
              field: 'user',
              display: $localize`Users`,
              type: FilterTypes.CustomModel,
              items: 'users',
              itemsDisplayField: 'full_name',
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: EmailMessageLogDetailsComponent,
    resolve: {
      emailMessageLogEntry: EmailMessageLogResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.emailMessageLogEntry.subject,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

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