import { test, expect } from 'bun:test';
import { prisma } from '../src/prisma/db';
import xprisma from '../src/dao/xprisma';


test('Test add location', async () => {
  const lat = 39.9042; // 纬度
  const lng = 116.4074; // 经度
  // 注意：MySQL 8.0 推荐顺序是 POINT(经度 纬度)
  const logto_uid = 'test';
  const result = await xprisma.userLocation.createCustom(logto_uid, '北京天安门', { lat, lng });
  const result2 = await xprisma.userLocation.createCustom(logto_uid, '北京天安门2', { lat: lat + 0.005, lng: lng + 0.007 });
  const result3 = await xprisma.userLocation.createCustom(logto_uid, 'test 2', { lat: 40.33, lng: 88.555 });
  expect(result).toBeDefined();
  expect(result2).toBeDefined();
  expect(result3).toBeDefined();
  const locations = await xprisma.userLocation.findManyWihtLocation(logto_uid);
  const data = {
    kaiqiu_uid: '1234',
    avatar: 'https://p3-sign.douyinpic.com/obj/douyin-user-image-file/3da637dde522b3a4bc0c5e8be5bea173?lk3s=7b078dd2&x-expires=1773838800&x-signature=mmUrSnUnta67cH%2B5rgBJXzdp4IA%3D&from=2064092626&s=sticker_comment&se=false&sc=sticker_heif&biz_tag=aweme_comment&l=20260318155244DA8F89CAF9B7CB54B2A9',
    // avatar: 'avatar 111',
  };
  const ids = locations.map(e => e.id);
  await prisma.userLocation.updateMany({
    where: { id: { in: ids }},
    data,
  });
  const id = ids[0] as number;
  const updateNoMatch = await prisma.userLocation.update({
    where: { id: id, logto_uid: 'no match' },
    data: { avatar: '222' }
  }).catch(() => null);
  expect(updateNoMatch).toBe(null);
  console.debug('updatematch', updateNoMatch);
  await xprisma.userLocation.updateLocation(id, {
    lat: lat - 0.001,
    lng: lng + 0.001,
  });
  expect(locations.length).toBe(3);
  const neerby = await xprisma.userLocation.findNearby({
    lat: lat - 0.001,
    lng: lng - 0.003,
  }, 500).catch();
  console.debug('location', locations);
  console.debug('neerby', neerby);
  expect(neerby).toBeArray();
  await prisma.userLocation.deleteMany({
    where: {
      id: { in: locations.map(e => e.id) },
    }
  });
});