import {
  JellyfinItemKind,
  JellyfinItemSortBy,
  JellyfinLibraryItemsResponse,
} from '@tunarr/types/jellyfin';
import { makeEndpoint, parametersBuilder } from '@tunarr/zodios-core';
import { z } from 'zod/v4';

export const jellyfinEndpoints = [
  makeEndpoint({
    method: 'get',
    path: '/api/jellyfin/:mediaSourceId/user_libraries',
    parameters: parametersBuilder()
      .addPath('mediaSourceId', z.string())
      .build(),
    // response: JellyfinLibraryResponse,
    response: JellyfinLibraryItemsResponse,
    alias: 'getJellyfinUserLibraries',
  }),
  makeEndpoint({
    method: 'get',
    path: '/api/jellyfin/:mediaSourceId/libraries/:libraryId/items',
    parameters: parametersBuilder()
      .addPaths({
        mediaSourceId: z.string(),
        libraryId: z.string(),
      })
      .addQueries({
        offset: z.coerce.number().nonnegative().optional(),
        limit: z.coerce.number().positive().optional(),
        itemTypes: JellyfinItemKind.array()
          .transform((arr) => arr?.join(','))
          .optional(),
        nameStartsWithOrGreater: z.string().min(1).optional(),
        nameStartsWith: z.string().min(1).optional(),
        nameLessThan: z.string().min(1).optional(),
        sortBy: JellyfinItemSortBy.array().nonempty().nullish(),
        recursive: z
          .union([
            z.boolean(),
            z.literal('true'),
            z.literal('false'),
            z.coerce.number(),
          ])
          .optional(),
      })
      .build(),
    response: JellyfinLibraryItemsResponse,
    alias: 'getJellyfinItems',
  }),
] as const;
