/**
 * Artboard shape path data and dimension presets.
 *
 * Paths are expressed as SVG path "d" attribute strings, generated relative to
 * a 0,0 origin for the given width × height.  For standard shapes (rect,
 * square, circle, oval) no clip path is needed — Konva primitives handle them.
 * Shield and pennant require custom clip paths.
 */

import type { ArtboardConfig, ArtboardShape, ArtboardUnit } from '../types/canvas';

// -- Dimension presets (inches) -----------------------------------------------

export interface DimensionPreset {
  label: string;
  width: number;
  height: number;
  unit: ArtboardUnit;
}

/** Default presets for each artboard shape. */
export const ARTBOARD_PRESETS: Record<ArtboardShape, DimensionPreset> = {
  rect:    { label: 'Rectangle',  width: 4,    height: 6,   unit: 'inches' },
  square:  { label: 'Square',     width: 4,    height: 4,   unit: 'inches' },
  circle:  { label: 'Circle',     width: 4,    height: 4,   unit: 'inches' },
  oval:    { label: 'Oval',       width: 3,    height: 4,   unit: 'inches' },
  shield:  { label: 'Shield',     width: 4,    height: 5,   unit: 'inches' },
  pennant: { label: 'Pennant',    width: 3.5,  height: 8,   unit: 'inches' },
  custom:  { label: 'Custom',     width: 4,    height: 4,   unit: 'inches' },
};

// -- Path generators ----------------------------------------------------------

/**
 * Shield shape: flat top, slightly flared sides, pointed bottom.
 * The path fills the full w × h bounding box.
 */
export function shieldPath(w: number, h: number): string {
  const indent = w * 0.08;
  return [
    `M ${indent} 0`,
    `L ${w - indent} 0`,
    `Q ${w} 0, ${w} ${h * 0.08}`,
    `L ${w} ${h * 0.5}`,
    `Q ${w} ${h * 0.75}, ${w / 2} ${h}`,
    `Q 0 ${h * 0.75}, 0 ${h * 0.5}`,
    `L 0 ${h * 0.08}`,
    `Q 0 0, ${indent} 0`,
    'Z',
  ].join(' ');
}

/**
 * Pennant / triangle shape: wide top tapering to a point at the bottom-center.
 */
export function pennantPath(w: number, h: number): string {
  return `M 0 0 L ${w} 0 L ${w / 2} ${h} Z`;
}

/**
 * Given an ArtboardConfig, return the SVG clip path string if the shape needs
 * one, or undefined if standard Konva shapes suffice.
 */
export function artboardClipPath(config: ArtboardConfig): string | undefined {
  const { shape, width, height } = config;
  const pxW = toPx(width, config.unit);
  const pxH = toPx(height, config.unit);

  switch (shape) {
    case 'shield':
      return shieldPath(pxW, pxH);
    case 'pennant':
      return pennantPath(pxW, pxH);
    case 'custom':
      return config.clipPath;
    default:
      return undefined;
  }
}

// -- Unit conversion ----------------------------------------------------------

/** Pixels-per-inch at screen DPI (96). */
const PPI = 96;
/** Pixels-per-mm. */
const PPMM = PPI / 25.4;

/** Convert a dimension to pixels. */
export function toPx(value: number, unit: ArtboardUnit): number {
  return unit === 'inches' ? value * PPI : value * PPMM;
}

/** Convert pixels back to the given unit. */
export function fromPx(px: number, unit: ArtboardUnit): number {
  return unit === 'inches' ? px / PPI : px / PPMM;
}
