export const ROLE_PROFILE = {
  END_USER: 'End User',
  PHCC_END_USER: 'PHCC End User',
  TECHNICIAN: 'Technician',
} as const;

export type SidebarLinkId =
  | 'dashboard'
  | 'assets'
  | 'inventory'
  | 'work-orders'
  | 'ppm-planner'
  | 'maintenance-calendar'
  | 'active-map'
  | 'maintenance-teams'
  | 'procurement'
  | 'sla'
  | 'support';

const END_USER_LINKS: SidebarLinkId[] = [
  'assets',
  'work-orders',
  'support',
];

const TECHNICIAN_LINKS: SidebarLinkId[] = [
  'dashboard',
  'assets',
  'inventory',
  'work-orders',
  'maintenance-calendar',
  'active-map',
  'procurement',
  'support',
];

export function isEndUserRole(roleProfile?: string | null): boolean {
  return (
    roleProfile === ROLE_PROFILE.END_USER ||
    roleProfile === ROLE_PROFILE.PHCC_END_USER
  );
}

export function isTechnicianRole(roleProfile?: string | null): boolean {
  return roleProfile === ROLE_PROFILE.TECHNICIAN;
}

export function getDefaultRoute(roleProfile?: string | null): string {
  if (isEndUserRole(roleProfile)) {
    return '/work-orders';
  }
  return '/dashboard';
}

export function isSidebarLinkVisible(
  linkId: SidebarLinkId,
  roleProfile?: string | null
): boolean {
  if (isEndUserRole(roleProfile)) {
    return END_USER_LINKS.includes(linkId);
  }

  if (isTechnicianRole(roleProfile)) {
    return TECHNICIAN_LINKS.includes(linkId);
  }

  return true;
}

export function getStoredUserEmail(): string {
  try {
    const user = localStorage.getItem('user');
    if (!user) return '';
    const parsed = JSON.parse(user);
    return parsed.email || parsed.user_id || parsed.name || '';
  } catch {
    return '';
  }
}

export function getStoredRoleProfile(): string {
  try {
    const user = localStorage.getItem('user');
    if (!user) return '';
    const parsed = JSON.parse(user);
    return parsed.role_profile_name || '';
  } catch {
    return '';
  }
}

export function buildWorkOrderRoleFilters(
  roleProfile: string | null | undefined,
  userEmail: string
): { filters?: Record<string, any>; or_filters?: any[] } | null {
  if (!userEmail) return null;

  if (isEndUserRole(roleProfile)) {
    return { filters: { owner: userEmail } };
  }

  if (isTechnicianRole(roleProfile)) {
    return {
      or_filters: [
        ['owner', '=', userEmail],
        ['custom_assign_to_contractor', '=', userEmail],
        ['assigned_technician', '=', userEmail],
      ],
    };
  }

  return null;
}

export type WorkOrderListQuery = {
  filters?: Record<string, any> | any[];
  or_filters?: any[];
};

export function mergeWorkOrderFilters(
  userFilters: Record<string, any>,
  permissionFilters: Record<string, any>,
  roleProfile?: string | null,
  userEmail?: string
): WorkOrderListQuery {
  const merged: Record<string, any> = { ...userFilters };

  for (const [field, value] of Object.entries(permissionFilters)) {
    if (!merged[field]) {
      merged[field] = value;
    }
  }

  const roleFilters = buildWorkOrderRoleFilters(roleProfile, userEmail || '');

  if (!roleFilters) {
    return { filters: merged };
  }

  return {
    filters: {
      ...merged,
      ...(roleFilters.filters || {}),
    },
    ...(roleFilters.or_filters ? { or_filters: roleFilters.or_filters } : {}),
  };
}
