/**
 * Figma API Types
 */

export interface FigmaColor {
  r: number;
  g: number;
  b: number;
  a: number;
}

export interface FigmaPaint {
  type: 'SOLID' | 'IMAGE' | 'GRADIENT_LINEAR' | 'GRADIENT_RADIAL' | 'GRADIENT_ANGULAR' | 'GRADIENT_DIAMOND';
  color?: FigmaColor;
  opacity?: number;
  imageRef?: string;
  scaleMode?: 'FILL' | 'FIT' | 'STRETCH' | 'TILE';
  gradientHandlePositions?: Array<{ x: number; y: number }>;
  gradientStops?: Array<{ position: number; color: FigmaColor }>;
  visible?: boolean;
}

export interface FigmaEffect {
  type: 'DROP_SHADOW' | 'INNER_SHADOW' | 'LAYER_BLUR' | 'BACKGROUND_BLUR';
  visible: boolean;
  offset?: { x: number; y: number };
  radius: number;
  spread?: number;
  color?: FigmaColor;
}

export interface FigmaTextStyle {
  fontFamily?: string;
  fontWeight?: number;
  fontSize?: number;
  lineHeightPx?: number;
  letterSpacing?: number;
  textCase?: string;
  textAlignHorizontal?: string;
  textAlignVertical?: string;
}

export interface FigmaNode {
  id: string;
  name: string;
  type: string;
  visible?: boolean;
  children?: FigmaNode[];
  characters?: string;
  style?: FigmaTextStyle;
  fills?: FigmaPaint[];
  strokes?: FigmaPaint[];
  strokeWeight?: number;
  individualStrokeWeights?: { top: number; right: number; bottom: number; left: number };
  strokeDashes?: number[];
  effects?: FigmaEffect[];
  opacity?: number;
  cornerRadius?: number;
  rectangleCornerRadii?: number[];
  layoutMode?: 'NONE' | 'HORIZONTAL' | 'VERTICAL';
  primaryAxisAlignItems?: 'MIN' | 'MAX' | 'CENTER' | 'SPACE_BETWEEN' | 'BASELINE';
  counterAxisAlignItems?: 'MIN' | 'MAX' | 'CENTER' | 'SPACE_BETWEEN' | 'BASELINE';
  layoutAlign?: 'MIN' | 'MAX' | 'CENTER' | 'STRETCH';
  layoutPositioning?: 'AUTO' | 'ABSOLUTE';
  layoutWrap?: 'NO_WRAP' | 'WRAP';
  itemSpacing?: number;
  paddingTop?: number;
  paddingRight?: number;
  paddingBottom?: number;
  paddingLeft?: number;
  overflowDirection?: string;
  layoutSizingHorizontal?: 'FIXED' | 'FILL' | 'HUG';
  layoutSizingVertical?: 'FIXED' | 'FILL' | 'HUG';
  layoutGrow?: number;
  preserveRatio?: boolean;
  absoluteBoundingBox?: {
    x: number;
    y: number;
    width: number;
    height: number;
  };
  clipsContent?: boolean;
  componentId?: string;
  componentProperties?: Record<string, { value: string | boolean | number; type: string }>;
}

export interface FigmaComponent {
  name: string;
  description?: string;
  type?: string;
}

export interface FigmaComponentSet {
  name: string;
  description?: string;
}

export interface FigmaFileResponse {
  name: string;
  lastModified: string;
  thumbnailUrl?: string;
  document: FigmaNode;
  components?: Record<string, FigmaComponent>;
  componentSets?: Record<string, FigmaComponentSet>;
}

export interface FigmaNodeResponse {
  name: string;
  lastModified: string;
  thumbnailUrl?: string;
  nodes: Record<string, {
    document: FigmaNode;
    components?: Record<string, FigmaComponent>;
    componentSets?: Record<string, FigmaComponentSet>;
  }>;
}

export interface FigmaImageResponse {
  images: Record<string, string | null>;
}

export interface FigmaImageFillsResponse {
  meta: {
    images: Record<string, string>;
  };
}

export type FigmaResponse = FigmaFileResponse | FigmaNodeResponse;

export interface SimplifiedLayout {
  mode: 'none' | 'row' | 'column';
  justifyContent?: string;
  alignItems?: string;
  alignSelf?: string;
  wrap?: boolean;
  gap?: string;
  padding?: string;
  position?: 'absolute';
  locationRelativeToParent?: { x: number; y: number };
  dimensions?: { width?: number; height?: number; aspectRatio?: number };
  sizing?: { horizontal?: 'fixed' | 'fill' | 'hug'; vertical?: 'fixed' | 'fill' | 'hug' };
  overflowScroll?: ('x' | 'y')[];
}

export interface SimplifiedStroke {
  colors: SimplifiedFill[];
  strokeWeight?: string;
  strokeDashes?: number[];
}

export interface SimplifiedEffects {
  boxShadow?: string;
  textShadow?: string;
  filter?: string;
  backdropFilter?: string;
}

export interface SimplifiedTextStyle {
  fontFamily?: string;
  fontWeight?: number;
  fontSize?: number;
  lineHeight?: string;
  letterSpacing?: string;
  textCase?: string;
  textAlignHorizontal?: string;
  textAlignVertical?: string;
}

export type SimplifiedFill = 
  | { 
      type?: string; 
      hex?: string; 
      rgba?: string; 
      opacity?: number; 
      imageRef?: string; 
      scaleMode?: string;
      gradientHandlePositions?: Array<{ x: number; y: number }>;
      gradientStops?: Array<{ position: number; color: { hex: string; opacity: number } }>;
    }
  | string;

export interface SimplifiedNode {
  id: string;
  name: string;
  type: string;
  text?: string;
  textStyle?: string;
  fills?: string;
  strokes?: string;
  effects?: string;
  layout?: string;
  opacity?: number;
  borderRadius?: string;
  componentId?: string;
  componentProperties?: Array<{ name: string; value: string; type: string }>;
  children?: SimplifiedNode[];
}

export interface SimplifiedDesign {
  name: string;
  lastModified: string;
  thumbnailUrl: string;
  nodes: SimplifiedNode[];
  components: Record<string, { name: string; description?: string; type?: string }>;
  componentSets: Record<string, { name: string; description?: string }>;
  globalVars: {
    styles: Record<string, unknown>;
  };
}

export interface ParsedFigmaUrl {
  fileKey: string;
  nodeId: string | null;
}
