export interface Subscription {
  id: number;
  company_id: number;
  company_name: string;
  status: string;
  is_trial: boolean;
  trial_ends_at: string | null;
  starts_at: string | null;
  ends_at: string | null;
  canceled_at: string | null;
  plan: { id: number; name: string } | null;
  created_at: string;
}

export interface SubscriptionStats {
  total: number;
  active: number;
  trial: number;
  expired_trial: number;
}

export interface UpdateCompanyPlanPayload {
  subscription_plan_id: number;
}

/** Plan changes during the month and cancellation (billing automation, part 2). */
export interface PlanChangePreview {
  from: { id: number; name: string; price_per_vehicle: number } | null;
  to: { id: number; name: string; price_per_vehicle: number };
  effective_at: string;
  /** The prorated difference for the rest of the month, carried by the next invoice. */
  adjustment: {
    amount_ht: number;
    amount_ttc: number;
    vehicles: number;
    days: number;
    days_in_month: number;
    label: string;
  } | null;
  /** vehicle_plans: billed by its vehicles' plans, the amount is theirs, not vehicles × unit price. */
  next_month: { vehicles_count: number; unit_price: number; amount_ht: number; amount_ttc: number; currency: string; vehicle_plans?: boolean };
}

export type SubscriptionChangeKind = "plan_change" | "cancellation" | "cancellation_undone" | "vehicle_plans"
  // The 15-day trial of Fleet Business (2026-09-29): started, stopped by the client, taken back,
  // become paid, or ended back on the free plan.
  | "trial_started" | "trial_stopped" | "trial_resumed" | "trial_converted" | "trial_ended";

/** A company's 15-day trial of Fleet Business: bank details given first, nothing billed until it ends. */
export interface CompanyTrial {
  running: boolean;
  /** YYYY-MM-DD */
  started_on: string;
  /** YYYY-MM-DD: the trial's last day, billed from the day after. */
  ends_on: string;
  /** Stopped by the client: back on the free plan at the end, nothing billed. */
  stopped: boolean;
  converted: boolean;
  ended: boolean;
  method: "card" | "sepa";
  card: { brand: string | null; last_four: string } | null;
}

export interface SubscriptionHistoryItem {
  id: number;
  kind: SubscriptionChangeKind;
  from_plan: string | null;
  to_plan: string | null;
  /** vehicle_plans: what was added and taken off ("Ajouté : Fleet Business ×2 (2 véhicules)"). */
  detail?: string | null;
  effective_at: string;
  /** YYYY-MM-DD of effective_at, in the platform's time zone. */
  effective_on: string;
  reason: string | null;
  source: "client" | "admin";
  by: string | null;
  adjustments: { label: string; amount_ht: number; invoiced: boolean }[];
  created_at: string | null;
}

export interface CompanySubscriptionState {
  cancellation: {
    ends_at: string;
    /** The last day of access, YYYY-MM-DD: shown as is, whatever the browser's time zone. */
    ends_on: string;
    requested_at: string | null;
    reason: string | null;
    source: "client" | "admin";
    ended: boolean;
  } | null;
  can_change_plan: boolean;
  can_cancel: boolean;
  support_email: string;
  pending_adjustments: { id: number; label: string; amount_ht: number; created_at: string | null }[];
  history: SubscriptionHistoryItem[];
  /** The trial of Fleet Business, when the company started one. */
  trial?: CompanyTrial | null;
}

export interface CancelSubscriptionPayload {
  /** YYYY-MM-DD; the end of the current month when left out. */
  ends_at?: string | null;
  reason?: string | null;
}
