import { Database } from 'bun:sqlite';
import path from 'path';

const dbUrl = process.env.DATABASE_URL;
if (!dbUrl) throw new Error('DATABASE_URL is not set');

const dbPath = dbUrl.startsWith('file:') ? dbUrl.slice(5) : dbUrl;
const db = new Database(path.resolve(dbPath));

const duplicates = db.query(`
  SELECT anonymous_id, date, COUNT(*) as count
  FROM daily_completions
  GROUP BY anonymous_id, date
  HAVING count > 1
`).all() as { anonymous_id: string; date: string; count: number }[];

if (duplicates.length === 0) {
  console.log('No duplicates found.');
  process.exit(0);
}

console.log(`Found ${duplicates.length} duplicate group(s):`);

const deleteStmt = db.query(`
  DELETE FROM daily_completions
  WHERE anonymous_id = $anonymous_id AND date = $date
    AND id NOT IN (
      SELECT id FROM daily_completions
      WHERE anonymous_id = $anonymous_id AND date = $date
      ORDER BY completed_at ASC
      LIMIT 1
    )
`);

for (const { anonymous_id, date, count } of duplicates) {
  deleteStmt.run({ $anonymous_id: anonymous_id, $date: date });
  console.log(`  ${anonymous_id} / ${date}: kept earliest, deleted ${count - 1} row(s) (had ${count})`);
}

console.log('Done.');
db.close();
