import { del } from '../../../lib/object-storage.js';
import { sql } from '../../../lib/sql.js';
import { getUserFromRequest } from '../../../lib/permission-middleware';

export default async function handler(req, res) {
  try {
    const user = await getUserFromRequest(req);
    if (!user) {
      return res.status(401).json({ error: 'Authentication required' });
    }

    const symbolId = parseInt(req.query.id, 10);
    if (!Number.isInteger(symbolId)) {
      return res.status(400).json({ error: 'Invalid symbol id' });
    }

    if (req.method !== 'DELETE') {
      return res.status(405).json({ error: 'Method not allowed' });
    }

    const found = await sql`
      SELECT id, image_url FROM custom_symbols
      WHERE id = ${symbolId} AND user_id = ${user.userId}
    `;
    if (found.rows.length === 0) {
      return res.status(404).json({ error: 'Symbol not found' });
    }

    try {
      await del(found.rows[0].image_url);
    } catch (blobError) {
      console.warn('Failed to delete symbol image:', blobError);
    }

    await sql`DELETE FROM custom_symbols WHERE id = ${symbolId}`;
    return res.status(200).json({ message: 'Symbol deleted' });
  } catch (error) {
    console.error('Custom symbol API error:', error);
    return res.status(500).json({ error: 'Internal server error' });
  }
}
