local tables = {}
local schema = os.getenv("PGSCHEMA") or "public"

-- Tabel halduspiirkondadele
tables.admin = osm2pgsql.define_area_table('administrative_polygon', {
  { column = 'name', type = 'text' },
  { column = 'admin_level', type = 'text' },
  { column = 'boundary', type = 'text' },
  { column = 'geom', type = 'geometry' }
}, {
  schema = schema,
  proj = 3301
})

-- Tabel teedele
tables.roads = osm2pgsql.define_way_table('roads', {
  { column = 'name', type = 'text' },
  { column = 'highway', type = 'text' },
  { column = 'surface', type = 'text' },
  { column = 'geom', type = 'linestring' }
}, {
  schema = schema,
  proj = 3301
})

-- Halduspiirkondade töötlemine
function osm2pgsql.process_area(object)
  print(">> process_area called for relation id:", object.id)
  local boundary = object.tags.boundary
  local admin_level = object.tags.admin_level
  local geom = object:as_polygon()

  if boundary == 'administrative' and admin_level and geom then
    tables.admin:insert({
      name = object.tags.name,
      admin_level = admin_level,
      boundary = boundary,
      geom = geom
    })
  end
end

-- Teede töötlemine
local allowed_types = {
  residential = true,
  primary = true,
  secondary = true,
  tertiary = true,
  secondary_link = true,
  primary_link = true,
  track = true,
  service = true,
  unclassified = true
}

function osm2pgsql.process_way(object)
  local highway = object.tags.highway
  if highway and highway ~= '' and allowed_types[highway] then
    local geom = object:as_linestring()
    if geom then
      tables.roads:insert({
        name = object.tags.name,
        highway = highway,
        surface = object.tags.surface,
        geom = geom
      })
    end
  end
end
