defmodule Berrypod.ActivityLog.PruneWorker do
  @moduledoc """
  Nightly Oban cron job that prunes activity log entries older than 90 days.
  """

  use Oban.Worker, queue: :default, max_attempts: 1

  import Ecto.Query

  alias Berrypod.Repo
  alias Berrypod.ActivityLog.Entry

  @impl Oban.Worker
  def perform(_job) do
    cutoff = DateTime.utc_now() |> DateTime.add(-90, :day)

    {count, _} = Repo.delete_all(from(a in Entry, where: a.inserted_at < ^cutoff))

    if count > 0 do
      require Logger
      Logger.info("Pruned #{count} activity log entries older than 90 days")
    end

    :ok
  end
end
