import { describe, it, expect, vi } from 'vitest';
import { createTeamEndpoints, STORE_ROLE_RANK } from '../team';
import type { AlqoveClient } from '../../client';

function fakeClient() {
  const client = {
    get: vi.fn().mockResolvedValue({ data: [] }),
    post: vi.fn().mockResolvedValue({ data: {} }),
    patch: vi.fn().mockResolvedValue({ data: {} }),
    delete: vi.fn().mockResolvedValue(undefined),
  };
  return { client, team: createTeamEndpoints(client as unknown as AlqoveClient) };
}

describe('createTeamEndpoints', () => {
  it('reads my memberships from /v1/me/memberships', async () => {
    const { client, team } = fakeClient();
    await team.myMemberships();
    expect(client.get).toHaveBeenCalledWith('/v1/me/memberships');
  });

  it('scopes roster, pay-rate, position and invitation calls under the store', async () => {
    const { client, team } = fakeClient();
    await team.members.list('s1');
    await team.members.update('s1', 'm1', { role: 'shift_lead' });
    await team.members.terminate('s1', 'm1');
    await team.payRates.list('s1', 'm1');
    await team.payRates.create('s1', 'm1', { hourly_rate_cents: 1850 });
    await team.positions.list('s1');
    await team.positions.create('s1', { name: 'Buyer' });
    await team.positions.update('s1', 'p1', { is_active: false });
    await team.invitations.list('s1');
    await team.invitations.create('s1', { email: 'a@example.com', role: 'sales' });
    await team.invitations.revoke('s1', 'i1');

    expect(client.get).toHaveBeenCalledWith('/v1/stores/s1/members');
    expect(client.patch).toHaveBeenCalledWith('/v1/stores/s1/members/m1', { role: 'shift_lead' });
    expect(client.delete).toHaveBeenCalledWith('/v1/stores/s1/members/m1');
    expect(client.get).toHaveBeenCalledWith('/v1/stores/s1/members/m1/pay-rates');
    expect(client.post).toHaveBeenCalledWith('/v1/stores/s1/members/m1/pay-rates', { hourly_rate_cents: 1850 });
    expect(client.get).toHaveBeenCalledWith('/v1/stores/s1/positions');
    expect(client.post).toHaveBeenCalledWith('/v1/stores/s1/positions', { name: 'Buyer' });
    expect(client.patch).toHaveBeenCalledWith('/v1/stores/s1/positions/p1', { is_active: false });
    expect(client.get).toHaveBeenCalledWith('/v1/stores/s1/invitations');
    expect(client.post).toHaveBeenCalledWith('/v1/stores/s1/invitations', { email: 'a@example.com', role: 'sales' });
    expect(client.delete).toHaveBeenCalledWith('/v1/stores/s1/invitations/i1');
  });

  it('sends the accept token in the body, never the path (Port 00 §4)', async () => {
    const { client, team } = fakeClient();
    await team.invitations.accept('secret-token');
    expect(client.post).toHaveBeenCalledWith('/v1/invitations/accept', { token: 'secret-token' });
    const [url] = client.post.mock.calls[0];
    expect(url).not.toContain('secret-token');
  });

  it('role ranks mirror the API preset ordering', () => {
    expect(STORE_ROLE_RANK.owner).toBeGreaterThan(STORE_ROLE_RANK.manager);
    expect(STORE_ROLE_RANK.manager).toBeGreaterThan(STORE_ROLE_RANK.shift_lead);
    expect(STORE_ROLE_RANK.shift_lead).toBeGreaterThan(STORE_ROLE_RANK.sales);
  });
});
