-- NOTE: Beautification tool not available. Original content preserved.

-- Määrame skeemi keskkonnamuutujast või vaikimisi 'public'
local schema = os.getenv("PGSCHEMA") or "public"
local tables = {}

-- Define the "countries" locator and get all country geometries from the
-- database. Use the import-countries.lua file to import them first, before
-- you run this.
local countries = osm2pgsql.define_locator({name = "countries"})
countries:add_from_db("SELECT code, ST_Subdivide(geom, 200) FROM " .. schema .. ".countries")

tables.buildings =
    osm2pgsql.define_way_table(
    "buildings",
    {
        -- Add a SERIAL column and tell osm2pgsql not to fill it (PostgreSQL will
        -- do that for us)
        {column = "id", sql_type = "serial", create_only = true},
        -- We always need a highway type, so we can declare the column as NOT NULL
        -- This will contain the country code of the first matching country
        -- (which can be any of the countries because there is no order here).
        {column = "country", type = "text"},
        {column = "city", type = "text"},
        {column = "street", type = "text"},
        {column = "housenumber", type = "text"},
        {column = "housename", type = "text"},
        {column = "postcode", type = "text"},
        {column = "name", type = "text"},
        {column = "place", type = "text"},
        {column = "amenity", type = "text"},
        {column = "height", type = "int"},
        {column = "use", type = "text"},
        {column = "levels", type = "int"},
        {column = "tags", type = "jsonb"},
        {column = "geom", type = "polygon", not_null = true}
    },
    {
        schema = schema,
        proj = 3301
    }
)

-- Helper function to remove some of the tags we usually are not interested in.
-- Something like this can be useful if you are writing all tags to the
-- database in a JSON(B) column and don't want that cluttered with lots of tags
-- nobody cares about. Returns true if there are no tags left.
local function clean_tags(tags)
  local keys_to_remove = {
    "shelter"
  }
  for _, key in ipairs(keys_to_remove) do
    tags[key] = nil
  end
  return next(tags) == nil
end
-- Mittevajalike tag'ide eemaldamine
local function hide_tags(tags)
  local keys_to_hide = {
    "odbl", "created_by", "source", "source:ref", "source:addr",
    "addr:country", "addr:city", "addr:street", "addr:housenumber", "addr:housename",
    "addr:postcode", "height", "building", "maaamet:ETAK", "building:use",
    "building:levels", "old_addr:street", "old_addr:housenumber", "source:addr:2023",
    "maaamet:orig_tunnus", "wikidata", "old_addr:housename", "old_addr:place",
    "name", "phone", "internet_access", "wheelchair", "roof:shape",
    "name:en", "name:ru", "brand", "fuel", "website", "alt_name",
    "capacity", "operator", "fuel:diesel", "self_service", "opening_hours",
    "brand:wikidata", "fuel:octane_95", "fuel:octane_98", "brand:wikipedia",
    "roof:material", "email", "name:et", "roof", "stars", "fee",
    "old_name", "name:fi", "description:ru", "url", "name:lt",
    "addr:place", "roof:levels", "addr:name", "name:pl", "name:lv",
    "payment:credit_cards", "payment:debit_cards", "amenity", "tourism",
    "shop", "man_made", "addr:old_street", "operator:wikidata",
    "fuel:adblue", "contact:email", "leisure", "contact:phone",
    "contact:website", "healthcare", "maaamet:ADS", "name:vro",
    "check_date", "building:material", "owner:ru", "int_name", "wikipedia", "power", 
	"fuel:HGV_diesel", "branch", "EE:EHIS:id", "operator:type", "operator:type",
	"layer"
  }
  for _, key in ipairs(keys_to_hide) do
    tags[key] = nil
  end
  return next(tags) == nil
end


function osm2pgsql.process_way(object)
    -- Get the type of "building" and remove it from the tags
    local building = object.tags.building
    -- Only with geometry
    local geom = object:as_polygon()
    if not geom then
        return
    end
    -- Only Estonia
    local cc = countries:first_intersecting(geom)
    if cc ~= "EE" then
        return
    end

    if building then
	    local city = object.tags["addr:city"]
        local street = object.tags["addr:street"]
        local housenumber = object.tags["addr:housenumber"]
		local housename = object.tags["addr:housename"]
        local postcode = object.tags["addr:postcode"]
        local name = object.tags["name"] or object.tags["addr:name"]
        local place = object.tags["addr:place"]
        local amenity = object.tags["amenity"] or object.tags["tourism"] or object.tags["shop"] 
			or object.tags["man_made"] or object.tags["leisure"] or object.tags["healthcare"]
			or object.tags["power"]
		local use = object.tags["building:use"]
        local levels = object.tags["building:levels"]		
        local tags = object.tags
        local height = object.tags.height
        -- the way
        local row = {
            country = cc,
            city = city,
            street = street,
            housenumber = housenumber,
			housename = housename,
            postcode = postcode,
            name = name,
			place = place,
			amenity = amenity,
			height = height,
            use = use,
			levels = levels,
            tags = tags,
            geom = geom
        }
    if clean_tags(object.tags) then
        return
    end
		-- hide_tags(object.tags)
        tables.buildings:insert(row)
    end
end
