import assert from "node:assert/strict";
import test from "node:test";
import { buildInsightsFromSnapshots, followerGrowthPct, rollupSnapshotsFromDailyMetrics } from "@/lib/marketing/meta";

test("follower growth percentage handles zero baseline safely", () => {
  assert.equal(followerGrowthPct(0, 100), 0);
  assert.equal(followerGrowthPct(100, 110), 10);
});

test("buildInsightsFromSnapshots includes period-over-period deltas for comparable snapshots", () => {
  const insights = buildInsightsFromSnapshots([
    {
      id: "1",
      channel: "facebook",
      range: "7d",
      followersStart: 1000,
      followersEnd: 1100,
      engagementRate: 4,
      reach: 5000,
      impressions: 7000,
      capturedAt: "2026-03-18T00:00:00.000Z",
    },
    {
      id: "2",
      channel: "facebook",
      range: "7d",
      followersStart: 900,
      followersEnd: 1000,
      engagementRate: 3.5,
      reach: 4200,
      impressions: 6000,
      capturedAt: "2026-03-11T00:00:00.000Z",
    },
  ]);

  assert.equal(insights.length, 1);
  assert.equal(insights[0].followerDelta, 100);
  assert.equal(insights[0].previousFollowerDelta, 100);
  assert.equal(insights[0].followerGrowthPct, 10);
});

test("buildInsightsFromSnapshots ignores stale previous snapshots", () => {
  const insights = buildInsightsFromSnapshots([
    {
      id: "1",
      channel: "facebook",
      range: "7d",
      followersStart: 1000,
      followersEnd: 1100,
      engagementRate: 4,
      reach: 5000,
      impressions: 7000,
      capturedAt: "2026-03-18T00:00:00.000Z",
    },
    {
      id: "2",
      channel: "facebook",
      range: "7d",
      followersStart: 900,
      followersEnd: 1000,
      engagementRate: 3.5,
      reach: 4200,
      impressions: 6000,
      capturedAt: "2026-02-20T00:00:00.000Z",
    },
  ]);

  assert.equal(insights.length, 1);
  assert.equal(insights[0].followerDelta, 100);
  assert.equal(insights[0].previousFollowerDelta, null);
  assert.equal(insights[0].previousReach, null);
  assert.equal(insights[0].previousImpressions, null);
  assert.equal(insights[0].previousEngagementRatePct, null);
});

test("buildInsightsFromSnapshots uses prior period snapshot instead of previous day for 7d", () => {
  const insights = buildInsightsFromSnapshots([
    {
      id: "1",
      channel: "instagram",
      range: "7d",
      followersStart: 1200,
      followersEnd: 1240,
      engagementRate: 4.5,
      reach: 6200,
      impressions: 8300,
      capturedAt: "2026-03-18T00:00:00.000Z",
    },
    {
      id: "2",
      channel: "instagram",
      range: "7d",
      followersStart: 1195,
      followersEnd: 1200,
      engagementRate: 4.1,
      reach: 5000,
      impressions: 7000,
      capturedAt: "2026-03-17T00:00:00.000Z",
    },
    {
      id: "3",
      channel: "instagram",
      range: "7d",
      followersStart: 1140,
      followersEnd: 1195,
      engagementRate: 3.9,
      reach: 4700,
      impressions: 6700,
      capturedAt: "2026-03-11T00:00:00.000Z",
    },
  ]);

  assert.equal(insights.length, 1);
  assert.equal(insights[0].previousReach, 4700);
  assert.equal(insights[0].previousImpressions, 6700);
  assert.equal(insights[0].previousFollowerDelta, 55);
});

test("rollupSnapshotsFromDailyMetrics produces 7d and 30d windows", () => {
  const metrics = Array.from({ length: 30 }).map((_, index) => ({
    channel: "instagram" as const,
    metricDate: `2026-03-${String(index + 1).padStart(2, "0")}`,
    followers: 1000 + index * 2,
    reach: 500 + index * 10,
    impressions: 700 + index * 12,
    engagements: 70 + index,
  }));

  const snapshots = rollupSnapshotsFromDailyMetrics(metrics, new Date("2026-03-30T00:00:00.000Z"));
  assert.equal(snapshots.length, 2);
  assert.equal(
    snapshots.some((snapshot) => snapshot.range === "7d"),
    true
  );
  assert.equal(
    snapshots.some((snapshot) => snapshot.range === "30d"),
    true
  );
});
