defmodule Towerops.GeoIP.Location do
  @moduledoc """
  Schema for GeoIP location data (cities, countries, regions).
  Maps geoname_id to location details from MaxMind GeoLite2-City database.
  """
  use Ecto.Schema

  import Ecto.Changeset

  @primary_key {:geoname_id, :integer, autogenerate: false}
  schema "geoip_locations" do
    field :country_code, :string
    field :country_name, :string
    field :city_name, :string
    field :subdivision_1_name, :string
    field :subdivision_2_name, :string
    field :latitude, :float
    field :longitude, :float

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(location, attrs) do
    location
    |> cast(attrs, [
      :geoname_id,
      :country_code,
      :country_name,
      :city_name,
      :subdivision_1_name,
      :subdivision_2_name,
      :latitude,
      :longitude
    ])
    |> validate_required([:geoname_id, :country_code])
  end
end
