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 { 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 { JournalListComponent } from './journal-list/journal-list.component';
import { JournalListResolver } from '@fleio-api/billing/journal/journal-list.resolver';
import { JournalDetailsComponent } from './journal-details/journal-details.component';
import { JournalResolver } from '@fleio-api/billing/journal/journal.resolver';
import { JournalEditComponent } from './journal-edit/journal-edit.component';
import { JournalPermissionsResolver } from '@fleio-api/billing/journal/journal-permissions.resolver';

const routes: Routes = [
  {
    path: '',
    component: JournalListComponent,
    resolve: {
      journalEntries: JournalListResolver,
    },
    canActivate: [AuthGuard],
    data: {
      config: {
        feature: 'billing.journal',
        search: {
          show: true,
          placeholder: $localize`Search journal entries ...`,
        },
        subheader: {
          objectNamePlural: $localize`journal entries`,
          objectName: $localize`journal entry`,
          objectList(data): FleioObjectsList<IBaseFleioObjectModel> {
            return data.journalEntries;
          }
        },
        ordering: {
          default: {
            field: 'date_added',
            display: $localize`Date`,
            direction: OrderingDirection.Descending,
          },
          options: [
            {display: $localize`Id`, field: 'id'},
            {display: $localize`Date`, field: 'date_added'},
            {display: $localize`Invoice`, field: 'invoice'},
            {display: $localize`Transaction`, field: 'transaction'},
            {display: $localize`Transaction status`, field: 'transaction__status'},
            {display: $localize`Source`, field: 'source'},
            {display: $localize`Destination`, field: 'destination'},
            {display: $localize`Source amount`, field: 'source_amount'},
            {display: $localize`Destination amount`, field: 'destination_amount'},
          ]
        },
        filterConfig: {
          availableOptions: [
            {
              display: $localize`User`,
              field: 'user_id',
              type: FilterTypes.CustomModel,
              items: 'users',
              itemsDisplayField: 'display'
            },
            {
              display: $localize`Source`,
              field: 'source',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Destination`,
              field: 'destination',
              type: FilterTypes.Choices
            },
            {
              display: $localize`Credit related client`,
              field: 'client_credit__client',
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'name'
            },
            {
              display: $localize`Invoice related client`,
              field: 'invoice__client',
              type: FilterTypes.CustomModel,
              items: 'clients',
              itemsDisplayField: 'name'
            },
            {
              display: $localize`Date added`,
              field: 'date_added',
              type: FilterTypes.Date
            },
            {
              display: $localize`Destination amount`,
              field: 'destination_amount',
              type: FilterTypes.Decimal
            },
            {
              display: $localize`Source amount`,
              field: 'source_amount',
              type: FilterTypes.Decimal
            },
          ]
        }
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id',
    component: JournalDetailsComponent,
    resolve: {
      journalEntry: JournalResolver,
      permissions: JournalPermissionsResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: () => $localize`Detail`,
      } as IRouteConfig,
    },
    runGuardsAndResolvers: 'always'
  },
  {
    path: ':id/edit',
    component: JournalEditComponent,
    resolve: {
      journalEntry: JournalResolver,
    },
    data: {
      config: {
        getBreadCrumbDetail: (data) => $localize`Edit journal entry ${data.journalEntry.id}`,
      } as IRouteConfig,
    }
  },
];

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