/**
 * Integration Tests for Team Management Auto-Creation
 *
 * Tests the auto-creation of organizations for users with subscriptions
 * when they access team management endpoints without an existing organization.
 *
 * This prevents the regression where users with subscriptions couldn't
 * access team management because they had no organization.
 */

import { jest } from "@jest/globals";
import { NextRequest } from "next/server";

// Mock auth
const mockGetAuthenticatedEmail = jest.fn();
jest.mock("@/lib/utils/auth-helpers.util", () => ({
  getAuthenticatedEmail: mockGetAuthenticatedEmail,
}));

// Mock auth module
jest.mock("@/lib/auth", () => ({
  auth: jest.fn(),
}));

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

jest.mock("@/lib/db");

// eslint-disable-next-line @typescript-eslint/no-require-imports
const { prisma: mockPrisma } = require("@/lib/db");

// Mock organization service
const mockOrganizationService = {
  getUserOrganizationRole: jest.fn(),
  createOrganization: jest.fn(),
  getSeatConfiguration: jest.fn(),
};

jest.mock("@/lib/services/organization.service", () => ({
  organizationService: mockOrganizationService,
}));

// Mock dual-database
jest.mock("@/lib/dual-database", () => ({
  saveToBothDatabases: jest.fn(),
  saveUserToBoth: jest.fn(),
  saveAuditLogToBoth: jest.fn(),
}));

describe("Team Management Tier Seat Limits", () => {
  beforeEach(() => {
    jest.clearAllMocks();
    jest.resetModules();
  });

  it("should correctly calculate tier1 has 0 included seats", () => {
    const { TIER_SEAT_LIMITS } = require("@/lib/types/organization.types");
    expect(TIER_SEAT_LIMITS["tier1"]).toBe(0);
  });

  it("should correctly calculate tier2 has 0 included seats", () => {
    const { TIER_SEAT_LIMITS } = require("@/lib/types/organization.types");
    expect(TIER_SEAT_LIMITS["tier2"]).toBe(0);
  });

  it("should correctly calculate tier3 (Business) has 1 included seat", () => {
    const { TIER_SEAT_LIMITS } = require("@/lib/types/organization.types");
    expect(TIER_SEAT_LIMITS["tier3"]).toBe(1);
  });

  it("should correctly calculate tier4 (Enterprise) has 2 included seats", () => {
    const { TIER_SEAT_LIMITS } = require("@/lib/types/organization.types");
    expect(TIER_SEAT_LIMITS["tier4"]).toBe(2);
  });

  it("should correctly calculate tier5 (Ultimate) has 5 included seats", () => {
    const { TIER_SEAT_LIMITS } = require("@/lib/types/organization.types");
    expect(TIER_SEAT_LIMITS["tier5"]).toBe(5);
  });

  it("should return true for tierSupportsMultiStaff on tier3", () => {
    const {
      tierSupportsMultiStaff,
    } = require("@/lib/types/organization.types");
    expect(tierSupportsMultiStaff("tier3")).toBe(true);
  });

  it("should return true for tierSupportsMultiStaff on tier4", () => {
    const {
      tierSupportsMultiStaff,
    } = require("@/lib/types/organization.types");
    expect(tierSupportsMultiStaff("tier4")).toBe(true);
  });

  it("should return true for tierSupportsMultiStaff on tier5", () => {
    const {
      tierSupportsMultiStaff,
    } = require("@/lib/types/organization.types");
    expect(tierSupportsMultiStaff("tier5")).toBe(true);
  });

  it("should return false for tierSupportsMultiStaff on tier1", () => {
    const {
      tierSupportsMultiStaff,
    } = require("@/lib/types/organization.types");
    expect(tierSupportsMultiStaff("tier1")).toBe(false);
  });

  it("should return false for tierSupportsMultiStaff on tier2", () => {
    const {
      tierSupportsMultiStaff,
    } = require("@/lib/types/organization.types");
    expect(tierSupportsMultiStaff("tier2")).toBe(false);
  });

  it("should calculate correct available seats", () => {
    const {
      calculateAvailableSeats,
    } = require("@/lib/types/organization.types");

    // tier3 with 0 used
    expect(calculateAvailableSeats("tier3", 0, 0)).toBe(1);

    // tier3 fully used
    expect(calculateAvailableSeats("tier3", 0, 1)).toBe(0);

    // tier4 with 1 used
    expect(calculateAvailableSeats("tier4", 0, 1)).toBe(1);

    // tier4 with 1 purchased and 2 used
    expect(calculateAvailableSeats("tier4", 1, 2)).toBe(1);

    // tier5 fully used
    expect(calculateAvailableSeats("tier5", 0, 5)).toBe(0);

    // tier5 with purchased seats
    expect(calculateAvailableSeats("tier5", 3, 5)).toBe(3);
  });
});
