import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from '@shared/auth/auth.guard';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { TicketsListComponent } from './tickets-list/tickets-list.component';
import { TicketsListResolver } from '@fleio-api/plugins/tickets/tickets-list.resolver';
import { TicketsOpenNewComponent } from './tickets-open-new/tickets-open-new.component';
import { TicketsCreateOptionsResolver } from '@fleio-api/plugins/tickets/tickets-create-options.resolver';
import { TicketDetailsComponent } from './ticket-details/ticket-details.component';
import { TicketResolver } from '@fleio-api/plugins/tickets/ticket.resolver';
import { TicketCreateOptionsResolver } from '@fleio-api/plugins/tickets/ticket-create-options.resolver';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';

const routes: Routes = [
  {
    path: '',
    component: TicketsListComponent,
    resolve: {
      tickets: TicketsListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'plugins.tickets',
        search: {
          show: true,
          placeholder: $localize`Search tickets ...`,
        },
        ordering: {
          default: {
            field: 'last_reply_at',
            display: $localize`Last reply`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              field: 'last_reply_at',
              display: $localize`Last reply`
            },
            {
              field: 'created_at',
              display: $localize`Created at`
            },
            {
              field: 'status',
              display: $localize`Status`
            },
            {
              field: 'title',
              display: $localize`Title`
            },
            {
              field: 'department',
              display: $localize`Department`
            },
          ],
        },
        subheader: {
          objectName: $localize`ticket`,
          objectNamePlural: $localize`tickets`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.tickets;
          }
        },
        getBreadCrumbDetail: () => $localize`Tickets`,
        filterConfig: {
          availableOptions: [
            {
              display: $localize`Created at`,
              field: 'created_at',
              type: FilterTypes.Date
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Department`,
              field: 'department',
              type: FilterTypes.CustomModel,
              items: 'departments',
              itemsDisplayField: 'name',
            }
          ],
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'open-new-ticket',
    component: TicketsOpenNewComponent,
    resolve: {
      createOptions: TicketsCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Open a new ticket`,
      } as IRouteConfig,
    },
  },
  {
    path: 'open-new-ticket/:forclient',
    component: TicketsOpenNewComponent,
    resolve: {
      createOptions: TicketsCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Open a new ticket`,
      } as IRouteConfig,
    },
  },
  {
    path: ':id',
    component: TicketDetailsComponent,
    resolve: {
      ticket: TicketResolver,
      createOptions: TicketCreateOptionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`#${data.ticket.id}`,
        removeWidthLimit: true,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
];

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