import type { SessionSummary } from './cashline-api.service';
import { JourneyId } from '../journeys/journey.models';

export type FrontendPackageId = 'internal' | 'supplier' | 'customer';

function hasRolePrefix(session: SessionSummary, prefix: string): boolean {
  return session.roleCodes.some((roleCode) => roleCode.startsWith(prefix));
}

export function isSupplierSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('EXTERNAL_SUPPLIER_USER')
    || session.roleCodes.includes('SUPPLIER_ADMIN')
    || session.workspace === 'supplier';
}

export function isCustomerSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('EXTERNAL_CUSTOMER_USER')
    || session.roleCodes.includes('CUSTOMER_ADMIN')
    || session.workspace === 'customer';
}

export function isIslamicFinanceSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('IF_OPERATIONS_MAKER') || session.workspace === 'islamic';
}

export function isAccountingOfficeSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('ACCOUNTING_OFFICE_USER')
    || session.roleCodes.includes('ACCOUNTING_OFFICE_SUPERVISOR')
    || session.workspace === 'accounting-office';
}

export function isFinanceSession(session: SessionSummary): boolean {
  return hasRolePrefix(session, 'FINANCE_');
}

export function isOperationsSession(session: SessionSummary): boolean {
  return ['operations', 'procurement', 'inventory', 'sales'].includes(session.workspace);
}

export function isAdministratorSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('SUPER_ADMIN') || session.workspace === 'administrator';
}

export function isSystemOwnerSession(session: SessionSummary): boolean {
  return session.roleCodes.includes('SYSTEM_OWNER') || session.workspace === 'system-owner';
}

export function deriveWorkspace(roleCodes: readonly string[]): string {
  if (roleCodes.includes('SUPER_ADMIN')) {
    return 'administrator';
  }

  if (roleCodes.includes('SYSTEM_OWNER')) {
    return 'system-owner';
  }

  if (roleCodes.includes('ACCOUNTING_OFFICE_USER') || roleCodes.includes('ACCOUNTING_OFFICE_SUPERVISOR')) {
    return 'accounting-office';
  }

  if (roleCodes.includes('EXTERNAL_SUPPLIER_USER') || roleCodes.includes('SUPPLIER_ADMIN')) {
    return 'supplier';
  }

  if (roleCodes.includes('EXTERNAL_CUSTOMER_USER') || roleCodes.includes('CUSTOMER_ADMIN')) {
    return 'customer';
  }

  if (roleCodes.includes('IF_OPERATIONS_MAKER')) {
    return 'islamic';
  }

  if (roleCodes.includes('FINANCE_CHECKER')) {
    return 'finance-checker';
  }

  if (roleCodes.some((roleCode) => roleCode.startsWith('FINANCE_'))) {
    return 'finance';
  }

  if (roleCodes.includes('ERP_PROCUREMENT_MAKER')) {
    return 'procurement';
  }

  if (roleCodes.includes('ERP_INVENTORY_CONTROLLER')) {
    return 'inventory';
  }

  if (roleCodes.includes('ERP_SALES_OPERATOR')) {
    return 'sales';
  }

  if (roleCodes.includes('ERP_OPS_MAKER')) {
    return 'operations';
  }

  return 'general';
}

export function allowedJourneyIds(session: SessionSummary | null): JourneyId[] {
  if (!session) {
    return [];
  }

  if (isSupplierSession(session)) {
    return ['supplier'];
  }

  if (isCustomerSession(session)) {
    return ['customer'];
  }

  if (isAdministratorSession(session) || isSystemOwnerSession(session)) {
    return [];
  }

  if (isAccountingOfficeSession(session)) {
    return ['accounting-office'];
  }

  if (isIslamicFinanceSession(session)) {
    return ['islamic-finance', 'reporting-center'];
  }

  if (isFinanceSession(session)) {
    return ['finance-approvals', 'reporting-center'];
  }

  if (isOperationsSession(session)) {
    return ['operations-control', 'reporting-center', 'workflow-monitoring'];
  }

  return [];
}

export function defaultJourneyId(session: SessionSummary | null): JourneyId {
  return allowedJourneyIds(session)[0] ?? 'supplier';
}

export function canAccessJourney(session: SessionSummary | null, journeyId: JourneyId): boolean {
  return allowedJourneyIds(session).includes(journeyId);
}

export function frontendPackageForJourney(journeyId: JourneyId): FrontendPackageId {
  if (journeyId === 'supplier') {
    return 'supplier';
  }

  if (journeyId === 'customer') {
    return 'customer';
  }

  return 'internal';
}

export function frontendPackageForSession(session: SessionSummary | null): FrontendPackageId {
  if (session && isAdministratorSession(session)) {
    return 'internal';
  }

  return frontendPackageForJourney(defaultJourneyId(session));
}

export function resolveJourneyRoute(journeyId: JourneyId): string {
  const packageId = frontendPackageForJourney(journeyId);
  return packageId === 'internal'
    ? `/internal/${journeyId}`
    : `/${journeyId}`;
}

export function journeyIdFromRoutePath(path: string): JourneyId | null {
  const normalizedPath = path.trim().replace(/^\/+/, '').replace(/\/+$/, '');

  if (!normalizedPath) {
    return null;
  }

  switch (normalizedPath) {
    case 'supplier':
      return 'supplier';
    case 'customer':
      return 'customer';
    case 'internal/accounting-office':
      return 'accounting-office';
    case 'internal/islamic-finance':
      return 'islamic-finance';
    case 'internal/finance-approvals':
      return 'finance-approvals';
    case 'internal/operations-control':
      return 'operations-control';
    case 'internal/reporting-center':
      return 'reporting-center';
    case 'internal/workflow-monitoring':
      return 'workflow-monitoring';
    default:
      return null;
  }
}
