import type { Bilingual, LicenseCategory, LicenseTemplate, LicenseType } from '@ema-platform/api';

import { BASE_API_URL } from '@ema-platform/api';

/** Same resolution — and same fallback — the shared RTK Query baseQuery uses. */
export const API_BASE_URL = BASE_API_URL;

export const STATUS_COLOR: Record<LicenseTemplate['status'], string> = {
  DRAFT: 'gray',
  PUBLISHED: 'teal',
  ARCHIVED: 'dark',
};

/**
 * Page options sent with every save and preview.
 *
 * A4 unless the version carries an explicit page size — set for documents
 * like the Seaman Book, whose ICAO 9303 passport-booklet dimensions have no
 * named `format` preset. `width`/`height` win over `format` in Puppeteer, so
 * a custom size is sent alone rather than alongside `format: 'A4'`.
 */
export function pageOptionsFor(landscape: boolean, size?: { width: string; height: string }) {
  if (size) return { width: size.width, height: size.height, landscape, printBackground: true };
  return { format: 'A4' as const, landscape, printBackground: true };
}

/**
 * Order the category groups appear in, and their fallback labels.
 *
 * Seafarer certification sits first: it holds over 50 of the 80-odd licence
 * types, and it is what the designer is opened for most often. Any category
 * not listed here still renders — it falls to the end under its own key —
 * so a new category on the server does not silently hide its types.
 */
const CATEGORY_ORDER: { key: LicenseCategory; fallback: string }[] = [
  { key: 'MARITIME_PERSONNEL', fallback: 'Seafarer certification (CoC / CoP)' },
  { key: 'CARGO_FREIGHT', fallback: 'Cargo & freight' },
  { key: 'SHIPPING_AGENCY', fallback: 'Shipping agency' },
  { key: 'INVESTMENT', fallback: 'Investment & joint ventures' },
  { key: 'VESSEL_SERVICES', fallback: 'Vessel services' },
  { key: 'WAIVER_SERVICES', fallback: 'Waiver services' },
];

/**
 * The licence-type picker's options, grouped by category.
 *
 * Grouping is what makes the picker usable at all: certificates of competency
 * and proficiency are a different kind of thing from an operator's logistics
 * licence, and a flat alphabetical list interleaves the two.
 */
export function groupedTypeOptions(
  licenseTypes: LicenseType[],
  localized: (value?: Bilingual) => string,
  t: (key: string, fallback: string) => string,
) {
  const byCategory = new Map<string, { value: string; label: string }[]>();

  for (const type of licenseTypes) {
    const option = { value: type.id, label: localized(type.name) || type.key };
    const bucket = byCategory.get(type.category);
    if (bucket) bucket.push(option);
    else byCategory.set(type.category, [option]);
  }

  const known = CATEGORY_ORDER.map(({ key, fallback }) => ({
    group: t(`designer.category.${key}`, fallback),
    items: (byCategory.get(key) ?? []).sort((a, b) => a.label.localeCompare(b.label)),
  }));

  // Categories the client does not know about yet, so their types stay reachable.
  const unknown = [...byCategory.entries()]
    .filter(([key]) => !CATEGORY_ORDER.some((c) => c.key === key))
    .map(([key, items]) => ({
      group: key,
      items: items.sort((a, b) => a.label.localeCompare(b.label)),
    }));

  return [...known, ...unknown].filter((group) => group.items.length > 0);
}
