# typed: strict
module Tapioca::Compilers
  class HasTimestampsWithDueAtDsl < Tapioca::Dsl::Compiler
    extend T::Sig

    ConstantType =
      type_member { { fixed: T.class_of(::HasTimestampsWithDueAt) } }

    sig { override.returns(T::Enumerable[Module]) }
    def self.gather_constants
      all_classes.select { |c| c < ::HasTimestampsWithDueAt }
    end

    sig { override.void }
    def decorate
      root.create_path(constant) do |klass|
        klass.create_method(
          "due_timestamp",
          parameters: [
            create_param("field", type: "Symbol"),
            create_param("interval", type: "ActiveSupport::Duration"),
          ],
          class_method: true,
          return_type: "void",
        )

        klass.create_method(
          "attr_json_due_timestamp",
          parameters: [
            create_param("field", type: "Symbol"),
            create_param("interval", type: "ActiveSupport::Duration"),
          ],
          class_method: true,
          return_type: "void",
        )

        fields =
          T.cast(
            T.unsafe(constant).due_at_timestamp_fields || {},
            T::Hash[Symbol, ActiveSupport::Duration],
          )

        fields.each do |field, interval|
          klass.create_method(
            "#{field}_scan",
            return_type: "HasTimestampsWithDueAt::TimestampScanInfo",
          )
        end
      end
    end
  end
end
