import assert from "node:assert/strict";
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import type {
  ExifMetadata,
  ImageMetadataSuggestion,
  VisionAIResult,
} from "./vision.ts";
import { getImagesToProcess, mergeMetaAndVisionData } from "./vision.ts";

const FINAL: ImageMetadataSuggestion = {
  id: "2R9A2805",
  title: [
    "Blossom and Buzz",
    "Spring's Gentle Awakening",
    "Cherry Blossom Haven",
    "Nature's Delicate Balance",
    "A Bee's Spring Feast",
  ],
  image: "./2R9A2805.jpg",
  alt: "Close-up of vibrant pink cherry blossoms on a branch with a honeybee collecting nectar. The bee's wings are slightly blurred, capturing its motion as it works. The background is a soft, dreamy pink hue, complementing the sharp details of the blossoms and the bee.",
  location: "48 deg 8' 37.56\" N, 11 deg 34' 13.32\" E",
  date: "2024-03-17",
  tags: ["nature", "cherryblossom", "bee", "spring", "floral"],
  exif: {
    camera: "Canon EOS R6m2",
    lens: "RF70-200mm F2.8 L IS USM",
    aperture: "2.8",
    iso: "125",
    focal_length: "200.0",
    shutter_speed: "1/1000",
  },
};

const VISION_DATA: VisionAIResult = {
  title_ideas: [
    "Blossom and Buzz",
    "Spring's Gentle Awakening",
    "Cherry Blossom Haven",
    "Nature's Delicate Balance",
    "A Bee's Spring Feast",
  ],
  description:
    "Close-up of vibrant pink cherry blossoms on a branch with a honeybee collecting nectar. The bee's wings are slightly blurred, capturing its motion as it works. The background is a soft, dreamy pink hue, complementing the sharp details of the blossoms and the bee.",
  tags: ["nature", "cherryblossom", "bee", "spring", "floral"],
};

const EXIF_DATA: ExifMetadata = {
  SourceFile: "/Users/flori/Sites/flori-dev/src/content/grid/2R9A2805.jpg",
  FileName: "2R9A2805.jpg",
  Model: "Canon EOS R6m2",
  ExposureTime: "1/1000",
  FNumber: 2.8,
  ISO: 125,
  DateTimeOriginal: "2024:03:17 15:06:16",
  FocalLength: "200.0 mm",
  LensModel: "RF70-200mm F2.8 L IS USM",
  GPSPosition: "48 deg 8' 37.56\" N, 11 deg 34' 13.32\" E",
};

async function main() {
  const tempRoot = await mkdtemp(join(tmpdir(), "vision-photos-"));

  try {
    assert.deepEqual(mergeMetaAndVisionData(EXIF_DATA, VISION_DATA), FINAL);

    const albumDirectory = join(tempRoot, "chiang-mai");
    const missingImage = join(albumDirectory, "2025-10-06-121017.jpg");
    const completeImage = join(albumDirectory, "2025-10-06-121212.jpg");

    await mkdir(albumDirectory, { recursive: true });
    await writeFile(missingImage, "");
    await writeFile(completeImage, "");
    await writeFile(join(albumDirectory, "2025-10-06-121212.json"), "{}");

    assert.deepEqual(await getImagesToProcess(tempRoot), [missingImage]);
    assert.deepEqual(await getImagesToProcess(tempRoot, { refresh: true }), [
      missingImage,
      completeImage,
    ]);
  } finally {
    await rm(tempRoot, { recursive: true, force: true });
  }
}

await main();
