-- Useful queries

-- User presence
SELECT
	u.name,
	u.surname,
	CASE WHEN p.will_be_present THEN 'Yes' ELSE 'No' END as will_be_present,
	p.notes, 
	p.created as when_user_answered
FROM presence p 
LEFT JOIN `user` u 
	ON p.user_id = u.id
	
-- User started app
SELECT
	u.name,
	u.surname,
	t.created AS logged_in
FROM token t
LEFT JOIN `user` u 
	ON u.id = t.user_id 
ORDER BY logged_in ASC

-- Report
SELECT 
	u.name,
	u.surname,
	(SELECT t.created FROM token t WHERE t.user_id = u.id ORDER BY t.created ASC LIMIT 1) AS started_app,
	CASE WHEN p.will_be_present IS NULL THEN '⏳' ELSE (
		CASE WHEN p.will_be_present THEN '✅' ELSE '❌' END
		) END as will_be_present,
	p.created as when_user_answered,
	p.notes
FROM `user` u
LEFT JOIN presence p 
	ON p.user_id = u.id
GROUP BY u.id
ORDER BY when_user_answered DESC, started_app DESC

-- Clear user token
DELETE FROM token WHERE user_id = 2


