/**
 * Mobile Water Log — shared types.
 *
 * Mirror of the server-returned headgate shape from
 * `src/actions/water-log/field.ts`. Keep these in sync if the server
 * shape changes.
 */

export type FieldHeadgate = {
  id: string;
  name: string;
  unit: string;
  status: string;
  high_threshold: number | null;
  low_threshold: number | null;
  active: boolean;
  /** Free-text location description / coordinate string ("geocode"). */
  notes: string | null;
  /** ISO timestamp of last successful log. */
  last_used_at: string | null;
  /**
   * Unit from the most recent log entry for this headgate, or
   * `null` if none have been logged yet. The log form's unit picker
   * prefers this over the headgate's configured `unit` so operators
   * don't have to re-pick on every visit.
   */
  last_unit: string | null;
};

/** The units the mobile app offers in its segmented control.
 *
 * The order is significant — CFS is the default for Tuxedo headgates
 * (it's the most common measurement). "holes" is the count of open
 * holes on a headgate plate, used by some Tuxedo operations where
 * the field is metered by orifice count rather than CFS.
 */
export const MOBILE_UNITS = [
  "CFS",
  "GPM",
  "gal",
  "ac-in",
  "ac-ft",
  "holes",
] as const;
export type MobileUnit = (typeof MOBILE_UNITS)[number];

/** True when the unit is an integer count (no decimal allowed). */
export function isIntegerUnit(unit: string): boolean {
  return unit === "holes";
}

/** Top-level screen identifiers — drives the state machine. */
export type Screen = "pin" | "headgates" | "log" | "success";

/** A submitted log entry (only kept locally for the success screen). */
export type SubmittedLog = {
  headgateName: string;
  measurement: number;
  unit: MobileUnit | string;
  timestamp: Date;
};