import type { LocationQuery, LocationQueryRaw } from 'vue-router';

import {
  buildListRouteStateQuery,
  defineListRouteStateSchema,
  mergeListRouteStateQuery,
  parseListRouteState,
  positiveIntegerListRouteField,
  stringListRouteField,
} from '#/composables/useListRouteState';

interface PluginListRouteState {
  categoryId: string;
  keyword: string;
  pageNumber: number;
  pageSize: number;
}

interface PluginToolListRouteState {
  keyword: string;
  pageNumber: number;
  pageSize: number;
}

const pluginListRouteSchema = defineListRouteStateSchema<PluginListRouteState>({
  path: '/ai/plugin',
  fields: {
    categoryId: stringListRouteField({ defaultValue: '0' }),
    keyword: stringListRouteField(),
    pageNumber: positiveIntegerListRouteField({ defaultValue: 1 }),
    pageSize: positiveIntegerListRouteField({
      allowedValues: [12, 24, 36, 48],
      defaultValue: 12,
    }),
  },
});

const pluginToolListRouteSchema =
  defineListRouteStateSchema<PluginToolListRouteState>({
    path: '/ai/plugin/tools',
    fields: {
      keyword: stringListRouteField(),
      pageNumber: positiveIntegerListRouteField({ defaultValue: 1 }),
      pageSize: positiveIntegerListRouteField({
        allowedValues: [10, 20, 50, 100],
        defaultValue: 10,
      }),
    },
  });

function parsePluginListRouteState(query: LocationQuery) {
  return parseListRouteState(query, pluginListRouteSchema);
}

function buildPluginListRouteQuery(
  state: PluginListRouteState,
): LocationQueryRaw {
  return buildListRouteStateQuery(state, pluginListRouteSchema);
}

function mergePluginListRouteQuery(
  query: LocationQuery,
  state: PluginListRouteState,
): LocationQueryRaw {
  return mergeListRouteStateQuery(query, state, pluginListRouteSchema);
}

function parsePluginToolListRouteState(query: LocationQuery) {
  return parseListRouteState(query, pluginToolListRouteSchema);
}

function buildPluginToolListRouteQuery(
  state: PluginToolListRouteState,
): LocationQueryRaw {
  return buildListRouteStateQuery(state, pluginToolListRouteSchema);
}

function mergePluginToolListRouteQuery(
  query: LocationQuery,
  state: PluginToolListRouteState,
): LocationQueryRaw {
  return mergeListRouteStateQuery(query, state, pluginToolListRouteSchema);
}

export {
  buildPluginListRouteQuery,
  buildPluginToolListRouteQuery,
  mergePluginListRouteQuery,
  mergePluginToolListRouteQuery,
  parsePluginListRouteState,
  parsePluginToolListRouteState,
  pluginListRouteSchema,
  pluginToolListRouteSchema,
};
export type { PluginListRouteState, PluginToolListRouteState };
