import assert from 'node:assert/strict';
import test from 'node:test';
import {
  getCustomerPackageRoleLabel,
  getCustomerPackageUiStatus,
  mapCustomerPackageErrorMessage
} from './customer-package';

test('mapCustomerPackageErrorMessage rewrites not-generated error', () => {
  assert.equal(
    mapCustomerPackageErrorMessage('Assembled quotation PDF has not been generated yet.'),
    'Customer package has not been generated yet. Please generate it first.'
  );
});

test('mapCustomerPackageErrorMessage rewrites approved-pdf prerequisite error', () => {
  assert.equal(
    mapCustomerPackageErrorMessage('Approved PDF has not been generated yet'),
    'Customer package requires an approved PDF first. Complete approval before generating the package.'
  );
});

test('mapCustomerPackageErrorMessage rewrites missing document error', () => {
  assert.equal(
    mapCustomerPackageErrorMessage(
      'Missing SLA document (SLA, brand generic, language th, product type crane).'
    ),
    'Cannot generate package because required SLA document is missing.'
  );
});

test('mapCustomerPackageErrorMessage rewrites forbidden error', () => {
  assert.equal(
    mapCustomerPackageErrorMessage('Forbidden'),
    'You do not have permission to generate or download customer package.'
  );
});

test('getCustomerPackageRoleLabel falls back to humanized labels', () => {
  assert.equal(getCustomerPackageRoleLabel('main'), 'Approved PDF');
  assert.equal(getCustomerPackageRoleLabel('custom_appendix'), 'Custom Appendix');
});

test('getCustomerPackageUiStatus reflects generated, failed, and not-generated states', () => {
  assert.equal(
    getCustomerPackageUiStatus({
      customerPackage: {
        artifactId: 'artifact-1',
        fileName: 'pkg.pdf',
        contentType: 'application/pdf',
        fileSize: 100,
        checksum: 'abc',
        status: 'active',
        pageCount: 2,
        generatedAt: '2026-06-30T00:00:00.000Z',
        generatedBy: 'user-1',
        generatedByName: 'User',
        includedDocuments: [],
        warnings: []
      },
      lastErrorMessage: 'Something failed before'
    }),
    'generated'
  );

  assert.equal(
    getCustomerPackageUiStatus({
      customerPackage: null,
      lastErrorMessage: 'Customer package generation failed.'
    }),
    'failed'
  );

  assert.equal(
    getCustomerPackageUiStatus({
      customerPackage: null,
      lastErrorMessage: null
    }),
    'not_generated'
  );
});
