// TypeScript mirror of the QSL template JSON schema (v1) defined in
// internal/qslcard/template.go. Template documents cross the Wails boundary
// as JSON strings; these types are the frontend's contract with that schema.

export interface CardGeom {
  w: number;
  h: number;
  dpi: number;
  bleed_px: number;
}

export interface CropRect {
  x: number;
  y: number;
  w: number;
  h: number;
}

export interface Scrim {
  enabled: boolean;
  color: string;
  opacity: number;
}

export interface Hero {
  photo: string;
  crop: CropRect;
  scrim?: Scrim;
}

export interface Shine {
  coverage: number;
  opacity: number;
}

export interface HaloFx {
  color: string;
  blur: number;
  opacity: number;
}

export interface ShadowFx {
  dx: number;
  dy: number;
  blur: number;
  color: string;
  opacity: number;
}

export interface BevelFx {
  dx: number;
  dy: number;
  dark: string;
  light: string;
}

// FxParams — per-call tuning of the canvas renderer (textFx.ts). All optional;
// omitted values fall back to the renderer's per-preset default.
export interface FxParams {
  plump?: number;
  edge?: number;
  outerw?: number;
  gloss?: number;
  gloss_h?: number;
  gloss_i?: number;
  inner_b?: number;
  depth?: number;
  angle?: number;
  slant?: number;
  grunge?: number;
  bevel?: number;
  seed?: number;
  dark?: string;
  outer?: string;
}

export interface StyleParams {
  gradient?: string[];
  shine?: Shine;
  outline_color?: string;
  outline_width?: number;
  halo?: HaloFx;
  shadow?: ShadowFx;
  bevel_offset?: BevelFx;
  color?: string;
  fx?: FxParams;
}

export type ElementType = 'callsign' | 'operator' | 'info_line' | 'country' | 'insert';

export interface CardElement {
  type: ElementType;
  x: number;
  y: number;
  rotate?: number;
  hidden?: boolean; // toggled off in the editor
  // text elements
  text?: string;
  font?: string;
  size?: number;
  style_preset?: string;
  style_params?: StyleParams;
  // country
  flag?: string;
  label?: string;
  // insert
  photo?: string;
  w?: number;
  border_px?: number;
  shadow?: boolean;
}

export interface QSOBox {
  enabled: boolean;
  x: number;
  y: number;
  w: number;
  h: number;
  bg: string;
  bg_opacity: number;
  fg?: string; // text colour (default dark); set it when using a dark background
  radius: number;
  title: string;
  fields: string[];
  footer: string;
}

export interface CardTemplate {
  schema: number;
  name: string;
  card: CardGeom;
  hero: Hero;
  elements: CardElement[];
  qso_box?: QSOBox;
}

// RenderModel mirrors qslcard.RenderModel: a template with placeholders
// resolved plus the values for the QSO box fields.
export interface RenderModel {
  template: CardTemplate;
  qso_fields: Record<string, string>;
}

// Binding result types (see app_qsl_designer.go).
export interface QSLTemplateInfo {
  id: number;
  name: string;
  profile_id?: number;
  is_default: boolean;
  updated_at: string;
}

export interface QSLFontInfo {
  family: string;
  kind: 'display' | 'script' | 'system';
  variable: boolean;
  data_b64: string;
}

export interface QSLPresetInfo {
  name: string;
  label: string;
  params: string[];
  defaults: StyleParams;
}

// Human labels for the QSO box fields the renderer knows.
// QSO_BOX_FIELDS is the canonical COLUMN ORDER of the confirmation box, and
// the whole list the designer offers. The renderer's column widths are tuned
// for it, so a box is always rebuilt from this order rather than from the
// order the operator happened to tick the boxes in.
export const QSO_BOX_FIELDS = ['qso_date', 'time_on', 'band', 'freq', 'mode', 'submode', 'rst_sent'] as const;

export const QSO_FIELD_LABELS: Record<string, string> = {
  qso_date: 'Date',
  time_on: 'UTC',
  band: 'Band',
  mode: 'Mode',
  rst_sent: 'RST',
  freq: 'Freq',
  submode: 'Submode',
};
