/**
 * Jellyfin Field Mapping Tests
 */

import { describe, it, expect } from "vitest";
import {
  SORT_FIELD_MAP,
  getJellyfinSortField,
  normalizeSortOrder,
  ITEM_TYPES,
  ITEM_TYPE_GROUPS,
} from "./jellyfinFieldMapping";

describe("Jellyfin Field Mapping", () => {
  describe("SORT_FIELD_MAP", () => {
    it("should map frontend sort keys to Jellyfin fields", () => {
      expect(SORT_FIELD_MAP.title).toBe("SortName");
      expect(SORT_FIELD_MAP.artist).toBe("Artist");
      expect(SORT_FIELD_MAP.album).toBe("Album");
      expect(SORT_FIELD_MAP.year).toBe("ProductionYear");
      expect(SORT_FIELD_MAP.recent).toBe("DatePlayed");
      expect(SORT_FIELD_MAP.added).toBe("DateCreated");
      expect(SORT_FIELD_MAP.rating).toBe("CommunityRating");
    });

    it("should have all common audio sorts", () => {
      expect(SORT_FIELD_MAP).toHaveProperty("title");
      expect(SORT_FIELD_MAP).toHaveProperty("artist");
      expect(SORT_FIELD_MAP).toHaveProperty("album");
      expect(SORT_FIELD_MAP).toHaveProperty("year");
      expect(SORT_FIELD_MAP).toHaveProperty("recent");
    });

    it("should have fallback sort names", () => {
      expect(SORT_FIELD_MAP.name).toBe("SortName");
    });

    it("should map aliases to same fields", () => {
      expect(SORT_FIELD_MAP.title).toBe(SORT_FIELD_MAP.name);
      expect(SORT_FIELD_MAP.recent).toBe("DatePlayed");
      expect(SORT_FIELD_MAP.dateAdded).toBe("DateCreated");
      expect(SORT_FIELD_MAP.datePlayed).toBe("DatePlayed");
    });
  });

  describe("getJellyfinSortField()", () => {
    it("should return mapped field for known keys", () => {
      expect(getJellyfinSortField("artist")).toBe("Artist");
      expect(getJellyfinSortField("album")).toBe("Album");
      expect(getJellyfinSortField("year")).toBe("ProductionYear");
    });

    it("should fallback to SortName for unknown keys", () => {
      expect(getJellyfinSortField("unknown")).toBe("SortName");
      expect(getJellyfinSortField("")).toBe("SortName");
      expect(getJellyfinSortField("invalidKey")).toBe("SortName");
    });

    it("should be case-sensitive", () => {
      // Should work with exact case
      expect(getJellyfinSortField("title")).toBe("SortName");
      // Unknown case variations fallback to default
      expect(getJellyfinSortField("Title")).toBe("SortName");
      expect(getJellyfinSortField("TITLE")).toBe("SortName");
    });
  });

  describe("normalizeSortOrder()", () => {
    it("should accept valid ascending orders", () => {
      expect(normalizeSortOrder("Ascending")).toBe("Ascending");
      expect(normalizeSortOrder("ascending")).toBe("Ascending");
      expect(normalizeSortOrder("asc")).toBe("Ascending");
      expect(normalizeSortOrder(undefined)).toBe("Ascending");
    });

    it("should accept valid descending orders", () => {
      expect(normalizeSortOrder("Descending")).toBe("Descending");
      expect(normalizeSortOrder("descending")).toBe("Descending");
      expect(normalizeSortOrder("desc")).toBe("Descending");
    });

    it("should default to Ascending for unknown values", () => {
      expect(normalizeSortOrder("invalid")).toBe("Ascending");
      expect(normalizeSortOrder("random")).toBe("Ascending");
      expect(normalizeSortOrder("")).toBe("Ascending");
    });
  });

  describe("ITEM_TYPES", () => {
    it("should define audio types", () => {
      expect(ITEM_TYPES.AUDIO).toBe("Audio");
      expect(ITEM_TYPES.MUSIC_ALBUM).toBe("MusicAlbum");
      expect(ITEM_TYPES.MUSIC_ARTIST).toBe("MusicArtist");
    });

    it("should define video types", () => {
      expect(ITEM_TYPES.MOVIE).toBe("Movie");
      expect(ITEM_TYPES.SERIES).toBe("Series");
      expect(ITEM_TYPES.EPISODE).toBe("Episode");
    });

    it("should have consistent case", () => {
      // Jellyfin API uses CamelCase
      expect(ITEM_TYPES.MUSIC_ALBUM).toBe("MusicAlbum");
      expect(ITEM_TYPES.MUSIC_ARTIST).toBe("MusicArtist");
      expect(ITEM_TYPES.MUSIC_VIDEO).toBe("MusicVideo");
    });
  });

  describe("ITEM_TYPE_GROUPS", () => {
    it("should group audio types correctly", () => {
      expect(ITEM_TYPE_GROUPS.audio).toContain(ITEM_TYPES.AUDIO);
      expect(ITEM_TYPE_GROUPS.audio).toContain(ITEM_TYPES.MUSIC_ALBUM);
      expect(ITEM_TYPE_GROUPS.audio).toContain(ITEM_TYPES.MUSIC_ARTIST);
      expect(ITEM_TYPE_GROUPS.audio.length).toBe(3);
    });

    it("should group video types correctly", () => {
      expect(ITEM_TYPE_GROUPS.video).toContain(ITEM_TYPES.MOVIE);
      expect(ITEM_TYPE_GROUPS.video).toContain(ITEM_TYPES.SERIES);
      expect(ITEM_TYPE_GROUPS.video).toContain(ITEM_TYPES.EPISODE);
    });

    it("should provide movie and TV show subgroups", () => {
      expect(ITEM_TYPE_GROUPS.movies).toEqual([ITEM_TYPES.MOVIE]);
      expect(ITEM_TYPE_GROUPS.tvshows).toContain(ITEM_TYPES.SERIES);
      expect(ITEM_TYPE_GROUPS.tvshows).toContain(ITEM_TYPES.EPISODE);
    });

    it("should have music alias for audio", () => {
      expect(ITEM_TYPE_GROUPS.music).toEqual(ITEM_TYPE_GROUPS.audio);
    });

    it("should provide episodes filter", () => {
      expect(ITEM_TYPE_GROUPS.episodes).toEqual([ITEM_TYPES.EPISODE]);
    });
  });
});
