-- core/Main.lua
-- WoW-only entry point. On login it builds the display window (2c-2). The
-- window itself reads the clock via Clock.cityNow, so the schedule reflects
-- the selected city's own "today".

local Window = require("Window")
local Picker = require("Picker")
local Wizard = require("Wizard")
local Minimap = require("Minimap")
local Theme = require("Theme")
local Selection = require("Selection")

local loader = CreateFrame("Frame")
loader:RegisterEvent("PLAYER_LOGIN")
loader:SetScript("OnEvent", function()
  PrayerTimesDB = PrayerTimesDB or {}
  Theme.init(PrayerTimesDB) -- pick the saved palette before any window is built
  Window.init(PrayerTimesDB)
  Picker.init(PrayerTimesDB)
  Wizard.init(PrayerTimesDB)
  Minimap.init(PrayerTimesDB)
  Window.create()
  Minimap.create()
  if Wizard.shouldOpen(PrayerTimesDB) then
    Wizard.open() -- first run: guided welcome wizard (ADR-0006)
    print("|cff33ff99MyPrayerTimes|r: welcome! Let's get you set up.")
  else
    print("|cff33ff99MyPrayerTimes|r loaded. |cffffd100/pt settings|r to change city.")
  end
end)

-- Slash commands.
SLASH_PRAYERTIMES1 = "/pt"
SLASH_PRAYERTIMES2 = "/prayertimes"
SlashCmdList["PRAYERTIMES"] = function(msg)
  local cmd, rest = (msg or ""):match("^%s*(%S*)%s*(.-)%s*$")
  cmd = (cmd or ""):lower()

  if cmd == "" or cmd == "help" then
    print("|cff33ff99MyPrayerTimes|r commands:")
    print("  |cffffd100/pt show|r / |cffffd100hide|r - show or hide the window")
    print("  |cffffd100/pt lock|r / |cffffd100unlock|r - lock or free its position")
    print("  |cffffd100/pt settings|r - open the city / settings window")
    print("  |cffffd100/pt setup|r - run the welcome wizard again")
    print("  |cffffd100/pt minimap|r - show or hide the minimap button")
    print("  |cffffd100/pt theme light|dark|r - switch the colour theme")
    print("  |cffffd100/pt city <name>|r - select a city by name")
    print("  |cffffd100/pt test|r - preview a notification")
  elseif cmd == "show" then
    if Window.frame then Window.frame:Show() end
  elseif cmd == "hide" then
    if Window.frame then Window.frame:Hide() end
  elseif cmd == "lock" then
    Window.setLocked(true); print("|cff33ff99MyPrayerTimes|r: window locked")
  elseif cmd == "unlock" then
    Window.setLocked(false); print("|cff33ff99MyPrayerTimes|r: window unlocked")
  elseif cmd == "settings" or cmd == "config" or cmd == "options" then
    Picker.toggle()
  elseif cmd == "welcome" or cmd == "setup" then
    Wizard.open() -- re-run the welcome/setup wizard on demand
  elseif cmd == "minimap" then
    Minimap.toggle()
    print("|cff33ff99MyPrayerTimes|r: minimap button "
      .. ((PrayerTimesDB.minimap and PrayerTimesDB.minimap.hide) and "hidden" or "shown"))
  elseif cmd == "theme" then
    local name = (rest or ""):lower()
    if name == "light" or name == "dark" then
      Theme.set(name); print("|cff33ff99MyPrayerTimes|r: theme set to " .. name)
    else
      print("|cff33ff99MyPrayerTimes|r: usage |cffffd100/pt theme light|r or |cffffd100dark|r (now: " .. Theme.current() .. ")")
    end
  elseif cmd == "city" then
    if rest == "" then
      Picker.open()
    else
      local matched = Picker.selectCityByName(rest)
      if matched then
        print("|cff33ff99MyPrayerTimes|r: city set to " .. matched)
      else
        -- Escape "|" so a typed escape sequence prints literally, not as a
        -- colour/hyperlink/texture in the chat frame.
        local safe = (rest:gsub("|", "||"))
        print("|cffff5555MyPrayerTimes|r: unknown city '" .. safe .. "' - try /pt settings")
      end
    end
  elseif cmd == "test" then
    Window.testNotification()
  else
    print("|cffff5555MyPrayerTimes|r: unknown command - try |cffffd100/pt help|r")
  end
end
