import { redirect } from 'next/navigation';
import {
  AuthError,
  getCurrentUser,
  requireEmployee,
  requireHRD,
  requireIT,
  requirePermission,
  requireSuperAdminOrganizationAccess,
  requireUserManagementAccess
} from '@/lib/auth/session';
import type { Permission } from '@/lib/auth/permissions';
import { isSuperAdmin } from '@/lib/auth/roles';

function getForbiddenRedirectPath(
  user: Awaited<ReturnType<typeof getCurrentUser>>,
  fallbackPath: string
) {
  if (isSuperAdmin(user?.systemRole) && !user?.activeOrganizationId) {
    return '/dashboard/organizers';
  }

  return fallbackPath;
}

export async function requireEmployeeDashboardAccess(fallbackPath = '/dashboard/overview') {
  try {
    return await requireEmployee();
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}

export async function requireHRDDashboardAccess(fallbackPath = '/dashboard/overview') {
  try {
    return await requireHRD();
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}

export async function requireITDashboardAccess(fallbackPath = '/dashboard/organizers') {
  try {
    return await requireIT();
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}

export async function requireUserManagementDashboardAccess(fallbackPath = '/dashboard/overview') {
  try {
    return await requireUserManagementAccess();
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}

export async function requireSuperAdminDashboardAccess(fallbackPath = '/dashboard/organizers') {
  try {
    return await requireSuperAdminOrganizationAccess();
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}

export async function requirePermissionDashboardAccess(
  permission: Permission,
  fallbackPath = '/dashboard/overview'
) {
  try {
    return await requirePermission(permission);
  } catch (error) {
    if (error instanceof AuthError) {
      if (error.status === 401) {
        redirect('/auth/sign-in');
      }

      const user = await getCurrentUser();
      redirect(getForbiddenRedirectPath(user, fallbackPath));
    }

    throw error;
  }
}
