/**
 * Achievements display rules from the audit (F FE-1, FE-3 … FE-7; B FE-4):
 * Whole hours on the leaderboards, organizations by `total_score`, tied ranks,
 * report learners / hours header / footer notes, profile rank and sponsorship.
 */
import { describe, test } from "node:test";
import assert from "node:assert/strict";

import { leaderboardHours, leaderboardRank, leaderboardValue, wholeAchievementHours } from "@/features/achievements/leaderboardValue";
import { reportEntryLearners, reportFooterNoteKeys, reportHoursColumnKey } from "@/features/achievements/reportEntryPolicy";
import {
  fromOwnTeamProfile,
  fromPublicOrganizationProfile,
  fromPublicProfile,
  rankFrom,
  sponsorshipProfileLabel,
} from "@/features/profile/components/individual/individualProfile";
import type { PublicProfileResponse } from "@/features/profile/components/commonProfileTypes";

describe("leaderboard values", () => {
  test("individual hours omit minutes, preferring the server display", () => {
    assert.equal(leaderboardValue({ total_hours: 12.58 }, "individuals"), "12");
    assert.equal(leaderboardValue({ total_hours: 12.58, total_hours_display: "12:35" }, "individuals"), "12");
    assert.equal(leaderboardHours({ total_hours: undefined }), "0");
  });
  test("achievement totals discard fractions rather than rounding up", () => {
    assert.equal(wholeAchievementHours(2567.0333), "2567");
    assert.equal(wholeAchievementHours("45:16"), "45");
    assert.equal(wholeAchievementHours(45.99), "45");
    assert.equal(wholeAchievementHours(NaN), "0");
  });
  test("companies show total_score, not sponsored_count", () => {
    assert.equal(leaderboardValue({ total_score: 7, sponsored_count: 0, executed_opportunities: 7 }, "companies"), "7");
    // A payload without total_score: completed + sponsorships.
    assert.equal(leaderboardValue({ sponsored_count: 2, executed_opportunities: 3 }, "companies"), "5");
    assert.equal(leaderboardValue({ executed_opportunities: 1200 }, "teams"), "1,200");
  });
  test("ties use the backend rank, else the position", () => {
    assert.equal(leaderboardRank({ rank: 1 }, 2), 1);
    assert.equal(leaderboardRank({}, 4), 4);
    assert.equal(leaderboardRank(null, 3), 3);
  });
});

describe("account report", () => {
  test("sponsorship rows read learners; attendees is the fallback", () => {
    assert.equal(reportEntryLearners({ learners: 4, attendees: null }), 4);
    assert.equal(reportEntryLearners({ attendees: 3 }), 3);
    assert.equal(reportEntryLearners({ learners: null, attendees: null }), null);
  });
  test("hours header and footer notes follow the PDF per kind", () => {
    assert.equal(reportHoursColumnKey("individual"), "VOLUNTEER_HOURS");
    assert.equal(reportHoursColumnKey("team"), "REPORT_COL_TOTAL_HOURS");
    assert.deepEqual(reportFooterNoteKeys("individual", 2.5), ["REPORT_ACTUAL_HOURS_NOTE", "REPORT_OFFICIAL_RECORD_NOTE"]);
    assert.deepEqual(reportFooterNoteKeys("organization", 2.5), ["REPORT_ECONOMIC_RATE_NOTE", "REPORT_ACTUAL_HOURS_NOTE", "REPORT_BENEFICIARIES_NOTE"]);
    assert.deepEqual(reportFooterNoteKeys("team", null), ["REPORT_ACTUAL_HOURS_NOTE", "REPORT_BENEFICIARIES_NOTE"]);
  });
});

describe("profile rank and sponsorship", () => {
  test("current_rank maps on every payload; unranked is null", () => {
    assert.equal(rankFrom({ current_rank: 3 }), 3);
    assert.equal(rankFrom({ current_rank: "٢" }), 2);
    assert.equal(rankFrom({ current_rank: null }), null);
    assert.equal(rankFrom({ current_rank: 0 }), null);
    assert.equal(fromOwnTeamProfile({ current_rank: 5 }, { id: 1 }).rank, 5);
    const response = { id: 9, user_type: "volunteer", profile_data: { id: 9, current_rank: 1 } } as PublicProfileResponse;
    assert.equal(fromPublicProfile(response).rank, 1);
  });
  test("an organization's sponsorship type and level, localized", () => {
    const sponsorship = {
      type: { value_en: "Financial sponsor", value_ar: "راعي مالي" },
      level: { value_en: "Gold", value_ar: "ذهبي" },
    };
    assert.equal(sponsorshipProfileLabel(sponsorship, "ar"), "راعي مالي – ذهبي");
    assert.equal(sponsorshipProfileLabel({ ...sponsorship, level: null }, "en"), "Financial sponsor");
    assert.equal(sponsorshipProfileLabel(null, "en"), "");
    const response = {
      id: 4, user_type: "organization",
      profile_data: { id: 4, sponsorship_profile: sponsorship },
    } as PublicProfileResponse;
    assert.deepEqual(fromPublicOrganizationProfile(response).sponsorshipProfile, sponsorship);
  });
});
