-- ==============================
-- verb_tense.lua
-- ==============================

local _, ns = ...
local VerbTense = {}
ns.VerbTense = VerbTense

-- Развёртка сокращений
function VerbTense:Expand_contractions(text)
    local s = text:lower()
    for short, full in pairs(ns.Contractions.data) do
        s = s:gsub("%f[%a]"..short.."%f[%A]", full)
    end
    return s
end

-- Проверка наличия глагола (правильные + неправильные)
local function has_verb(s)
    -- Проверка по словарю неправильных
    for base, forms in pairs(ns.Irregular_verbs.data) do
        if s:find("%f[%a]"..base.."%f[%A]") then return true end
        for _, f in ipairs(forms) do
            if s:find("%f[%a]"..f.."%f[%A]") then return true end
        end
    end

    -- Проверка на окончания правильных глаголов
    if s:find("%a+ed%f[%A]") then return true end   -- worked, played
    if s:find("%a+ing%f[%A]") then return true end  -- working, testing
    if s:find("%a+s%f[%A]") then return true end    -- works, plays

    return false
end

-- Определение времени и залога
function VerbTense:DetectVerbTense(sentence)
    local s = VerbTense:Expand_contractions(sentence)

    -- Проверка на наличие глагола
    if not has_verb(s) then
        return { time="None", aspect="None", voice="None", note="Нет глагола — время не определяется" }
    end

    -- Проверка на пассивный залог: "be + V3"
    local passive = false
    if s:find("am ") or s:find("is ") or s:find("are ") or
       s:find("was ") or s:find("were ") or
       s:find("be ") or s:find("been ") or s:find("being ") then
        for _, forms in pairs(ns.Irregular_verbs.data) do
            if s:find(forms[2]) then passive = true end
        end
        if s:find("ed") or s:find("en") then passive = true end
    end

    -- Future
    if s:find("will have been") then
        return { time="Future", aspect="Perfect Continuous", voice=passive and "Passive" or "Active" }
    elseif s:find("will have") then
        return { time="Future", aspect="Perfect", voice=passive and "Passive" or "Active" }
    elseif s:find("will be") then
        return { time="Future", aspect="Continuous", voice=passive and "Passive" or "Active" }
    elseif s:find("will ") then
        return { time="Future", aspect="Simple", voice=passive and "Passive" or "Active" }
    end

    -- Present Perfect Continuous
    if s:find("have been ") or s:find("has been ") then
        if s:find("ing") then
            return { time="Present", aspect="Perfect Continuous", voice=passive and "Passive" or "Active" }
        else
            return { time="Present", aspect="Perfect", voice=passive and "Passive" or "Active" }
        end
    end

    -- Past Perfect Continuous
    if s:find("had been ") then
        if s:find("ing") then
            return { time="Past", aspect="Perfect Continuous", voice=passive and "Passive" or "Active" }
        else
            return { time="Past", aspect="Perfect", voice=passive and "Passive" or "Active" }
        end
    end

    -- Continuous
    if s:find("am ") or s:find("is ") or s:find("are ") then
        if s:find("ing") then
            return { time="Present", aspect="Continuous", voice=passive and "Passive" or "Active" }
        end
    elseif s:find("was ") or s:find("were ") then
        if s:find("ing") then
            return { time="Past", aspect="Continuous", voice=passive and "Passive" or "Active" }
        end
    end

    -- Simple / Irregular Past
    for base, forms in pairs(ns.Irregular_verbs.data) do
        if s:find("%f[%a]"..forms[1].."%f[%A]") then
            return { time="Past", aspect="Simple", voice="Active", note="Irregular form" }
        elseif s:find("%f[%a]"..forms[2].."%f[%A]") and s:find("have") then
            return { time="Present/Past", aspect="Perfect", voice=passive and "Passive" or "Active", note="Irregular form" }
        end
    end

    if s:find("ed") or s:find("did ") then
        return { time="Past", aspect="Simple", voice=passive and "Passive" or "Active" }
    elseif s:find("s ") or s:find("s$") then
        return { time="Present", aspect="Simple", voice=passive and "Passive" or "Active" }
    end

    return { time="Unknown", aspect="Unknown", voice="Unknown", note="Глагол найден, но время не распознано" }
end