/** Loyalty Platform TypeScript types — mirrors backend DTOs (Phase 7.0–7.6). */

export type ProgramStatus = "draft" | "active" | "suspended" | "archived";
export type MemberStatus =
  | "pending"
  | "active"
  | "suspended"
  | "frozen"
  | "expired"
  | "cancelled"
  | "transferred"
  | "closed";
export type TierStatus = "active" | "inactive" | "archived";
export type PointAccountStatus = "open" | "frozen" | "closed";
export type LedgerEntryType = "earn" | "redeem" | "adjust" | "expire";
export type RewardType = "coupon" | "discount" | "cashback" | "voucher" | "gift" | "custom";
export type RewardStatus = "draft" | "active" | "inactive" | "archived";
export type CampaignStatus = "draft" | "scheduled" | "active" | "paused" | "expired" | "archived";
export type WalletAccountStatus = "open" | "frozen" | "closed";
export type WalletLedgerEntryType = "credit" | "debit" | "adjust" | "transfer_in" | "transfer_out";
export type RedemptionStatus = "pending" | "fulfilled" | "cancelled" | "expired";
export type CampaignApplicationStatus = "applied" | "reversed";
export type ReferralProgramStatus = "draft" | "active" | "inactive";
export type ReferralCodeStatus = "active" | "revoked";
export type ReferralAttributionStatus = "pending" | "converted" | "rewarded" | "cancelled";

export interface HealthResponse {
  status: string;
  service: string;
  version: string;
}

export type PaginationParams = {
  page?: number;
  page_size?: number;
};

export type ListParams = PaginationParams & Record<string, string | number | undefined>;

export interface LoyaltyProgram {
  id: string;
  tenant_id: string;
  code: string;
  name: string;
  description: string | null;
  status: ProgramStatus;
  points_currency_name: string;
  currency_code: string;
  is_default: boolean;
  settings: Record<string, unknown> | null;
  version: number;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface MembershipTier {
  id: string;
  tenant_id: string;
  program_id: string;
  code: string;
  name: string;
  description: string | null;
  rank: number;
  min_points: number;
  status: TierStatus;
  benefits: Record<string, unknown> | null;
  rules: Record<string, unknown> | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface Member {
  id: string;
  tenant_id: string;
  program_id: string;
  membership_number: string;
  external_customer_ref: string | null;
  display_name: string;
  email: string | null;
  mobile: string | null;
  status: MemberStatus;
  tier_id: string | null;
  enrolled_at: string | null;
  activated_at: string | null;
  membership_started_at: string | null;
  membership_expires_at: string | null;
  frozen_at: string | null;
  freeze_reason: string | null;
  cancelled_at: string | null;
  cancel_reason: string | null;
  expired_at: string | null;
  transferred_to_member_id: string | null;
  profile: Record<string, unknown> | null;
  version: number;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface MembershipLifecycleEvent {
  id: string;
  tenant_id: string;
  member_id: string;
  action: string;
  from_status: string;
  to_status: string;
  reason: string | null;
  metadata_json: Record<string, unknown> | null;
  actor_user_id: string | null;
  created_at: string;
}

export interface PointAccount {
  id: string;
  tenant_id: string;
  program_id: string;
  member_id: string;
  account_number: string;
  status: PointAccountStatus;
  currency_name: string;
  version: number;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface PointBalance {
  point_account_id: string;
  member_id: string;
  program_id: string;
  status: PointAccountStatus;
  balance: number;
  currency_name: string;
}

export interface PointLedgerEntry {
  id: string;
  tenant_id: string;
  point_account_id: string;
  program_id: string;
  member_id: string;
  entry_type: LedgerEntryType;
  amount: number;
  balance_after: number;
  idempotency_key: string | null;
  reference_type: string | null;
  reference_id: string | null;
  reason: string | null;
  metadata_json: Record<string, unknown> | null;
  expires_at: string | null;
  actor_user_id: string | null;
  created_at: string;
}

export interface PointLedgerPage {
  items: PointLedgerEntry[];
  total: number;
  offset: number;
  limit: number;
}

export interface Reward {
  id: string;
  tenant_id: string;
  program_id: string;
  code: string;
  name: string;
  description: string | null;
  reward_type: RewardType;
  status: RewardStatus;
  points_cost: number;
  eligibility_rules: Record<string, unknown> | null;
  metadata_json: Record<string, unknown> | null;
  stock_limit: number | null;
  redeemed_count: number;
  max_per_member: number | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface RewardRedemption {
  id: string;
  tenant_id: string;
  program_id: string;
  reward_id: string;
  member_id: string;
  point_account_id: string;
  status: RedemptionStatus;
  points_spent: number;
  ledger_entry_id: string | null;
  voucher_code: string;
  idempotency_key: string | null;
  reason: string | null;
  cancel_reason: string | null;
  fulfilled_at: string | null;
  cancelled_at: string | null;
  metadata_json: Record<string, unknown> | null;
  actor_user_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface RewardRedemptionPage {
  items: RewardRedemption[];
  total: number;
  offset: number;
  limit: number;
}

export interface Campaign {
  id: string;
  tenant_id: string;
  program_id: string;
  code: string;
  name: string;
  description: string | null;
  status: CampaignStatus;
  rule_version: number;
  rules: Record<string, unknown> | null;
  starts_on: string | null;
  ends_on: string | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface CampaignApplication {
  id: string;
  tenant_id: string;
  campaign_id: string;
  program_id: string;
  member_id: string;
  point_account_id: string;
  rule_version: number;
  points_granted: number;
  ledger_entry_id: string | null;
  status: CampaignApplicationStatus;
  idempotency_key: string | null;
  actor_user_id: string | null;
  metadata_json: Record<string, unknown> | null;
  created_at: string;
}

export interface CampaignApplicationPage {
  items: CampaignApplication[];
  total: number;
  offset: number;
  limit: number;
}

export interface ReferralProgram {
  id: string;
  tenant_id: string;
  program_id: string;
  code: string;
  name: string;
  status: ReferralProgramStatus;
  referrer_bonus_points: number;
  referee_bonus_points: number;
  max_referrals_per_member: number | null;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface ReferralCode {
  id: string;
  tenant_id: string;
  referral_program_id: string;
  program_id: string;
  member_id: string;
  code: string;
  status: ReferralCodeStatus;
  uses_count: number;
  created_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface ReferralCodePage {
  items: ReferralCode[];
  total: number;
  offset: number;
  limit: number;
}

export interface ReferralAttribution {
  id: string;
  tenant_id: string;
  referral_program_id: string;
  referral_code_id: string;
  program_id: string;
  referrer_member_id: string;
  referee_member_id: string;
  status: ReferralAttributionStatus;
  referrer_ledger_entry_id: string | null;
  referee_ledger_entry_id: string | null;
  idempotency_key: string | null;
  converted_at: string | null;
  rewarded_at: string | null;
  cancelled_at: string | null;
  cancel_reason: string | null;
  actor_user_id: string | null;
  created_at: string;
  updated_at: string;
}

export interface ReferralAttributionPage {
  items: ReferralAttribution[];
  total: number;
  offset: number;
  limit: number;
}

export interface WalletAccount {
  id: string;
  tenant_id: string;
  program_id: string;
  member_id: string;
  account_number: string;
  currency_code: string;
  status: WalletAccountStatus;
  is_deleted: boolean;
  created_by: string | null;
  updated_by: string | null;
  created_at: string;
  updated_at: string;
}

export interface WalletBalance {
  wallet_account_id: string;
  member_id: string;
  program_id: string;
  status: WalletAccountStatus;
  currency_code: string;
  balance: number;
}

export interface WalletLedgerEntry {
  id: string;
  tenant_id: string;
  wallet_account_id: string;
  program_id: string;
  member_id: string;
  entry_type: WalletLedgerEntryType;
  amount: number;
  balance_after: number;
  idempotency_key: string | null;
  reference_type: string | null;
  reference_id: string | null;
  reason: string | null;
  metadata_json: Record<string, unknown> | null;
  actor_user_id: string | null;
  created_at: string;
}

export interface WalletLedgerPage {
  items: WalletLedgerEntry[];
  total: number;
  offset: number;
  limit: number;
}

export interface WalletTransferResult {
  source_entry: WalletLedgerEntry;
  target_entry: WalletLedgerEntry;
}

export interface AuditEntry {
  id: string;
  tenant_id: string;
  entity_type: string;
  entity_id: string;
  action: string;
  actor_user_id: string | null;
  changes: Record<string, unknown> | null;
  message: string | null;
  created_at: string;
}
