ENT.Type = "anim"
ENT.PrintName = "Магазин NPC"
ENT.Category = "[FT] Магазины"
ENT.Spawnable = true
ENT.AdminOnly = true
ENT.RenderGroup = RENDERGROUP_BOTH
ENT.bNoPersist = true
ENT.AutomaticFrameAdvance = true

function ENT:SetupDataTables()
	self:NetworkVar("String", 0, "ShopType")
end

if SERVER then
	function ENT:Initialize()
		self:SetModel("models/humans/group01/male_01.mdl")
		self:PhysicsInit(SOLID_BBOX)
		self:SetMoveType(MOVETYPE_NONE)
		self:SetSolid(SOLID_BBOX)
		self:SetUseType(SIMPLE_USE)
		self:SetCollisionGroup(COLLISION_GROUP_NPC)
		
		-- Установка bbox для корректной коллизии
		self:SetCollisionBounds(Vector(-16, -16, 0), Vector(16, 16, 72))
		
		-- Дефолтный тип магазина
		self:SetShopType("consumables")
		
		local phys = self:GetPhysicsObject()
		if IsValid(phys) then
			phys:EnableMotion(false)
		end

		-- Анимация простоя
		local sequence = self:LookupSequence("idle_subtle")
		if sequence > 0 then
			self:ResetSequence(sequence)
		end
	end
	
	function ENT:Use(activator, caller)
		if not IsValid(activator) or not activator:IsPlayer() then return end
		
		local shopType = self:GetShopType()
		local plugin = ix.plugin.Get("shop")
		local shopData = plugin and plugin.shops[shopType]
		
		if not shopData then
			activator:Notify("Этот магазин не настроен или не имеет товаров (ID: " .. tostring(shopType) .. ")")
			return
		end
		
		net.Start("ixShop_OpenMenu")
			net.WriteString(shopType)
			net.WriteTable(shopData)
		net.Send(activator)
	end
	
	function ENT:Think()
		self:NextThink(CurTime())
		return true
	end
else
	function ENT:Draw()
		self:DrawModel()
		
		local pos = self:GetPos() + Vector(0, 0, 85)
		local ang = Angle(0, LocalPlayer():EyeAngles().y - 90, 90)
		
		local distance = LocalPlayer():GetPos():Distance(self:GetPos())
		if distance > 300 then return end
		
		local shopType = self:GetShopType()
		local plugin = ix.plugin.Get("shop")
		local shopData = plugin and plugin.shops[shopType]
		
		if not shopData then return end
		
		cam.Start3D2D(pos, ang, 0.1)
			-- Название магазина
			draw.SimpleText(shopData.name, "DermaLarge", 0, 0, Color(255, 255, 255), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
			
			-- Подсказка
			draw.SimpleText("Нажмите E для взаимодействия", "DermaDefault", 0, 25, Color(200, 200, 200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
		cam.End3D2D()
	end
end
