export interface TicketBatch {
  id: string;
  name: string;
  price: number;
  ticketsPerUnit?: number;
  description?: string;
  available: boolean;
  quantity?: number;
  sold?: number;
  status?: 'active' | 'sold_out' | 'paused';
  isUnlimited?: boolean;
  remaining?: number | null;
  startDate?: Date;
  endDate?: Date;
}

export interface EventModel {
  id: string;
  title: string;
  date: Date;
  location: string;
  attractions: string[];
  imageUrl: string;
  description: string;
  batches: TicketBatch[];
  status: 'active' | 'featured' | 'sold_out' | 'finished' | 'draft' | 'last_tickets';
  featured?: boolean;
  category?: string;
  slug?: string;
  horario?: string;
  totalTickets?: number;
  isUnlimited?: boolean;
  remainingTickets?: number | null;
  internalNotes?: string;
}

export interface Ticket {
  id: string;
  code: string;
  batchName: string;
  holderName?: string | null;
  holderEmail?: string | null;
  ticketEmailSentAt?: string | null;
  ticketEmailError?: string | null;
  status: 'unused' | 'used' | 'cancelled';
  usedAt?: Date;
  validatedBy?: string;
  qrValue?: string;
}

export interface Sale {
  id: string;
  orderNumber: string;
  orderType?: 'ticket' | 'membership';
  buyerName: string;
  email: string;
  whatsapp: string;
  customerCpf?: string | null;
  eventId: string;
  eventTitle: string;
  eventImageUrl?: string | null;
  quantity: number;
  expectedTicketCount?: number;
  missingTicketCount?: number;
  subtotalValue?: number;
  serviceFee?: number;
  totalValue: number;
  paymentMethod: 'pix' | 'credit_card';
  paymentStatus: 'pending' | 'paid' | 'cancelled' | 'refunded';
  purchaseDate: Date;
  tickets: Ticket[];
  ticketStatus: 'unused' | 'used' | 'cancelled';
  paymentId?: string | null;
  ticketEmailSentAt?: string | null;
  ticketEmailError?: string | null;
}


export interface VenueConfig {
  name: string;
  logoUrl?: string;
  whatsapp: string;
  instagram: string;
  email?: string;
  address: string;
  googleMapsLink: string;
  wazeLink: string;
  pixKey: string;
  serviceTax: number;
  confirmationMessage: string;
  whatsappTemplate: string;
  asaasEnabled?: boolean;
  asaasEnvironment?: 'sandbox' | 'production';
  asaasFeePerOrder?: number;
  organizerPixKey?: string;
  organizerPixKeyType?: string;
  organizerRecipientName?: string;
  organizerAutoWithdrawThreshold?: number;
  organizerManualWithdrawFee?: number;
  organizerAutoWithdrawFee?: number;
  platformPixKey?: string;
  platformPixKeyType?: string;
  platformRecipientName?: string;
}

export interface WalletTransaction {
  id: string;
  direction: 'credit' | 'debit';
  amount: number;
  category: string;
  description: string;
  referenceType?: string | null;
  referenceId?: string | null;
  createdAt: Date;
}

export interface WalletOverview {
  organizer: {
    balance: number;
    availableForManualWithdraw: number;
    autoWithdrawThreshold: number;
    manualWithdrawFee: number;
    autoWithdrawFee: number;
    pixKey?: string | null;
    pixKeyType?: string | null;
    recipientName?: string | null;
    transactions: WalletTransaction[];
  };
  platform?: {
    balance: number;
    availableForManualWithdraw: number;
    pixKey?: string | null;
    pixKeyType?: string | null;
    recipientName?: string | null;
    transactions: WalletTransaction[];
  };
  advancedUnlocked: boolean;
}

export interface WithdrawalResponse {
  withdrawal: {
    id: number;
    ownerType: string;
    triggerType: string;
    amountGross: number;
    feeAmount: number;
    amountNet: number;
    destinationPixKey: string;
    destinationPixKeyType: string;
    destinationName?: string | null;
    status: string;
    requestedBy?: string | null;
    createdAt?: string | null;
    processedAt?: string | null;
    errorMessage?: string | null;
  };
  wallet: WalletOverview;
}

export interface Partner {
  id: string;
  name: string;
  whatsapp: string;
  cpf: string;
  birthDate: string;
  email?: string;
  photoUrl?: string;
  planId: string;
  startDate: Date;
  expiryDate: Date;
  status: 'active' | 'expired' | 'pending' | 'blocked';
  memberCode?: string;
  qrValue?: string;
  lastValidatedAt?: Date;
  lastValidatedBy?: string;
  notes?: string;
}

export interface MembershipPlan {
  id: string;
  name: string;
  price: number;
  validityDays: number;
  benefits: string[];
}

export interface LeisureActivity {
  id: string;
  title: string;
  date: Date;
  time: string;
  description: string;
  imageUrl: string;
  acceptsPartner: boolean;
  guestPrice?: number;
}

export interface GiftClaim {
  id: string;
  firstName: string;
  lastName: string;
  cpf?: string | null;
  whatsapp: string;
  claimDate: Date;
  code: string;
  status: 'confirmed' | 'used' | 'cancelled';
  usedAt?: Date;
  guestAllowance?: number;
}

export interface GalleryImage {
  id: string;
  url: string;
  type: 'instagram' | 'upload';
  title?: string;
  displayOrder: number;
}


export interface Promoter {
  id: string;
  name: string;
  slug?: string | null;
  code: string;
  instagram?: string | null;
  whatsapp?: string | null;
  notes?: string | null;
  landingUrl?: string | null;
  isActive: boolean;
  clicks: number;
  totalOrders: number;
  paidOrders: number;
  totalRevenue: number;
  generatedLink: string;
  lastClickAt?: string | null;
  createdAt?: string | null;
}

export interface TicketHolderInput {
  name: string;
  email: string;
}

export interface PurchaseFormData {
  name: string;
  email: string;
  whatsapp: string;
  cpf: string;
  batchId: string;
  quantity: number;
  paymentMethod: 'pix' | 'credit_card';
  sendTicketsSeparately?: boolean;
  ticketHolders: TicketHolderInput[];
}

export interface HeroBanner {
  id: number;
  title: string;
  subtitle?: string;
  description?: string;
  image_url: string;
  cta_label?: string;
  cta_link?: string;
  is_active: number;
  display_order: number;
}

export interface AppBootstrapData {
  settings: VenueConfig;
  heroBanners: HeroBanner[];
  events: EventModel[];
  membershipPlans: MembershipPlan[];
  leisureActivities: LeisureActivity[];
  partners: Partner[];
  sales: Sale[];
  giftClaims: GiftClaim[];
  galleryImages: GalleryImage[];
}

export interface PixCheckoutData {
  encodedImage: string | null;
  payload: string | null;
  expirationDate: string | null;
}

export interface TicketOrderResponse {
  orderNumber: string;
  totalValue: number;
  eventTitle: string;
  pix: PixCheckoutData;
  payment: {
    id: string | null;
    status: string;
    invoiceUrl: string | null;
    method?: 'pix' | 'credit_card';
  };
}

export interface OrderStatusResponse extends Sale {}

export interface PartnerOrderResponse {
  orderNumber: string;
  partner: {
    name: string;
    cpf: string;
    whatsapp: string;
    email?: string | null;
    planId: string;
    status: Partner['status'];
  };
  pix: PixCheckoutData;
  payment: {
    id: string | null;
    status: string;
    invoiceUrl: string | null;
  };
}

export interface TicketValidationResult {
  orderNumber: string;
  buyerName: string;
  customerCpf?: string | null;
  whatsapp: string;
  eventId?: string;
  eventTitle: string;
  paymentStatus: Sale['paymentStatus'];
  tickets: Ticket[];
}

export interface TicketValidationListItem {
  orderId: string;
  orderNumber: string;
  buyerName: string;
  customerCpf?: string | null;
  whatsapp?: string | null;
  email?: string | null;
  paymentStatus: Sale['paymentStatus'];
  quantity: number;
  totalValue: number;
  purchaseDate?: string | null;
  eventTitle: string;
  eventDate?: string | null;
  ticket: Ticket;
}

export interface PartnerValidationResult {
  id: string;
  name: string;
  cpf: string;
  whatsapp: string;
  status: Partner['status'];
  planId: string;
  memberCode?: string;
  qrValue?: string;
  expiryDate?: Date;
  lastValidatedAt?: Date;
  lastValidatedBy?: string;
}
