import { DataSource } from 'typeorm';
export async function handler(dataSource: DataSource, { user, query }) {
  const data = await dataSource.query(`
  select
	(case
		when from_unixtime(min(jj.t / 1000), '%H:%i') <> from_unixtime(max(jj.t / 1000), '%H:%i') 
		then concat(
			from_unixtime(min(jj.t / 1000), '%H:%i'), '-', from_unixtime(max(jj.t / 1000), '%H:%i')
		)
		else from_unixtime(min(jj.t / 1000), '%H:%i') end
		) t,
	jj.c
from
	(
	select
		lmd.timestamp as t,
		lmd.total_user as c
	from
		live_msg_data lmd
	where
		lmd.room_id = ${query.id}
		and lmd.type in ('leave', 'join')
	group by
		round(lmd.timestamp / 1000 / 60)
	) jj
group by
	jj.c
order by
	jj.t
        `);
  // const minTime = Math.min(...data.map((i) => i.t));
  // const maxTime = Math.max(...data.map((i) => i.t));
  // console.log(minTime, maxTime);
  // console.log(new Date(minTime), new Date(maxTime));
  return data;
}
