defmodule Towerops.Workers.FirmwareVersionFetcherWorkerTest do
  use Towerops.DataCase, async: false

  alias Towerops.Devices.Firmware
  alias Towerops.Workers.FirmwareVersionFetcherWorker

  describe "perform/1" do
    @tag :skip
    test "successfully fetches and parses valid RSS feed" do
      # This test requires actual HTTP call to MikroTik RSS feed
      # Skip for now - integration test would be better
      assert :ok = FirmwareVersionFetcherWorker.perform(%Oban.Job{})

      # Verify record was created
      release = Firmware.get_latest_firmware_release("mikrotik", "routeros")
      assert release.vendor == "mikrotik"
      assert release.product_line == "routeros"
      assert release.version
      assert release.fetched_at
    end
  end

  describe "parse_rss_feed/1" do
    test "parses valid RSS feed XML" do
      valid_rss = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>7.14.1</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Wed, 15 Jan 2025 12:00:00 +0000</pubDate>
            <description>RouterOS 7.14.1 stable release</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(valid_rss)
      assert data.version == "7.14.1"
      assert data.download_url == "https://mikrotik.com/download"
      assert data.release_date == ~D[2025-01-15]
      assert data.metadata["rss_title"] == "RouterOS latest stable version"
      assert data.metadata["rss_description"] == "RouterOS 7.14.1 stable release"
    end

    test "handles malformed XML" do
      malformed_xml = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version
          <!-- Missing closing tags -->
      """

      assert {:error, :parse_error} = FirmwareVersionFetcherWorker.parse_rss_feed(malformed_xml)
    end

    test "handles missing version in RSS feed" do
      missing_version = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <link>https://mikrotik.com/download</link>
            <pubDate>Wed, 15 Jan 2025 12:00:00 +0000</pubDate>
          </item>
        </channel>
      </rss>
      """

      assert {:error, :invalid_data} = FirmwareVersionFetcherWorker.parse_rss_feed(missing_version)
    end

    test "handles empty RSS feed" do
      empty_feed = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
        </channel>
      </rss>
      """

      assert {:error, :no_items} = FirmwareVersionFetcherWorker.parse_rss_feed(empty_feed)
    end

    test "parses RFC822 date correctly" do
      rss_with_date = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>7.14.1</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Mon, 01 Feb 2025 14:30:45 +0000</pubDate>
            <description>Test</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(rss_with_date)
      assert data.release_date == ~D[2025-02-01]
    end

    test "handles invalid RFC822 date format gracefully" do
      invalid_date = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>7.14.1</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Invalid Date Format</pubDate>
            <description>RouterOS 7.14.1 stable release</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(invalid_date)
      assert data.version == "7.14.1"
      assert is_nil(data.release_date)
    end

    test "extracts version number from 'RouterOS X.Y.Z [stable]' format" do
      rss_with_full_version = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>RouterOS 7.21.2 [stable]</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Wed, 15 Jan 2025 12:00:00 +0000</pubDate>
            <description>RouterOS 7.21.2 stable release</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(rss_with_full_version)
      assert data.version == "7.21.2"
      assert data.metadata["raw_version"] == "RouterOS 7.21.2 [stable]"
    end

    test "extracts two-part version number" do
      rss_with_two_part = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>RouterOS 7.21 [stable]</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Wed, 15 Jan 2025 12:00:00 +0000</pubDate>
            <description>Test</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(rss_with_two_part)
      assert data.version == "7.21"
    end

    test "handles missing pubDate" do
      rss_no_date = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>RouterOS latest stable version</title>
          <item>
            <title>7.14.1</title>
            <link>https://mikrotik.com/download</link>
            <description>Test</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(rss_no_date)
      assert data.version == "7.14.1"
      assert is_nil(data.release_date)
    end

    test "stores metadata correctly" do
      rss_with_metadata = """
      <?xml version="1.0" encoding="UTF-8"?>
      <rss version="2.0">
        <channel>
          <title>Custom Channel Title</title>
          <item>
            <title>7.14.1</title>
            <link>https://mikrotik.com/download</link>
            <pubDate>Wed, 15 Jan 2025 12:00:00 +0000</pubDate>
            <description>Custom description with details</description>
          </item>
        </channel>
      </rss>
      """

      assert {:ok, data} = FirmwareVersionFetcherWorker.parse_rss_feed(rss_with_metadata)
      assert data.metadata["rss_title"] == "Custom Channel Title"
      assert data.metadata["rss_description"] == "Custom description with details"
    end
  end
end
