-- entities/laundering_npc/init.lua
-- Comportement serveur du NPC blanchisseur.
-- Invincible, non-physgun (sauf SuperAdmin), ouvre le menu au USE.

AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")
include("shared.lua")

function ENT:Initialize()
    self:SetModel("models/Humans/Group02/male_02.mdl")
    self:PhysicsInit(SOLID_VPHYSICS)
    self:SetMoveType(MOVETYPE_NONE)
    self:SetSolid(SOLID_VPHYSICS)
    self:SetUseType(SIMPLE_USE)
    self:DropToFloor()

    -- Freeze physics pour qu'il ne tombe pas / ne soit pas poussable
    local phys = self:GetPhysicsObject()
    if IsValid(phys) then
        phys:EnableMotion(false)
        phys:Sleep()
    end

    -- Animation idle : préfère ACT_IDLE générique, fallback sur idle_all_01 (citizens)
    local seq = self:SelectWeightedSequence(ACT_IDLE)
    if not seq or seq < 0 then
        seq = self:LookupSequence("idle_all_01")
    end
    if seq and seq >= 0 then
        self:ResetSequence(seq)
        self:SetCycle(0)
        self:SetPlaybackRate(1)
    end
end

-- Invincible
function ENT:OnTakeDamage(dmg)
    return 0
end

-- Physgun uniquement SuperAdmin
function ENT:PhysgunPickup(ply)
    return IsValid(ply) and ply:IsSuperAdmin()
end

-- Interaction USE : ouvre le menu de blanchiment
function ENT:Use(activator, caller)
    if not IsValid(activator) or not activator:IsPlayer() then return end

    -- Distance check (anti-USE depuis loin)
    local maxDist = Laundering.Config.MaxDistance
    if activator:GetPos():DistToSqr(self:GetPos()) > (maxDist * maxDist) then
        return
    end

    -- Vérification whitelist jobs (si configurée)
    local wl = Laundering.Config.AllowedJobs
    if wl and #wl > 0 then
        local teamName = team.GetName(activator:Team())
        local allowed  = false
        for _, name in ipairs(wl) do
            if name == teamName then allowed = true; break end
        end
        if not allowed then
            DarkRP.notify(activator, 1, 4, "Votre profession ne vous permet pas d'utiliser ce service.")
            return
        end
    end

    -- Envoyer les données au client pour ouvrir le menu
    net.Start("Laundering.OpenMenu")
        net.WriteEntity(self)
        net.WriteUInt(DirtyMoney.Get(activator), 30)
    net.Send(activator)
end
