/**
 * Unit Tests for Team Invitation Emails Service
 *
 * Tests the email template generation and sending functionality.
 */

import { jest } from "@jest/globals";

// Mock email sender
const mockSendEmail = jest.fn();
jest.mock("@/lib/email", () => ({
  sendEmail: mockSendEmail,
}));

// Mock logger
jest.mock("@/lib/logger", () => ({
  logger: {
    info: jest.fn(),
    error: jest.fn(),
    warn: jest.fn(),
  },
}));

// Import after mocks
import { sendTeamInvitation } from "@/lib/services/team-invitation-emails.service";

// Email service API changed - mock setup needs rewrite
describe.skip("Team Invitation Emails Service", () => {
  beforeEach(() => {
    jest.clearAllMocks();
    mockSendEmail.mockResolvedValue(true);
  });

  describe("sendTeamInvitation", () => {
    const defaultParams = {
      email: "newstaff@example.com",
      inviteeName: "Jane Smith",
      orgName: "Test Clinic",
      inviterName: "Dr. John Doe",
      role: "STAFF" as const,
      token: "secure_token_123",
      expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000),
      lang: "en" as const,
    };

    it("should send email successfully", async () => {
      const result = await sendTeamInvitation(defaultParams);

      expect(result.success).toBe(true);
      expect(mockSendEmail).toHaveBeenCalledTimes(1);
    });

    it("should generate correct invite link", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("/en/invite/secure_token_123");
      expect(template.text).toContain("/en/invite/secure_token_123");
    });

    it("should include organization name in email", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Test Clinic");
      expect(template.text).toContain("Test Clinic");
      expect(template.subject).toContain("Test Clinic");
    });

    it("should include invitee name in greeting", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Hello Jane Smith,");
    });

    it("should include inviter name in email body", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Dr. John Doe");
    });

    it("should include role in email", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Staff Member");
    });

    it("should NOT include WhatsApp text after update", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      // Should NOT contain WhatsApp appointment management text
      expect(template.html).not.toContain("WhatsApp appointment management");
      expect(template.text).not.toContain("WhatsApp Appointment Management");

      // Should just say "on Mawidi"
      expect(template.html).toContain("on Mawidi.");
    });

    it("should handle ADMIN role", async () => {
      await sendTeamInvitation({
        ...defaultParams,
        role: "ADMIN",
      });

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Admin");
    });

    it("should generate Arabic email for ar language", async () => {
      await sendTeamInvitation({
        ...defaultParams,
        lang: "ar",
      });

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain('dir="rtl"');
      expect(template.html).toContain("مرحباً");
      expect(template.subject).toContain("دعوة للانضمام");
    });

    it("should format expiry date in Arabic for ar language", async () => {
      await sendTeamInvitation({
        ...defaultParams,
        lang: "ar",
      });

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      // Arabic date format should be used
      expect(template.html).toContain("تنتهي");
    });

    it("should use generic greeting when name not provided", async () => {
      await sendTeamInvitation({
        ...defaultParams,
        inviteeName: undefined,
      });

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("Hello,");
      expect(template.html).not.toContain("Hello Jane");
    });

    it("should return error when email sending fails", async () => {
      mockSendEmail.mockResolvedValue(false);

      const result = await sendTeamInvitation(defaultParams);

      expect(result.success).toBe(false);
      expect(result.error).toBe("Failed to send email");
    });

    it("should handle exceptions gracefully", async () => {
      mockSendEmail.mockRejectedValue(new Error("SMTP error"));

      const result = await sendTeamInvitation(defaultParams);

      expect(result.success).toBe(false);
      expect(result.error).toBe("Internal error sending email");
    });

    it("should include expiry date in email", async () => {
      const expiresAt = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000);
      await sendTeamInvitation({
        ...defaultParams,
        expiresAt,
      });

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      // Should contain formatted date
      const expectedMonth = expiresAt.toLocaleDateString("en-US", {
        month: "long",
      });
      expect(template.html).toContain(expectedMonth);
    });

    it("should include CTA button in HTML email", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("cta-button");
      expect(template.html).toContain("Accept Invitation");
    });

    it("should include fallback link for copy-paste", async () => {
      await sendTeamInvitation(defaultParams);

      const emailCall = mockSendEmail.mock.calls[0];
      const template = emailCall[1];

      expect(template.html).toContain("link-fallback");
      expect(template.html).toContain("Copy and paste this URL");
    });
  });
});
