import type { Db } from "../db.js"; import { nowIso } from "../db.js"; /** * Katalog nóżek/stopek meblowych. ai_prompt trafia do promptu generacji * ujęć (podmiana nóg na wybrane wykończenie); finish_type to etykieta * wykończenia dla operatora/eksportu. */ export interface FurnitureLegRow { id: string; name: string; ai_prompt: string | null; finish_type: string | null; image_asset_id: string | null; height_cm: number | null; material: string | null; source_url: string | null; created_at: string; } export function upsertFurnitureLeg( db: Db, input: { id: string; name: string; aiPrompt?: string | null; finishType?: string | null; imageAssetId?: string | null; heightCm?: number | null; material?: string | null; sourceUrl?: string | null; } ): FurnitureLegRow { db.prepare( `INSERT INTO furniture_legs (id, name, ai_prompt, finish_type, image_asset_id, height_cm, material, source_url, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT (id) DO UPDATE SET name = excluded.name, ai_prompt = COALESCE(excluded.ai_prompt, furniture_legs.ai_prompt), finish_type = COALESCE(excluded.finish_type, furniture_legs.finish_type), image_asset_id = COALESCE(excluded.image_asset_id, furniture_legs.image_asset_id), height_cm = COALESCE(excluded.height_cm, furniture_legs.height_cm), material = COALESCE(excluded.material, furniture_legs.material), source_url = COALESCE(excluded.source_url, furniture_legs.source_url)` ).run( input.id, input.name, input.aiPrompt ?? null, input.finishType ?? null, input.imageAssetId ?? null, input.heightCm ?? null, input.material ?? null, input.sourceUrl ?? null, nowIso() ); return getFurnitureLeg(db, input.id)!; } /** Generuje identyfikator nóżki z nazwy — „leg_" + slug (np. "Czarny walec" → "leg_czarny-walec"). */ export function legIdFromName(name: string): string { const slug = name .replace(/ł/g, "l") .replace(/Ł/g, "L") // ł nie dekompozytuje się w NFD — mapowanie ręczne .normalize("NFD") .replace(/[̀-ͯ]/g, "") .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 60); return `leg_${slug || "nowa"}`; } export function getFurnitureLeg(db: Db, id: string): FurnitureLegRow | null { const row = db.prepare("SELECT * FROM furniture_legs WHERE id = ?").get(id) as unknown as | FurnitureLegRow | undefined; return row ?? null; } export function listFurnitureLegs(db: Db): FurnitureLegRow[] { return db.prepare("SELECT * FROM furniture_legs ORDER BY name").all() as unknown as FurnitureLegRow[]; }