text: from sqlite3 import Connection, Row
  from typing import Optional
  from uuid import UUID
  
  from models import Image, ImageType
  
  from .connection import get_con
  
  
  def create_images_table() -> None:
      query = """
          CREATE TABLE IF NOT EXISTS images (
              id TEXT NOT NULL,
              mime TEXT NOT NULL,
              data BLOB NOT NULL,
              sha256 TEXT NOT NULL,
              desc TEXT,
              PRIMARY KEY (id)
          )
      """
      con = get_con()
      con.execute(query)
      con.commit()
  
  
  def create_file_images_table() -> None:
      query = """
          CREATE TABLE IF NOT EXISTS file_images (
              file_id TEXT NOT NULL,
              image_type TEXT NOT NULL,
              image_id TEXT NOT NULL,
              FOREIGN KEY (file_id) REFERENCES files (id),
              FOREIGN KEY (image_id) REFERENCES images (id),
              PRIMARY KEY (file_id, image_type)
          )
      """
      con = get_con()
      con.execute(query)
      con.commit()
  
  
  def insert_image(con: Connection, image: Image) -> None:
      image_dict = image.dict()
      # Normalize ID field from UUID instance to string
      image_dict["id"] = str(image.id)
      con.execute(
          """
          INSERT INTO images (id, mime, data, sha256, desc)
          VALUES (:id, :mime, :data, :sha256, :desc)
          """,
          image_dict,
      )
  
  
  def get_image_for_row(row: Row) -> Image:
      return Image(
          id=UUID(hex=row[0]), mime=row[1], data=row[2], sha256=row[3], desc=row[4]
      )
  
  
  def fetch_image_by_hash(sha256: str) -> Optional[Image]:
      con = get_con()
      cur = con.execute(
          "SELECT * FROM images WHERE sha256 = ?",
          (sha256,),
      )
      row = cur.fetchone()
      if not row:
          return None
      return get_image_for_row(row)
  
  
  def fetch_image_by_id(id: UUID) -> Optional[Image]:
      con = get_con()
      cur = con.execute(
          "SELECT * FROM images WHERE id = ?",
          (id,),
      )
      row = cur.fetchone()
      if not row:
          return None
      return get_image_for_row(row)
  
  
  def fetch_file_images_by_file() -> dict[UUID, dict[ImageType, UUID]]:
      con = get_con()
      cur = con.execute("SELECT * FROM file_images")
  
      file_images_by_file: dict[UUID, dict[ImageType, UUID]] = {}
      for row in cur:
          file_id = UUID(hex=row[0])
          if file_id not in file_images_by_file:
              file_images_by_file[file_id] = {}
          file_images_by_file[file_id][ImageType(row[1])] = UUID(hex=row[2])
  
      return file_images_by_file
  
  
  def fetch_file_images_for_file(file_id: UUID) -> dict[ImageType, UUID]:
      con = get_con()
      cur = con.execute(
          "SELECT * FROM file_images WHERE file_id = ?",
          (str(file_id),),
      )
      rows = cur.fetchall()
      return {ImageType(row[1]): UUID(hex=row[2]) for row in rows}
  
  
  def insert_file_image(
      con: Connection, file_id: UUID, image_type: ImageType, image_id: UUID
  ) -> None:
      con.execute(
          """
          INSERT INTO file_images (file_id, image_type, image_id)
          VALUES (?, ?, ?)
          """,
          (str(file_id), image_type.value, str(image_id)),
      )
  