// A1 Session 3 — internal partner attribution report.
// Pure report math + route behavior with an injected Supabase client.

const express = require('express');
const request = require('supertest');

const partnersRouter = require('../../src/routes/partners');
const { sanitizePartnerCode, buildPartnerReport, _setClientForTests } = partnersRouter;

const KEY = 'test-internal-key';

function makeApp() {
  const app = express();
  app.use('/api/partners', partnersRouter);
  return app;
}

describe('sanitizePartnerCode', () => {
  it('uppercases and accepts the cookie convention (A-Z 0-9 - _, ≤32)', () => {
    expect(sanitizePartnerCode('hoopspod')).toBe('HOOPSPOD');
    expect(sanitizePartnerCode('THE-WIRE_22')).toBe('THE-WIRE_22');
  });
  it('rejects unsafe input', () => {
    expect(sanitizePartnerCode('a b')).toBeNull();
    expect(sanitizePartnerCode('a'.repeat(33))).toBeNull();
    expect(sanitizePartnerCode('')).toBeNull();
    expect(sanitizePartnerCode("x';drop")).toBeNull();
  });
});

describe('buildPartnerReport', () => {
  it('counts signups, active paid conversions, and founder-aware MRR', () => {
    const rows = [
      { tier: 'free', subscription_status: 'none', founder_pricing: false },
      { tier: 'analyst', subscription_status: 'active', founder_pricing: false }, // 19.99
      { tier: 'analyst', subscription_status: 'active', founder_pricing: true }, // 14.99
      { tier: 'desk', subscription_status: 'active', founder_pricing: false }, // 49.99
      { tier: 'desk', subscription_status: 'canceled', founder_pricing: false }, // not active
    ];
    expect(buildPartnerReport(rows)).toEqual({ signups: 5, conversions: 3, mrr_attributed: 84.97 });
  });
  it('empty / bad input → zeros', () => {
    expect(buildPartnerReport([])).toEqual({ signups: 0, conversions: 0, mrr_attributed: 0 });
    expect(buildPartnerReport(null)).toEqual({ signups: 0, conversions: 0, mrr_attributed: 0 });
  });
});

describe('GET /api/partners/report/:code', () => {
  const OLD_KEY = process.env.VYNDR_INTERNAL_KEY;
  beforeAll(() => { process.env.VYNDR_INTERNAL_KEY = KEY; });
  afterAll(() => {
    if (OLD_KEY === undefined) delete process.env.VYNDR_INTERNAL_KEY;
    else process.env.VYNDR_INTERNAL_KEY = OLD_KEY;
    _setClientForTests(null);
  });

  const fakeClient = (result) => () => ({
    from: () => ({ select: () => ({ eq: () => Promise.resolve(result) }) }),
  });

  it('401s without the internal key', async () => {
    const res = await request(makeApp()).get('/api/partners/report/HOOPSPOD');
    expect(res.status).toBe(401);
  });

  it('400s an invalid code', async () => {
    const res = await request(makeApp())
      .get('/api/partners/report/bad%20code')
      .set('x-internal-key', KEY);
    expect(res.status).toBe(400);
  });

  it('returns real attribution when the partner_ref column exists', async () => {
    _setClientForTests(fakeClient({
      data: [
        { tier: 'analyst', subscription_status: 'active', founder_pricing: true },
        { tier: 'free', subscription_status: 'none', founder_pricing: false },
      ],
      error: null,
    }));
    const res = await request(makeApp())
      .get('/api/partners/report/hoopspod')
      .set('x-internal-key', KEY);
    expect(res.status).toBe(200);
    expect(res.body).toEqual({ ok: true, code: 'HOOPSPOD', signups: 2, conversions: 1, mrr_attributed: 14.99 });
  });

  it('degrades to zeros + note when the column is not migrated yet', async () => {
    _setClientForTests(fakeClient({ data: null, error: { message: 'column user_profiles.partner_ref does not exist' } }));
    const res = await request(makeApp())
      .get('/api/partners/report/HOOPSPOD')
      .set('x-internal-key', KEY);
    expect(res.status).toBe(200);
    expect(res.body.signups).toBe(0);
    expect(res.body.conversions).toBe(0);
    expect(res.body.mrr_attributed).toBe(0);
    expect(res.body.note).toContain('docs/PARTNERS.md');
  });
});
