/**
 * @aurora-catalyst-generated
 * @source cliter/o-auth/client.aurora.yaml
 */
import { type ResolveFn } from '@angular/router';
import {
  buildSortOrder,
  DEFAULT_PAGE_SIZE,
  queryFindById,
  queryGet,
  queryPaginate,
  type DataTableData,
} from '@aurora';
import {
  type OAuthApplication,
  type OAuthClient,
  type OAuthScope,
} from '@src/src/app/aurora.types';
import { forkJoin } from 'rxjs';
import { APPLICATION_LIST_CONFIG } from '../../application/data-access/application.list-config';
import { SCOPE_LIST_CONFIG } from '../../scope/data-access/scope.list-config';
import { CLIENT_DETAIL_CONFIG } from './client.detail-config';
import { CLIENT_LIST_CONFIG } from './client.list-config';

const scopeOptions$ = () =>
  queryGet<OAuthScope>(
    SCOPE_LIST_CONFIG.queryGet,
    SCOPE_LIST_CONFIG.getKey,
    SCOPE_LIST_CONFIG.fields,
    { query: { order: buildSortOrder([]) } },
  );

export const clientListResolver: ResolveFn<{
  pagination: DataTableData<OAuthClient>;
  applicationOptions: OAuthApplication[];
  scopeOptions: OAuthScope[];
}> = () => {
  const { queryPagination, paginationKey, fields } = CLIENT_LIST_CONFIG;
  const pagination$ = queryPaginate<OAuthClient>(queryPagination, paginationKey, fields, {
    query: {
      offset: 0,
      limit: DEFAULT_PAGE_SIZE,
      order: buildSortOrder([]),
    },
    constraint: { include: [{ association: 'applications' }] },
  });
  const applicationOptions$ = queryGet<OAuthApplication>(
    APPLICATION_LIST_CONFIG.queryGet,
    APPLICATION_LIST_CONFIG.getKey,
    APPLICATION_LIST_CONFIG.fields,
    { query: { order: buildSortOrder([]) } },
  );
  return forkJoin({
    pagination: pagination$,
    applicationOptions: applicationOptions$,
    scopeOptions: scopeOptions$(),
  });
};

export const clientNewResolver: ResolveFn<{
  applicationOptions: OAuthApplication[];
  scopeOptions: OAuthScope[];
}> = () => {
  const applicationOptions$ = queryGet<OAuthApplication>(
    APPLICATION_LIST_CONFIG.queryGet,
    APPLICATION_LIST_CONFIG.getKey,
    APPLICATION_LIST_CONFIG.fields,
    { query: { order: buildSortOrder([]) } },
  );
  return forkJoin({ applicationOptions: applicationOptions$, scopeOptions: scopeOptions$() });
};

export const clientDetailResolver: ResolveFn<
  {
    item: OAuthClient | null;
    applicationOptions: OAuthApplication[];
    scopeOptions: OAuthScope[];
  } | null
> = (route) => {
  const id = route.paramMap.get('id');
  if (!id) return null;
  const { queryObject, objectKey, fields } = CLIENT_DETAIL_CONFIG;
  const item$ = queryFindById<OAuthClient>(queryObject, objectKey, id, fields, {
    constraint: { include: [{ association: 'applications' }] },
  });
  const applicationOptions$ = queryGet<OAuthApplication>(
    APPLICATION_LIST_CONFIG.queryGet,
    APPLICATION_LIST_CONFIG.getKey,
    APPLICATION_LIST_CONFIG.fields,
    { query: { order: buildSortOrder([]) } },
  );
  return forkJoin({
    item: item$,
    applicationOptions: applicationOptions$,
    scopeOptions: scopeOptions$(),
  });
};
