/**
 * Custom-report entity registry — client-safe metadata.
 *
 * This module holds ONLY the pure-data parts of the custom-report
 * registry: entity keys, the filter/column type contracts, the
 * per-entity column allowlists, and the per-entity metadata
 * (label/description/dateAxis/columns). It has NO `@/lib/db`, drizzle,
 * or schema imports, so it is safe to import from client components
 * (e.g. the column-picker UI in the custom report builder).
 *
 * The server-only query logic (`runQuery` + drizzle) lives in
 * `registry.ts`, which imports the column arrays + meta from here and
 * composes the full `ENTITY_REGISTRY` consumed by the run endpoint.
 *
 * Adding a new entity:
 *   1. Append it to ENTITY_KEYS.
 *   2. Add its column array + an ENTITY_META entry here.
 *   3. Add the matching `runQuery` + ENTITY_REGISTRY entry in
 *      `registry.ts`.
 *   4. The UI's entity-picker reads ENTITY_META directly.
 */

export const ENTITY_KEYS = ['clients', 'interests', 'berths', 'tenancies'] as const;
export type EntityKey = (typeof ENTITY_KEYS)[number];

export interface CustomFilter {
  /** ISO 8601 — inclusive lower bound on the entity's "date" column
   *  (createdAt or equivalent — see entity definition). */
  from?: Date;
  /** ISO 8601 — inclusive upper bound. */
  to?: Date;
}

export interface ColumnDefinition {
  /** Stable key. Persisted in saved-template configs. */
  key: string;
  /** Human-readable column header used in CSV/PDF output + the UI
   *  multi-select. */
  label: string;
  /** Default selection in the UI. Reps can uncheck. */
  defaultSelected?: boolean;
}

/**
 * Client-safe metadata for an entity — everything except the
 * server-only `runQuery`. The full `CustomEntityDefinition` (meta +
 * runQuery) lives in `registry.ts`.
 */
export interface CustomEntityMeta {
  key: EntityKey;
  label: string;
  description: string;
  /** Friendly name for the date filter — different entities anchor
   *  the date range to different timestamps. */
  dateAxis: string;
  columns: ColumnDefinition[];
}

// ─── Clients ─────────────────────────────────────────────────────────────────

export const CLIENTS_COLUMNS: ColumnDefinition[] = [
  { key: 'fullName', label: 'Full name', defaultSelected: true },
  { key: 'nationalityIso', label: 'Nationality', defaultSelected: false },
  { key: 'preferredLanguage', label: 'Preferred language' },
  { key: 'preferredContactMethod', label: 'Preferred contact', defaultSelected: false },
  { key: 'source', label: 'Source', defaultSelected: true },
  { key: 'createdAt', label: 'Created', defaultSelected: true },
  { key: 'archivedAt', label: 'Archived at' },
];

// ─── Interests ───────────────────────────────────────────────────────────────

export const INTERESTS_COLUMNS: ColumnDefinition[] = [
  { key: 'clientName', label: 'Client', defaultSelected: true },
  { key: 'primaryBerth', label: 'Primary berth', defaultSelected: true },
  { key: 'pipelineStage', label: 'Stage', defaultSelected: true },
  { key: 'leadCategory', label: 'Lead category' },
  { key: 'outcome', label: 'Outcome', defaultSelected: true },
  { key: 'source', label: 'Source', defaultSelected: false },
  { key: 'depositExpectedAmount', label: 'Deposit expected (amt)', defaultSelected: false },
  { key: 'depositExpectedCurrency', label: 'Deposit expected (ccy)' },
  { key: 'dateFirstContact', label: 'First contact', defaultSelected: false },
  { key: 'dateLastContact', label: 'Last contact', defaultSelected: false },
  { key: 'createdAt', label: 'Created', defaultSelected: true },
];

// ─── Berths ──────────────────────────────────────────────────────────────────

export const BERTHS_COLUMNS: ColumnDefinition[] = [
  { key: 'mooringNumber', label: 'Mooring', defaultSelected: true },
  { key: 'area', label: 'Area' },
  { key: 'status', label: 'Status', defaultSelected: true },
  { key: 'length', label: 'Length (m)' },
  { key: 'width', label: 'Width (m)' },
  { key: 'draft', label: 'Draft (m)' },
  { key: 'price', label: 'Price', defaultSelected: true },
  { key: 'priceCurrency', label: 'Currency' },
  { key: 'createdAt', label: 'Created' },
];

// ─── Tenancies ───────────────────────────────────────────────────────────────

export const TENANCIES_COLUMNS: ColumnDefinition[] = [
  { key: 'clientName', label: 'Client', defaultSelected: true },
  { key: 'mooringNumber', label: 'Berth', defaultSelected: true },
  { key: 'tenureType', label: 'Tenure type', defaultSelected: true },
  { key: 'startDate', label: 'Start', defaultSelected: true },
  { key: 'endDate', label: 'End', defaultSelected: true },
  { key: 'status', label: 'Status', defaultSelected: true },
  { key: 'createdAt', label: 'Created' },
];

// ─── Metadata registry ───────────────────────────────────────────────────────

export const ENTITY_META: Record<EntityKey, CustomEntityMeta> = {
  clients: {
    key: 'clients',
    label: 'Clients',
    description: 'People in your CRM: name, source, contact preferences.',
    dateAxis: 'Created',
    columns: CLIENTS_COLUMNS,
  },
  interests: {
    key: 'interests',
    label: 'Interests / deals',
    description: 'Sales pipeline: stage, outcome, value, deposit details.',
    dateAxis: 'Created',
    columns: INTERESTS_COLUMNS,
  },
  berths: {
    key: 'berths',
    label: 'Berths',
    description: 'Mooring inventory: dimensions, status, price.',
    dateAxis: 'Created',
    columns: BERTHS_COLUMNS,
  },
  tenancies: {
    key: 'tenancies',
    label: 'Tenancies',
    description: 'Berth leases / annual contracts: dates, tenure type, status.',
    dateAxis: 'Created',
    columns: TENANCIES_COLUMNS,
  },
};
