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

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

  sig do
    override
      .params(
        value:
          T.nilable(T.any(String, Time, DateTime, ActiveSupport::TimeWithZone)),
      )
      .returns(T.nilable(Time))
  end
  def cast(value)
    return nil if value.nil?
    case value
    when String
      Time.parse(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(String)) }
  def serialize(value)
    return nil if value.nil?
    value.utc.iso8601
  end
end
