import type {
  FilterPresetId,
  ImageAdjustments,
  ImageLayer,
} from "@/lib/image-editor-types";
import { DEFAULT_ADJUSTMENTS } from "@/lib/image-editor-types";

function isRecord(value: unknown): value is Record<string, unknown> {
  return typeof value === "object" && value !== null && !Array.isArray(value);
}

function parseImageLayer(value: unknown): ImageLayer | null {
  if (!isRecord(value)) return null;
  if (typeof value.id !== "string" || typeof value.type !== "string") return null;
  return {
    id: value.id,
    type: value.type as ImageLayer["type"],
    name: typeof value.name === "string" ? value.name : undefined,
    visible: typeof value.visible === "boolean" ? value.visible : true,
    x: typeof value.x === "number" ? value.x : 0,
    y: typeof value.y === "number" ? value.y : 0,
    width: typeof value.width === "number" ? value.width : 240,
    height: typeof value.height === "number" ? value.height : 120,
    rotation: typeof value.rotation === "number" ? value.rotation : 0,
    opacity: typeof value.opacity === "number" ? value.opacity : 1,
    zIndex: typeof value.zIndex === "number" ? value.zIndex : 0,
    props: isRecord(value.props) ? value.props : {},
  };
}

function parseAdjustments(value: unknown): ImageAdjustments {
  if (!isRecord(value)) return { ...DEFAULT_ADJUSTMENTS };
  return {
    brightness:
      typeof value.brightness === "number"
        ? value.brightness
        : DEFAULT_ADJUSTMENTS.brightness,
    contrast:
      typeof value.contrast === "number"
        ? value.contrast
        : DEFAULT_ADJUSTMENTS.contrast,
    saturation:
      typeof value.saturation === "number"
        ? value.saturation
        : DEFAULT_ADJUSTMENTS.saturation,
    hue: typeof value.hue === "number" ? value.hue : DEFAULT_ADJUSTMENTS.hue,
    blur: typeof value.blur === "number" ? value.blur : DEFAULT_ADJUSTMENTS.blur,
    sharpen:
      typeof value.sharpen === "number"
        ? value.sharpen
        : DEFAULT_ADJUSTMENTS.sharpen,
    vignette:
      typeof value.vignette === "number"
        ? value.vignette
        : DEFAULT_ADJUSTMENTS.vignette,
  };
}

export function isImageSceneDataEmpty(sceneData: Record<string, unknown>): boolean {
  if (!Array.isArray(sceneData.layers) || sceneData.layers.length === 0) {
    return true;
  }
  return false;
}

export interface ImagePersistedSceneData {
  canvasWidth: number;
  canvasHeight: number;
  layers: ImageLayer[];
  adjustments: ImageAdjustments;
  activeFilterPreset: FilterPresetId;
}

export function buildImageSceneDataPayload(
  input: ImagePersistedSceneData
): Record<string, unknown> {
  return {
    canvasWidth: input.canvasWidth,
    canvasHeight: input.canvasHeight,
    layers: input.layers,
    adjustments: input.adjustments,
    activeFilterPreset: input.activeFilterPreset,
  };
}

export function parseImageSceneData(
  sceneData: Record<string, unknown>
): ImagePersistedSceneData | null {
  const layers = Array.isArray(sceneData.layers)
    ? sceneData.layers
        .map(parseImageLayer)
        .filter((layer): layer is ImageLayer => layer !== null)
    : [];

  if (layers.length === 0) return null;

  return {
    canvasWidth:
      typeof sceneData.canvasWidth === "number" ? sceneData.canvasWidth : 1080,
    canvasHeight:
      typeof sceneData.canvasHeight === "number" ? sceneData.canvasHeight : 1080,
    layers,
    adjustments: parseAdjustments(sceneData.adjustments),
    activeFilterPreset:
      typeof sceneData.activeFilterPreset === "string"
        ? (sceneData.activeFilterPreset as FilterPresetId)
        : "original",
  };
}
