import type { Request, Response } from 'express';
import { prismaMock } from '../../setup';
import { AdminRole } from '../../../types';
import { validateMockIssuance } from '../../../routes/admin/licenses';

function responseMock(): Response {
  const response = {
    status: jest.fn(),
    json: jest.fn(),
  } as unknown as Response;
  (response.status as jest.Mock).mockReturnValue(response);
  return response;
}

function request(body: Record<string, unknown>, role: AdminRole = AdminRole.ADMIN): Request {
  return {
    body,
    admin: { id: 'admin-id', email: 'admin@example.com', role },
  } as unknown as Request;
}

describe('mock license issuance policy', () => {
  it('allows live issuance without mock-only requirements', async () => {
    const req = request({ execution_mode: 'live' });
    const res = responseMock();

    await expect(validateMockIssuance(req, res)).resolves.toBe(true);
    expect(prismaMock.user.findUnique).not.toHaveBeenCalled();
  });

  it('requires an admin role', async () => {
    const req = request({ execution_mode: 'mock', user_id: 'owner-id', plugin: 'international' }, AdminRole.SUPPORT);
    const res = responseMock();

    await expect(validateMockIssuance(req, res)).resolves.toBe(false);
    expect(res.status).toHaveBeenCalledWith(403);
    expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_ISSUANCE_FORBIDDEN' }),
    }));
  });

  it('requires an explicit international owner', async () => {
    const missingOwner = request({ execution_mode: 'mock', plugin: 'international' });
    const missingOwnerResponse = responseMock();
    const missingPlugin = request({ execution_mode: 'mock', user_id: 'owner-id', plugin: 'translate' });
    const missingPluginResponse = responseMock();

    await expect(validateMockIssuance(missingOwner, missingOwnerResponse)).resolves.toBe(false);
    await expect(validateMockIssuance(missingPlugin, missingPluginResponse)).resolves.toBe(false);

    expect(missingOwnerResponse.status).toHaveBeenCalledWith(422);
    expect(missingOwnerResponse.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_OWNER_REQUIRED' }),
    }));
    expect(missingPluginResponse.status).toHaveBeenCalledWith(403);
    expect(missingPluginResponse.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_ISSUANCE_FORBIDDEN' }),
    }));
  });

  it('requires an existing owner record', async () => {
    prismaMock.user.findUnique.mockResolvedValue(null);
    const res = responseMock();

    await expect(validateMockIssuance(
      request({ execution_mode: 'mock', user_id: 'missing-owner', plugin: 'international' }),
      res,
    )).resolves.toBe(false);

    expect(res.status).toHaveBeenCalledWith(422);
    expect(res.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_OWNER_REQUIRED' }),
    }));
  });

  it('requires an active owner and international subscription', async () => {
    prismaMock.user.findUnique.mockResolvedValueOnce({
      status: 'suspended',
      subscriptions: [{ id: 'subscription-id' }],
    } as any);
    const inactiveResponse = responseMock();
    await expect(validateMockIssuance(
      request({ execution_mode: 'mock', user_id: 'owner-id', plugin: 'international' }),
      inactiveResponse,
    )).resolves.toBe(false);
    expect(inactiveResponse.status).toHaveBeenCalledWith(403);
    expect(inactiveResponse.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_ISSUANCE_FORBIDDEN' }),
    }));

    prismaMock.user.findUnique.mockResolvedValueOnce({
      status: 'active',
      subscriptions: [],
    } as any);
    const subscriptionResponse = responseMock();
    await expect(validateMockIssuance(
      request({ execution_mode: 'mock', user_id: 'owner-id', plugin: 'international' }),
      subscriptionResponse,
    )).resolves.toBe(false);
    expect(subscriptionResponse.status).toHaveBeenCalledWith(403);
    expect(subscriptionResponse.json).toHaveBeenCalledWith(expect.objectContaining({
      error: expect.objectContaining({ code: 'MOCK_LICENSE_ISSUANCE_FORBIDDEN' }),
    }));
  });

  it('allows an active international subscription owner', async () => {
    prismaMock.user.findUnique.mockResolvedValue({
      status: 'active',
      subscriptions: [{ id: 'subscription-id' }],
    } as any);

    await expect(validateMockIssuance(
      request({ execution_mode: 'mock', user_id: 'owner-id', plugin: 'international' }),
      responseMock(),
    )).resolves.toBe(true);
  });
});
