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

  alias Berrypod.{Setup, Settings, Products}

  import Berrypod.AccountsFixtures

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

      refute status.admin_created
      refute status.provider_connected
      assert is_nil(status.provider_type)
      refute status.products_synced
      assert status.product_count == 0
      refute status.stripe_connected
      refute status.setup_complete
      refute status.site_live
      refute status.can_go_live
      refute status.theme_customised
      refute status.has_orders
      refute status.has_shipping
      refute status.checklist_dismissed
    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 provider 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.provider_connected
      assert status.provider_type == "printify"
      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 "setup_complete requires admin, provider, and stripe" do
      user_fixture()

      {:ok, _conn} =
        Products.create_provider_connection(%{
          name: "Test",
          provider_type: "printful",
          api_key: "test_api_key"
        })

      refute Setup.setup_status().setup_complete

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

      assert Setup.setup_status().setup_complete
    end

    test "can_go_live requires provider, products, stripe, and shipping" 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 and shipping
      refute Setup.setup_status().can_go_live

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

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

      Berrypod.Shipping.upsert_rates(conn.id, [
        %{
          blueprint_id: 1,
          print_provider_id: 1,
          country_code: "GB",
          first_item_cost: 350,
          additional_item_cost: 200,
          currency: "GBP"
        }
      ])

      assert Setup.setup_status().can_go_live
    end

    test "detects theme customised" do
      refute Setup.setup_status().theme_customised

      {:ok, _} = Settings.update_theme_settings(%{mood: "warm"})

      assert Setup.setup_status().theme_customised
    end

    test "detects shipping rates" do
      refute Setup.setup_status().has_shipping

      {:ok, conn} =
        Products.create_provider_connection(%{
          name: "Test",
          provider_type: "printify",
          api_key: "test_api_key"
        })

      Berrypod.Shipping.upsert_rates(conn.id, [
        %{
          blueprint_id: 1,
          print_provider_id: 1,
          country_code: "GB",
          first_item_cost: 350,
          additional_item_cost: 200,
          currency: "GBP"
        }
      ])

      assert Setup.setup_status().has_shipping
    end

    test "can_go_live requires shipping rates" 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"
        })

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

      # No shipping rates yet
      refute Setup.setup_status().can_go_live

      Berrypod.Shipping.upsert_rates(conn.id, [
        %{
          blueprint_id: 1,
          print_provider_id: 1,
          country_code: "GB",
          first_item_cost: 350,
          additional_item_cost: 200,
          currency: "GBP"
        }
      ])

      assert Setup.setup_status().can_go_live
    end
  end
end
