defmodule SimpleshopTheme.SetupTest do
  use SimpleshopTheme.DataCase, async: false

  alias SimpleshopTheme.{Setup, Settings, Products}

  import SimpleshopTheme.AccountsFixtures

  describe "setup_status/0" do
    test "returns all false on fresh install" do
      status = Setup.setup_status()

      refute status.admin_created
      refute status.printify_connected
      refute status.products_synced
      assert status.product_count == 0
      refute status.stripe_connected
      refute status.site_live
      refute status.can_go_live
    end

    test "detects admin created" do
      user_fixture()
      status = Setup.setup_status()

      assert status.admin_created
    end

    test "detects stripe connected" do
      {:ok, _} = Settings.put_secret("stripe_api_key", "sk_test_abc123")
      status = Setup.setup_status()

      assert status.stripe_connected
    end

    test "detects site live" do
      {:ok, _} = Settings.set_site_live(true)
      status = Setup.setup_status()

      assert status.site_live
    end

    test "detects printify connected with products" do
      {:ok, conn} =
        Products.create_provider_connection(%{
          name: "Test",
          provider_type: "printify",
          api_key: "test_api_key"
        })

      status = Setup.setup_status()
      assert status.printify_connected
      refute status.products_synced
      assert status.product_count == 0

      # Add a product
      {:ok, _product} =
        Products.create_product(%{
          title: "Test product",
          provider_product_id: "ext-1",
          provider_connection_id: conn.id,
          status: "active"
        })

      status = Setup.setup_status()
      assert status.products_synced
      assert status.product_count == 1
    end

    test "can_go_live requires printify, products, and stripe" do
      {:ok, conn} =
        Products.create_provider_connection(%{
          name: "Test",
          provider_type: "printify",
          api_key: "test_api_key"
        })

      {:ok, _product} =
        Products.create_product(%{
          title: "Test product",
          provider_product_id: "ext-1",
          provider_connection_id: conn.id,
          status: "active"
        })

      # Still missing stripe
      refute Setup.setup_status().can_go_live

      # Add stripe
      {:ok, _} = Settings.put_secret("stripe_api_key", "sk_test_abc123")

      assert Setup.setup_status().can_go_live
    end
  end
end
