import { MembershipController } from './membership.controller';

describe('MembershipController', () => {
  let ctrl: MembershipController;
  let svc: any; let prisma: any;

  beforeEach(() => {
    svc = { compute: jest.fn(), getEntitlements: jest.fn() };
    prisma = { membershipPlan: { findMany: jest.fn() } };
    ctrl = new MembershipController(svc, prisma);
  });

  it('GET /membership/me returns effectivePlan + grants + token', async () => {
    svc.compute.mockResolvedValue({ effectivePlan:'premium', grants:[{ source:'apple',status:'active',planCode:'premium',planName:'Premium',startedAt:'2026-01-01',expiresAt:null,autoRenewStatus:'on',environment:'Production' }], appAccountToken:'uuid-1' });
    svc.getEntitlements.mockResolvedValue({ planCode:'premium', entitlements:{ maxKnowledgeBases:10, maxStorageBytes:10737418240, maxFileSizeBytes:52428800, monthlyOcrPages:50, monthlyVisionPages:30, monthlyChatCount:300, monthlyAiAnalysisCount:0, monthlyRecallCount:0, monthlyCardGenCount:0, monthlyQuizCount:0 } });
    const r = await ctrl.getMyMembership({ user:{ id:'u1' } });
    expect(r.effectivePlan).toBe('premium');
    expect(r.grants).toHaveLength(1);
    expect(r.appAccountToken).toBe('uuid-1');
    expect(r.entitlements).toBeDefined();
  });

  it('GET /membership/me does not expose transactionId', async () => {
    svc.compute.mockResolvedValue({ effectivePlan:'free', grants:[], appAccountToken:null });
    svc.getEntitlements.mockResolvedValue({ planCode:'free', entitlements:{ maxKnowledgeBases:1, maxStorageBytes:1073741824, maxFileSizeBytes:10485760, monthlyOcrPages:0, monthlyVisionPages:0, monthlyChatCount:30, monthlyAiAnalysisCount:0, monthlyRecallCount:0, monthlyCardGenCount:0, monthlyQuizCount:0 } });
    const r = await ctrl.getMyMembership({ user:{ id:'u1' } });
    expect(r).not.toHaveProperty('transactionId');
    expect(r).not.toHaveProperty('originalTransactionId');
  });

  it('GET /membership/plans returns active plans', async () => {
    prisma.membershipPlan.findMany.mockResolvedValue([{ code:'free', name:'Free', maxKnowledgeBases:1, maxStorageBytes:BigInt(1073741824), maxFileSizeBytes:BigInt(10485760), monthlyOcrPages:0, monthlyVisionPages:0, monthlyChatCount:30, monthlyAiAnalysisCount:0, monthlyRecallCount:0, monthlyCardGenCount:0, monthlyQuizCount:0, priceMonthly:0, priceYearly:0 }]);
    const r = await ctrl.getPlans();
    expect(r.plans).toHaveLength(1);
    expect(r.plans[0].planCode).toBe('free');
  });
});
