export const PTW_TREND_PERIOD_OPTIONS = [
  { value: "7d", label: "Weekly" },
  { value: "30d", label: "Monthly" },
  { value: "90d", label: "Quarterly" },
  { value: "year", label: "Yearly" }
];

/** @deprecated Use PTW_TREND_PERIOD_OPTIONS for trend chart */
export const DASHBOARD_PERIOD_OPTIONS = [
  { id: "7d", label: "Last 7 days" },
  { id: "30d", label: "Last 30 days" },
  { id: "90d", label: "Last 90 days" },
  { id: "year", label: "This year" },
  { id: "all", label: "All time" }
];

export function chartScaleMax(maxValue) {
  const n = Number(maxValue) || 0;
  if (n <= 0) return 5;
  if (n <= 5) return 5;
  if (n <= 10) return 10;
  const mag = 10 ** Math.floor(Math.log10(n));
  const norm = n / mag;
  const ceilNorm = norm <= 1 ? 1 : norm <= 2 ? 2 : norm <= 5 ? 5 : 10;
  return ceilNorm * mag;
}

export function formatActivityTime(raw) {
  if (!raw) return "—";
  const date = new Date(raw);
  if (Number.isNaN(date.getTime())) return String(raw);
  return date.toLocaleString(undefined, {
    month: "short",
    day: "numeric",
    hour: "2-digit",
    minute: "2-digit"
  });
}

export const DASHBOARD_FILTER_LABELS = {
  my: "My permits",
  location: "My location permits",
  active: "Active permits",
  closed: "Closed permits"
};

export const DASHBOARD_CARD_TARGETS = {
  my_permits: { navId: "permits-submissions", dashFilter: "my" },
  my_location_permits: { navId: "permits-submissions", dashFilter: "location" },
  active: { navId: "permits-submissions", dashFilter: "active" },
  closed: { navId: "permits-submissions", dashFilter: "closed" }
};

export function isActivePermitRow(row) {
  if (Number(row?.permit_status_id) === 2) return true;
  if (Number(row?.permit_status_id) === 3) return false;
  if (row?.valid_until) {
    return new Date(row.valid_until).getTime() > Date.now();
  }
  return false;
}

export function isClosedPermitRow(row) {
  if (Number(row?.permit_status_id) === 3) return true;
  if (row?.valid_until && row?.valid_from) {
    return new Date(row.valid_until).getTime() <= Date.now();
  }
  return false;
}

export function effectivePermitLocation(row) {
  if (row?.location_id != null) return Number(row.location_id);
  if (row?.uploader_location_id != null) return Number(row.uploader_location_id);
  return null;
}

export function filterPermitsByDashboardFilter(
  permits,
  dashFilter,
  { userId = null, locationId = null } = {}
) {
  const list = Array.isArray(permits) ? permits : [];
  const filter = String(dashFilter || "").trim().toLowerCase();
  if (!filter) return list;

  switch (filter) {
    case "my":
      return list.filter(
        (row) => userId && Number(row.uploader_user_id) === Number(userId)
      );
    case "location": {
      if (!locationId) return list;
      const targetLoc = Number(locationId);
      return list.filter((row) => {
        const effective = effectivePermitLocation(row);
        if (effective != null) return effective === targetLoc;
        return userId && Number(row.uploader_user_id) === Number(userId);
      });
    }
    case "active":
      return list.filter(isActivePermitRow);
    case "closed":
      return list.filter(isClosedPermitRow);
    default:
      return list;
  }
}

export function handleDashboardActivate(event, callback) {
  if (!callback) return;
  if (event.key === "Enter" || event.key === " ") {
    event.preventDefault();
    callback();
  }
}
