-- Base Pedro Sounds (built-in)
BasePedroSounds = {
    {name = "Pedro",      path = "Interface\\AddOns\\Pedro\\Media\\Pedro.ogg",      duration = 9},
    {name = "PedroLong",  path = "Interface\\AddOns\\Pedro\\Media\\PedroLong.ogg",  duration = 40},
    {name = "GoldenKpop", path = "Interface\\AddOns\\Pedro\\Media\\GoldenKpop.ogg", duration = 48},
}

-- Runtime list (base + up to 5 custom)
PedroSounds = {}
PedroSongLengths = {}

function RebuildPedroSounds()
    wipe(PedroSounds)
    wipe(PedroSongLengths)
	
    -- Add built-in sounds
    for _, s in ipairs(BasePedroSounds) do
        table.insert(PedroSounds, s)
	end
	
    -- Add up to 5 custom songs
    if G and G.customSongs then
        local count = 0
        for _, s in ipairs(G.customSongs) do
            count = count + 1
            if count > 5 then break end
            table.insert(PedroSounds, s)
		end
	end
	
    -- Build duration lookup
    for _, sound in ipairs(PedroSounds) do
        if sound.path and sound.duration then
            PedroSongLengths[sound.path] = sound.duration
		end
	end
end

-- Resize main UI based on number of sounds
function ResizePedroMainUI()
    if not PedroMainUI then return end
	
    local perCustom = 24
    local baseHeight = 520
	
    local totalSounds = #PedroSounds
    local baseSounds = (BasePedroSounds and #BasePedroSounds) or 3
	
    local customCount = totalSounds - baseSounds
    if customCount < 0 then customCount = 0 end
	
    local newHeight = baseHeight + (customCount * perCustom)
	
    if newHeight < baseHeight then newHeight = baseHeight end
    if newHeight > 700 then newHeight = 700 end
	
    PedroMainUI:SetHeight(newHeight)
end

-- Get length of selected song
function GetSelectedSongLength()
    return PedroSongLengths[G.selectedSound] or 40
end

-- Core state variables
lustActive = false
canPlay = true
lastTriggerTime = nil
cooldown = 1.5
lustGuardDuration = 48 -- seconds between allowed Pedro triggers

pedroAudioChannel = G.audioChannel or "Master"
pedroPlayingID = nil

-- Reset cooldown
local function ResetCooldown()
    canPlay = true
end

-- Play Pedro sound if allowed
function PlayPedroIfAllowed(ignoreGuard)
    if not ignoreGuard then
        if not canPlay then return false end
        if lastTriggerTime and (GetTime() - lastTriggerTime) < lustGuardDuration then return false end
	end
	
    if type(pedroPlayingID) == "number" then
        StopSound(pedroPlayingID)
        pedroPlayingID = nil
	end
	
    local id = PlaySoundFile(G.selectedSound, pedroAudioChannel)
    if not id then
        print("Pedro: Failed to play sound. File missing or invalid.")
        return false
	end
	
    pedroPlayingID = id
	
    if not ignoreGuard then
        canPlay = false
        lastTriggerTime = GetTime()
        C_Timer.After(cooldown, ResetCooldown)
	end
	
    return true
end

-- Main trigger logic
function TryPlayPedro()
    if not G.enabled then return false end
	
    local songLength = GetSelectedSongLength()
    local played = false
	
    local hasDebuff = CheckLustDebuffByID()
    lustActive = hasDebuff
	
    if hasDebuff then
        if not isAnimating then
            local playedSound = PlayPedroIfAllowed()
            if playedSound and not G.disableAnimation then
                StartAnimation(songLength)
			end
            played = playedSound
		end
	end
	
    return played
end

-- Manual play
function TryPlayPedroManual()
    if not G.enabled then
        print("Pedro: Addon is disabled.")
        return
	end
	
    PlayPedroIfAllowed(true)
	
    local soundName = "Unknown"
    for _, sound in ipairs(PedroSounds) do
        if sound.path == G.selectedSound then
            soundName = sound.name
            break
		end
	end
	
    print("Pedro: Played " .. soundName .. " manually.")
	
    if not G.disableAnimation then
        local songLength = GetSelectedSongLength()
        StartAnimation(songLength)
	end
end