import type { ResolvedReportContext } from './types';

export function resolveCrmAccess(input: {
  organizationId: string;
  userId: string;
  membership: {
    role: string;
    businessRole: string;
    businessRoles?: string[];
    permissions?: string[] | null;
    branchScopeIds?: string[] | null;
    productTypeScopeIds?: string[] | null;
    ownershipScope?: string | null;
    branchScopeMode?: string | null;
    productScopeMode?: string | null;
    approvalAuthority?: string | null;
  };
}): ResolvedReportContext {
  return {
    organizationId: input.organizationId,
    userId: input.userId,
    membershipRole: input.membership.role,
    businessRole: input.membership.businessRole,
    businessRoles: input.membership.businessRoles ?? [input.membership.businessRole],
    permissions: input.membership.permissions ?? [],
    branchScopeIds: input.membership.branchScopeIds ?? [],
    productTypeScopeIds: input.membership.productTypeScopeIds ?? [],
    ownershipScope: input.membership.ownershipScope ?? 'organization',
    branchScopeMode: input.membership.branchScopeMode ?? 'all',
    productScopeMode: input.membership.productScopeMode ?? 'all',
    approvalAuthority: input.membership.approvalAuthority ?? null
  };
}
