# typed: strict
class ActiveModelUtcTimeIntValue < ActiveModel::Type::Value
  extend T::Sig

  sig { override.returns(Symbol) }
  def type
    :integer
  end

  sig do
    override
      .params(
        value:
          T.nilable(
            T.any(Integer, Time, DateTime, ActiveSupport::TimeWithZone),
          ),
      )
      .returns(T.nilable(Time))
  end
  def cast(value)
    return nil if value.nil?
    case value
    when Integer
      Time.at(value).utc
    when Time
      value
    when DateTime
      value.to_time.utc
    when ActiveSupport::TimeWithZone
      value.utc
    end
  end

  sig { override.params(value: T.nilable(Time)).returns(T.nilable(Integer)) }
  def serialize(value)
    return nil if value.nil?
    value.to_i
  end
end
