/**
 * Shared types for the marketing automation surface (Task #215).
 * Kept in one place so the segment evaluator, campaign runner, dispatcher,
 * and API validators all agree on the JSON shape that flows through them.
 */

export type CampaignChannel = 'email' | 'whatsapp';
export type CampaignStatus =
  | 'draft' | 'scheduled' | 'sending' | 'completed' | 'cancelled' | 'failed';
export type DeliveryStatus =
  | 'queued' | 'sending' | 'sent' | 'failed' | 'skipped';

export type AudienceChannel = 'any' | 'email' | 'whatsapp';

/**
 * Audience rules. Empty/undefined fields are treated as "no constraint".
 *  - branchIds[]            customers whose recent activity touched these branches
 *                           (or with no branch activity, when branchIds is empty)
 *  - orderRecencyDays       customers with at least one order within the window
 *  - orderCountMin          minimum total_visits (lifetime)
 *  - lifetimeSpendMin       minimum total_spend (lifetime)
 *  - tags[]                 customer must carry at least one of the listed tags
 *  - channel                'email' requires non-null email; 'whatsapp' requires phone
 *  - includeOptedOut        defaults false — opt_out=true rows excluded
 */
export interface AudienceRules {
  branchIds?: string[];
  orderRecencyDays?: number;
  orderCountMin?: number;
  lifetimeSpendMin?: number;
  tags?: string[];
  channel?: AudienceChannel;
  includeOptedOut?: boolean;
}

export interface SegmentRow {
  id: string;
  restaurant_id: string;
  /** When set, the segment belongs to a single branch — only that branch's
   *  staff (and restaurant owners) may list / target / edit it. NULL means
   *  the segment is restaurant-wide and only owners can see it. */
  branch_id: string | null;
  name: string;
  description: string | null;
  rules: AudienceRules;
  created_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface CampaignRow {
  id: string;
  restaurant_id: string;
  branch_id: string | null;
  segment_id: string | null;
  audience_rules: AudienceRules;
  name: string;
  channel: CampaignChannel;
  subject: string | null;
  body_html: string | null;
  template_name: string | null;
  template_lang: string | null;
  template_vars: string[];
  status: CampaignStatus;
  scheduled_at: string | null;
  ignore_quiet_hours: boolean;
  audience_count: number;
  sent_count: number;
  failed_count: number;
  opt_out_count: number;
  created_by: string | null;
  created_at: string;
  updated_at: string;
  started_at: string | null;
  completed_at: string | null;
  last_error: string | null;
  /** Optional sibling-group UUID — see CampaignDraftInput.launchGroupId. */
  launch_group_id: string | null;
  /** Set when the linked saved audience was deleted while this campaign was
   *  still non-completed. The campaign keeps its snapshotted audience_rules
   *  and continues to send, but the UI surfaces a badge so operators know
   *  edits to the original segment definition are no longer possible. */
  segment_deleted_at: string | null;
  /**
   * Per-campaign throttle controls (Task #240). NULL = no per-campaign cap.
   * Enforced by the dispatcher in addition to the global per-(branch,channel)
   * batch cap, so a single very-large send can be slowed without affecting
   * other campaigns from the same branch.
   */
  max_per_minute: number | null;
  max_per_hour: number | null;
}

export interface DeliveryRow {
  id: string;
  campaign_id: string;
  restaurant_id: string;
  customer_id: string | null;
  channel: CampaignChannel;
  recipient: string;
  status: DeliveryStatus;
  attempts: number;
  provider_message_id: string | null;
  last_error: string | null;
  scheduled_for: string;
  sent_at: string | null;
  created_at: string;
  updated_at: string;
}

export interface RecipientPreviewItem {
  customer_id: string;
  name: string;
  email: string | null;
  phone: string | null;
}
