import { Router } from 'express' import { getDb } from '../db/connection.js' import type { Product, SizeStock } from '../../shared/types.js' export const productsRouter = Router() interface ProductRow { id: string; name: string; slug: string; description: string; price_sats: number; images: string; sizes: string; category: string; is_active: number; created_at: string; updated_at: string } export function rowToProduct(row: ProductRow): Product { return { id: row.id, name: row.name, slug: row.slug, description: row.description, priceSats: row.price_sats, images: JSON.parse(row.images) as string[], sizes: JSON.parse(row.sizes) as SizeStock[], category: row.category, isActive: row.is_active === 1, createdAt: row.created_at, updatedAt: row.updated_at } } productsRouter.get('/', (req, res) => { const db = getDb() const category = req.query.category as string | undefined let rows: ProductRow[] if (category) { rows = db.prepare('SELECT * FROM products WHERE is_active = 1 AND category = ? ORDER BY created_at DESC').all(category) as ProductRow[] } else { rows = db.prepare('SELECT * FROM products WHERE is_active = 1 ORDER BY created_at DESC').all() as ProductRow[] } res.json(rows.map(rowToProduct)) }) productsRouter.get('/:slug', (req, res) => { const db = getDb() const row = db.prepare('SELECT * FROM products WHERE slug = ? AND is_active = 1').get(req.params.slug) as ProductRow | undefined if (!row) { res.status(404).json({ error: { code: 'NOT_FOUND', message: 'Product not found' } }); return } res.json(rowToProduct(row)) })