defmodule Mix.Tasks.RegisterWebhooks do
  @moduledoc """
  Registers Printify webhooks for a shop.

  Usage:
      mix register_webhooks https://yourshop.com/webhooks/printify

  Prerequisites:
  - Printify provider connection must exist with shop_id configured
  - webhook_secret must be set in the connection config

  Generate a webhook secret with:
      openssl rand -hex 20
  """

  use Mix.Task

  @shortdoc "Register Printify webhooks for product updates"

  @impl Mix.Task
  def run([url]) do
    Mix.Task.run("app.start")

    alias SimpleshopTheme.Products
    alias SimpleshopTheme.Providers.Printify

    case Products.get_provider_connection_by_type("printify") do
      nil ->
        Mix.shell().error("No Printify connection found. Create one at /admin/providers first.")

      conn ->
        Mix.shell().info("Registering webhooks for URL: #{url}")

        case Printify.register_webhooks(conn, url) do
          {:ok, results} ->
            Enum.each(results, fn
              {:ok, event, response} ->
                Mix.shell().info("✓ Registered: #{event} (ID: #{response["id"]})")

              {:error, event, reason} ->
                Mix.shell().error("✗ Failed: #{event} - #{inspect(reason)}")
            end)

            Mix.shell().info("\nDone!")

          {:error, :no_webhook_secret} ->
            Mix.shell().error("""
            No webhook secret configured.

            Generate one with: openssl rand -hex 20

            Then add it to your provider connection config as "webhook_secret".
            """)

          {:error, reason} ->
            Mix.shell().error("Failed: #{inspect(reason)}")
        end
    end
  end

  def run(_) do
    Mix.shell().error("""
    Usage: mix register_webhooks <url>

    Example:
        mix register_webhooks https://yourshop.com/webhooks/printify
    """)
  end
end
