/**
 * Plan entitlements (SCRUM-353): what a plan includes, and per company the exceptions to
 * its plan. The catalogue comes from the backend (config/entitlements.php).
 */
export type EntitlementState = "allow" | "preview" | "deny";

export type QuotaPeriod = "lifetime" | "month";

/** live: counted from the data · ledger: consumed per period · setting: a plan value. */
export type QuotaCounter = "live" | "ledger" | "setting";

export interface CapabilityDefinition {
  key: string;
  label: string;
  /** The backend can serve a restricted preview of it. */
  preview: boolean;
}

export interface QuotaDefinition {
  key: string;
  label: string;
  counter: QuotaCounter;
  periodic: boolean;
}

export interface EntitlementCatalogue {
  states: EntitlementState[];
  periods: QuotaPeriod[];
  capabilities: CapabilityDefinition[];
  quotas: QuotaDefinition[];
}

export interface PlanEntitlements {
  plan: {
    id: number;
    name: string;
    display_name: string | null;
    is_active: boolean;
    unlimited: boolean;
    default: EntitlementState;
    companies_count: number;
  };
  capabilities: (CapabilityDefinition & { state: EntitlementState; source: "line" | "default" })[];
  quotas: (QuotaDefinition & { limit: number | null; period: QuotaPeriod | null; source: "line" | "default" })[];
}

export interface UpdatePlanEntitlementsPayload {
  default?: EntitlementState;
  /** A null state removes the plan's line: the plan's default applies again. */
  capabilities?: { key: string; state: EntitlementState | null }[];
  /** limit null = no ceiling; remove = back to the plan's default. */
  quotas?: { key: string; limit?: number | null; period?: QuotaPeriod | null; remove?: boolean }[];
}

export interface EntitlementOverride {
  state: EntitlementState | null;
  limit: number | null;
  period: QuotaPeriod | null;
  note: string | null;
  expires_at: string | null;
  expired: boolean;
  set_by: number | null;
  updated_at: string | null;
}

export interface CompanyEntitlements {
  company: {
    id: number;
    company_id: number;
    name: string;
    plan: { id: number; name: string; unlimited: boolean } | null;
  };
  capabilities: (CapabilityDefinition & { state: EntitlementState; plan_state: EntitlementState; override: EntitlementOverride | null })[];
  quotas: (QuotaDefinition & {
    limit: number | null;
    period: QuotaPeriod | null;
    unlimited: boolean;
    plan_limit: number | null;
    used: number | null;
    override: EntitlementOverride | null;
  })[];
}

export interface SetOverridePayload {
  state?: EntitlementState;
  limit?: number | null;
  period?: QuotaPeriod | null;
  note?: string | null;
  expires_at?: string | null;
}

/** A refusal worth a sales call (freemium spec 8): one company, one kind of signal. */
export interface UpsellSignalRow {
  /** The company's id. */
  id: number;
  name: string;
  plan: string | null;
  vehicles: number;
  owner: string | null;
  email: string | null;
  phone: string | null;
  signal: {
    id: number;
    key: string;
    label: string;
    occurrences: number;
    first_seen_at: string | null;
    last_seen_at: string | null;
    /** Never sent to the sales team yet. */
    new: boolean;
    routed_at: string | null;
    handled_at: string | null;
    handled_by: string | null;
    /** Never handled, or tried again after it was. */
    open: boolean;
  };
}

export interface UpsellSignalOptions {
  keys: { value: string; label: string }[];
}
