defmodule ToweropsWeb.TimeHelpersTest do
  use ExUnit.Case, async: true

  alias ToweropsWeb.TimeHelpers

  describe "format_datetime/2" do
    test "formats datetime in UTC when timezone is nil" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_datetime(datetime, nil) == "Jan 15, 2026 at 02:34 PM UTC"
    end

    test "formats datetime in UTC when timezone is explicitly UTC" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_datetime(datetime, "UTC") == "Jan 15, 2026 at 02:34 PM UTC"
    end

    test "converts datetime to America/New_York timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      # 14:34 UTC = 09:34 EST (UTC-5)
      assert TimeHelpers.format_datetime(datetime, "America/New_York") =~ "Jan 15, 2026 at 09:34 AM"
      assert TimeHelpers.format_datetime(datetime, "America/New_York") =~ "EST"
    end

    test "converts datetime to America/Los_Angeles timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      # 14:34 UTC = 06:34 PST (UTC-8)
      assert TimeHelpers.format_datetime(datetime, "America/Los_Angeles") =~ "Jan 15, 2026 at 06:34 AM"
      assert TimeHelpers.format_datetime(datetime, "America/Los_Angeles") =~ "PST"
    end

    test "falls back to UTC for invalid timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_datetime(datetime, "Invalid/Timezone") == "Jan 15, 2026 at 02:34 PM UTC"
    end

    test "returns 'Never' for nil datetime" do
      assert TimeHelpers.format_datetime(nil, "America/New_York") == "Never"
      assert TimeHelpers.format_datetime(nil, nil) == "Never"
    end
  end

  describe "format_date/2" do
    test "formats date in UTC when timezone is nil" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_date(datetime, nil) == "Jan 15, 2026"
    end

    test "formats date in UTC when timezone is explicitly UTC" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_date(datetime, "UTC") == "Jan 15, 2026"
    end

    test "converts to different date in America/Los_Angeles for early UTC times" do
      # 02:00 UTC on Jan 15 = 18:00 PST on Jan 14 (UTC-8)
      datetime = ~U[2026-01-15 02:00:00Z]
      assert TimeHelpers.format_date(datetime, "America/Los_Angeles") == "Jan 14, 2026"
    end

    test "keeps same date for late UTC times in America/Los_Angeles" do
      # 20:00 UTC on Jan 15 = 12:00 PST on Jan 15 (UTC-8)
      datetime = ~U[2026-01-15 20:00:00Z]
      assert TimeHelpers.format_date(datetime, "America/Los_Angeles") == "Jan 15, 2026"
    end

    test "falls back to UTC for invalid timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_date(datetime, "Invalid/Timezone") == "Jan 15, 2026"
    end

    test "returns 'Never' for nil datetime" do
      assert TimeHelpers.format_date(nil, "America/New_York") == "Never"
      assert TimeHelpers.format_date(nil, nil) == "Never"
    end
  end

  describe "format_iso8601/2" do
    test "formats datetime in UTC when timezone is nil" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_iso8601(datetime, nil) == "2026-01-15 14:34:00 UTC"
    end

    test "formats datetime in UTC when timezone is explicitly UTC" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_iso8601(datetime, "UTC") == "2026-01-15 14:34:00 UTC"
    end

    test "converts datetime to America/New_York timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      # 14:34 UTC = 09:34 EST (UTC-5)
      result = TimeHelpers.format_iso8601(datetime, "America/New_York")
      assert result =~ "2026-01-15 09:34:00"
      assert result =~ "EST"
    end

    test "converts datetime to America/Los_Angeles timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      # 14:34 UTC = 06:34 PST (UTC-8)
      result = TimeHelpers.format_iso8601(datetime, "America/Los_Angeles")
      assert result =~ "2026-01-15 06:34:00"
      assert result =~ "PST"
    end

    test "falls back to UTC for invalid timezone" do
      datetime = ~U[2026-01-15 14:34:00Z]
      assert TimeHelpers.format_iso8601(datetime, "Invalid/Timezone") == "2026-01-15 14:34:00 UTC"
    end

    test "returns 'Never' for nil datetime" do
      assert TimeHelpers.format_iso8601(nil, "America/New_York") == "Never"
      assert TimeHelpers.format_iso8601(nil, nil) == "Never"
    end
  end

  describe "format_time_ago/1" do
    test "formats seconds ago" do
      datetime = DateTime.add(DateTime.utc_now(), -30, :second)
      assert TimeHelpers.format_time_ago(datetime) =~ "s ago"
    end

    test "formats minutes ago" do
      datetime = DateTime.add(DateTime.utc_now(), -120, :second)
      assert TimeHelpers.format_time_ago(datetime) =~ "m ago"
    end

    test "formats hours ago" do
      datetime = DateTime.add(DateTime.utc_now(), -7200, :second)
      assert TimeHelpers.format_time_ago(datetime) =~ "h ago"
    end

    test "formats days ago" do
      datetime = DateTime.add(DateTime.utc_now(), -172_800, :second)
      assert TimeHelpers.format_time_ago(datetime) =~ "d ago"
    end

    test "returns 'Never' for nil datetime" do
      assert TimeHelpers.format_time_ago(nil) == "Never"
    end
  end
end
