import type { DepartmentKey, UserRole } from "@/lib/types";

const DEFAULT_ELEVATED_EMAILS = ["mdares03@gmail.com"];
const DEFAULT_VIEW_DEPARTMENT: DepartmentKey = "marketing";

const ROLE_VALUES = new Set<UserRole>(["owner", "leader", "employee"]);
const DEPARTMENT_VALUES = new Set<DepartmentKey>([
  "marketing",
  "administracion",
  "capital_humano",
  "operaciones",
  "proyectos",
]);

export type ResolvedViewContext = {
  role: UserRole;
  department: DepartmentKey | null;
  departmentRole: string | null;
  isViewingAs: boolean;
};

function normalizeEmail(value: string | null | undefined): string {
  return value?.trim().toLowerCase() ?? "";
}

function readConfiguredElevatedEmails(): Set<string> {
  const configured = (process.env.ELEVATED_ACCESS_EMAILS ?? "")
    .split(",")
    .map((value) => normalizeEmail(value))
    .filter((value) => value.length > 0);

  return new Set([...DEFAULT_ELEVATED_EMAILS, ...configured]);
}

export function canUseElevatedAccess(email: string | null | undefined): boolean {
  const normalized = normalizeEmail(email);
  if (!normalized) {
    return false;
  }

  return readConfiguredElevatedEmails().has(normalized);
}

export function parseUserRole(value: unknown): UserRole | null {
  if (typeof value !== "string") {
    return null;
  }

  const normalized = value.trim().toLowerCase();
  if (ROLE_VALUES.has(normalized as UserRole)) {
    return normalized as UserRole;
  }

  return null;
}

export function parseDepartmentKey(value: unknown): DepartmentKey | null {
  if (typeof value !== "string") {
    return null;
  }

  const normalized = value.trim().toLowerCase();
  if (DEPARTMENT_VALUES.has(normalized as DepartmentKey)) {
    return normalized as DepartmentKey;
  }

  return null;
}

export function parseDepartmentRole(value: unknown): string | null {
  if (typeof value !== "string") {
    return null;
  }

  const normalized = value.trim();
  if (!normalized || normalized.length > 120) {
    return null;
  }

  return normalized;
}

type ResolveViewContextInput = {
  baseRole: UserRole;
  baseDepartment: DepartmentKey | null;
  baseDepartmentRole: string | null;
  viewAsRole: UserRole | null;
  viewAsDepartment: DepartmentKey | null;
  viewAsDepartmentRole: string | null;
};

export function resolveViewContext(input: ResolveViewContextInput): ResolvedViewContext {
  const { baseRole, baseDepartment, baseDepartmentRole, viewAsRole, viewAsDepartment, viewAsDepartmentRole } = input;

  if (!viewAsRole) {
    return {
      role: baseRole,
      department: baseDepartment,
      departmentRole: baseDepartmentRole,
      isViewingAs: false,
    };
  }

  if (viewAsRole === "owner") {
    return {
      role: viewAsRole,
      department: viewAsDepartment ?? baseDepartment,
      departmentRole: viewAsDepartmentRole ?? baseDepartmentRole,
      isViewingAs: true,
    };
  }

  return {
    role: viewAsRole,
    department: viewAsDepartment ?? baseDepartment ?? DEFAULT_VIEW_DEPARTMENT,
    departmentRole: viewAsDepartmentRole ?? baseDepartmentRole,
    isViewingAs: true,
  };
}
