export enum BlockType {
  TEXT = "text",
  BUTTON = "button",
  IMAGE = "image",
  CONTAINER = "container",
  SPACER = "spacer",
}

export interface Spacing {
  top: number;
  right: number;
  bottom: number;
  left: number;
}

export interface ResponsiveSettings {
  mobile?: Partial<BlockStyle>;
  tablet?: Partial<BlockStyle>;
}

export interface BaseBlock {
  id: string;
  type: BlockType;
  data: Record<string, unknown>;
  style: BlockStyle;
  responsive?: ResponsiveSettings;
}

export interface BlockStyle {
  margin?: Spacing;
  padding?: Spacing;
  backgroundColor?: string;
  borderRadius?: number;
  border?: {
    width: number;
    style: string;
    color: string;
  };
}

export interface TextBlock extends BaseBlock {
  type: BlockType.TEXT;
  data: {
    content: string;
    variables?: string[];
  };
  style: BlockStyle & {
    fontSize: number;
    fontWeight: string;
    color: string;
    textAlign: "left" | "center" | "right";
    lineHeight: number;
    fontFamily?: string;
  };
}

export interface ButtonBlock extends BaseBlock {
  type: BlockType.BUTTON;
  data: {
    text: string;
    url: string;
    variables?: string[];
  };
  style: BlockStyle & {
    textColor: string;
    fontSize: number;
    fontWeight: string;
    width?: number | "auto" | "100%";
    textAlign: "left" | "center" | "right";
  };
}

export interface ImageBlock extends BaseBlock {
  type: BlockType.IMAGE;
  data: {
    src: string;
    alt: string;
    url?: string;
    variables?: string[];
  };
  style: BlockStyle & {
    width: number | "auto" | "100%";
    height: number | "auto";
    objectFit: "cover" | "contain" | "fill";
    textAlign: "left" | "center" | "right";
  };
}

export interface ContainerBlock extends BaseBlock {
  type: BlockType.CONTAINER;
  data: {
    children: Block[];
  };
  style: BlockStyle & {
    maxWidth?: number;
    direction?: "row" | "column";
    justifyContent?: "flex-start" | "center" | "flex-end" | "space-between";
    alignItems?: "flex-start" | "center" | "flex-end";
  };
}

export interface SpacerBlock extends BaseBlock {
  type: BlockType.SPACER;
  data: Record<string, never>;
  style: BlockStyle & {
    height: number;
  };
}

export type Block =
  | TextBlock
  | ButtonBlock
  | ImageBlock
  | ContainerBlock
  | SpacerBlock;

// Template related types
export interface TemplateVariable {
  key: string;
  defaultValue: string;
  type: "text" | "url" | "image" | "number";
  description: string;
}

export interface Template {
  id: string;
  name: string;
  description?: string;
  blocks: Block[];
  variables: TemplateVariable[];
  settings: {
    width: number;
    backgroundColor: string;
    fontFamily: string;
    rtl: boolean;
  };
}

export interface ExportOptions {
  format: "html" | "json";
  includeCSS: boolean;
  optimizeForEmailClients: boolean;
  rtlSupport: boolean;
  minify: boolean;
}
