import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { IRouteConfig } from '@shared/ui-api/interfaces/route-config/route-config';
import { InvoiceListComponent } from './invoice-list/invoice-list.component';
import { InvoiceDetailsComponent } from './invoice-details/invoice-details.component';
import { InvoiceListResolver } from '@fleio-api/billing/invoices/invoice-list.resolver';
import { InvoiceResolver } from '@fleio-api/billing/invoices/invoice.resolver';
import { OrderingDirection } from '@shared/ui-api/interfaces/route-config/ordering-directions';
import { FilterTypes } from '@shared/ui-api/interfaces/route-config/filter-types';
import { FleioObjectsList } from '@fleio-api/fleio-objects-list';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { AuthGuard } from '@shared/auth/auth.guard';
import { PayMultipleInvoicesComponent } from './pay-multiple-invoices/pay-multiple-invoices.component';
import { InvoiceListPayResolver } from '@fleio-api/billing/invoices/invoice-list-pay.resolver';
import { InvoicePermissionsResolver } from '@fleio-api/billing/invoices/invoice-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: InvoiceListComponent,
    resolve: {
      invoices: InvoiceListResolver
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'billing.invoices',
        search: {
          show: true,
          placeholder: $localize`Search invoices ...`,
        },
        ordering: {
          default: {
            field: 'issue_date',
            display: $localize`Issue date`,
            direction: OrderingDirection.Descending
          },
          options: [
            {
              field: 'status',
              display: $localize`Status`
            },
            {
              field: 'issue_date',
              display: $localize`Issue date`
            },
            {
              field: 'due_date',
              display: $localize`Due date`
            },
          ],
        },
        filterConfig: {
          availableOptions: [
            {
              field: 'issue_date',
              display: $localize`Issue date`,
              type: FilterTypes.Date
            },
            {
              field: 'due_date',
              display: $localize`Due date`,
              type: FilterTypes.Date
            },
            {
              display: $localize`Status`,
              field: 'status',
              type: FilterTypes.Choices,
            },
            {
              field: 'total',
              display: $localize`Total`,
              type: FilterTypes.Decimal
            },
          ]
        },
        subheader: {
          objectName: $localize`invoice`,
          objectNamePlural: $localize`invoices`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.invoices;
          }
        },
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: 'pay-multiple-invoices',
    component: PayMultipleInvoicesComponent,
    resolve: {
      invoices: InvoiceListPayResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Pay multiple invoices`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
  {
    path: ':id',
    component: InvoiceDetailsComponent,
    resolve: {
      invoice: InvoiceResolver,
      permissions: InvoicePermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => data.invoice.display_number,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always',
  },
];

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