---@class aura_env
---@field achievements table<number, string>
---@field Scan fun(playerName: string)
---@field TryInspect fun()

---@class WeakAurasSaved
---@field Cyka table<string, table<string, table<number, string>>>

if not WeakAurasSaved.Cyka then WeakAurasSaved.Cyka = {} end
if not WeakAurasSaved.Cyka.AchievementSniffer then WeakAurasSaved.Cyka.AchievementSniffer = {} end
if not WeakAurasSaved.Cyka.AchievementSniffer2 then WeakAurasSaved.Cyka.AchievementSniffer2 = {} end
for k, v in pairs(WeakAurasSaved.Cyka.AchievementSniffer2) do
	if WeakAurasSaved.Cyka.AchievementSniffer[k] then WeakAurasSaved.Cyka.AchievementSniffer[k] = nil end
end

aura_env.achievements = {
	15,
	153,
	245,
	506,
	507,
	513,
	524,
	648,
	649,
	651,
	655,
	657,
	660,
	661,
	667,
	668,
	669,
	671,
	676,
	678,
	681,
	682,
	697,
	777,
	958,
	974,
	975,
	1017,
	1266,
	1556,
	1557,
	1558,
	1576,
	2078,
	2141,
	2200,
	4477,
	4478,
	4624,
	4914,
	4958,
	4960,
	5455,
	5456,
	5749,
	5752,
	6456,
	6460,
	6753,
	6757,
	6758,
	6835,
	7382,
	7383,
	7384,
	7437,
	7948,
	8929,
	8952,
	8956,
	8966,
	8967,
	8969,
	8978,
	8979,
	8980,
	8981,
	8982,
	8983,
	9017,
	9038,
	9132,
	9493,
	9507,
	10059,
	10079,
	10278,
	10460,
	10581,
	10585,
	10595,
	10597,
	10617,
	10657,
	10666,
	10667,
	10668,
	10672,
	10682,
	10684,
	10688,
	10689,
	10692,
	10693,
	10698,
	10706,
	10746,
	10755,
	10756,
	10763,
	10772,
	10775,
	10790,
	10800,
	10818,
	10819,
	10820,
	10875,
	10994,
	11124,
	11126,
	11127,
	11128,
	11153,
	11157,
	11164,
	11188,
	11189,
	11190,
	11214,
	11220,
	11298,
	11338,
	11394,
	11446,
	11473,
	11545,
	11559,
	11610,
	11611,
	11653,
	11657,
	11658,
	11659,
	11660,
	11674,
	11773,
	11787,
	11789,
	11790,
	11796,
	11992,
	11993,
	11994,
	11995,
	11996,
	11997,
	11998,
	11999,
	12000,
	12001,
	12008,
	12015,
	12020,
	12026,
	12028,
	12030,
	12072,
	12074,
	12085,
	12086,
	12103,
	12110,
	12445,
	12447,
	12448,
}

aura_env.TryInspect = function()
	local targetPlayer = UnitIsPlayer("target")
	if not targetPlayer then return end
	local targetName = UnitName("target")

	local should = false
	if not WeakAurasSaved.Cyka.AchievementSniffer[targetName] then should = true end
	if WeakAurasSaved.Cyka.AchievementSniffer2[targetName] then should = false end
	if aura_env.config.rescan then should = true end
	if not should then return end

	local canInspect = CheckInteractDistance("target", 1)
	if canInspect then SetAchievementComparisonUnit("target") end
end

---@param playerName string
aura_env.Scan = function(playerName)
	WeakAurasSaved.Cyka.AchievementSniffer[playerName] = {}
	for i, aid in ipairs(aura_env.achievements) do
		local completed, month, day, year = GetAchievementComparisonInfo(aid)
		if completed then
			---@type string
			local yearstr = "" .. year
			if year < 100 then yearstr = "20" .. year end

			local date = string.format("%04d-%02d-%02d", yearstr, month, day)

			local data = {
				id = aid,
				date = date,
				completed = completed,
			}
			WeakAurasSaved.Cyka.AchievementSniffer[playerName][aid] = data
		end
	end
end

--/run WeakAurasSaved.Cyka.AchievementSniffer = {}
--/dump WeakAurasSaved.Cyka.AchievementSniffer["Pinkleta"]
