export interface Event {
  id: string;
  slug: string;
  title: string;
  titleEs?: string;
  description: string;
  descriptionEs?: string;
  shortDescription?: string;
  shortDescriptionEs?: string;
  startDatetime: string;
  endDatetime?: string;
  location: string;
  locationUrl?: string;
  price: number;
  currency: string;
  capacity: number;
  status: 'draft' | 'published' | 'unlisted' | 'cancelled' | 'completed' | 'archived';
  bannerUrl?: string;
  externalBookingEnabled?: boolean;
  externalBookingUrl?: string;
  bookedCount?: number; // paid seats (confirmed + checked_in)
  claimedCount?: number; // "I've paid" claims awaiting admin verification (hold seats)
  availableSeats?: number; // capacity - booked - claimed; the server-authoritative number
  isFeatured?: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface LightningInvoice {
  paymentHash: string;
  paymentRequest: string; // BOLT11 invoice
  amount: number; // Amount in satoshis
  fiatAmount?: number; // Original fiat amount
  fiatCurrency?: string; // Original fiat currency
  expiresAt?: string;
}

export interface Ticket {
  id: string;
  bookingId?: string; // Groups multiple tickets from same booking
  bookingTicketCount?: number; // Total tickets in the booking (for per-quantity payment links)
  userId: string;
  eventId: string;
  attendeeFirstName: string;
  attendeeLastName?: string;
  attendeeEmail?: string;
  attendeePhone?: string;
  attendeeRuc?: string;
  preferredLanguage?: string;
  status: 'pending' | 'confirmed' | 'cancelled' | 'checked_in' | 'on_hold';
  checkinAt?: string;
  checkedInByAdminId?: string;
  qrCode: string;
  adminNote?: string;
  isGuest?: boolean;
  paymentStatus?: 'paid' | 'unpaid' | 'comp';
  createdAt: string;
  event?: Event;
  payment?: Payment;
  user?: User;
}

export interface TicketValidationResult {
  valid: boolean;
  status: 'valid' | 'already_checked_in' | 'pending_payment' | 'cancelled' | 'invalid' | 'wrong_event';
  canCheckIn: boolean;
  ticket?: {
    id: string;
    qrCode: string;
    attendeeName: string;
    attendeeEmail?: string;
    attendeePhone?: string;
    status: string;
    paymentStatus?: 'paid' | 'unpaid' | 'comp';
    amountDue?: number;
    checkinAt?: string;
    checkedInBy?: string;
  };
  event?: {
    id: string;
    title: string;
    startDatetime: string;
    location: string;
  };
  error?: string;
}

export interface TicketSearchResult {
  id: string;
  qrCode: string;
  attendeeName: string;
  attendeeEmail?: string;
  attendeePhone?: string;
  status: string;
  checkinAt?: string;
  event?: {
    id: string;
    title: string;
    startDatetime: string;
    location: string;
  } | null;
}

export interface LiveSearchResult {
  ticket_id: string;
  name: string;
  email?: string;
  status: string;
  checked_in: boolean;
  checkinAt?: string;
  event_id: string;
  qrCode: string;
  event?: {
    id: string;
    title: string;
    startDatetime: string;
    location: string;
  } | null;
}

export interface Payment {
  id: string;
  ticketId: string;
  provider: 'bancard' | 'lightning' | 'cash' | 'bank_transfer' | 'tpago';
  amount: number;
  currency: string;
  status: 'pending' | 'pending_approval' | 'paid' | 'refunded' | 'failed' | 'on_hold';
  reference?: string;
  userMarkedPaidAt?: string;
  payerName?: string; // Name of payer if different from attendee
  paidAt?: string;
  paidByAdminId?: string;
  adminNote?: string;
  reminderSentAt?: string; // When payment reminder email was sent
  createdAt: string;
  updatedAt: string;
}

export interface PaymentWithDetails extends Payment {
  ticket: {
    id: string;
    bookingId?: string;
    attendeeFirstName: string;
    attendeeLastName?: string;
    attendeeEmail?: string;
    attendeePhone?: string;
    attendeeRuc?: string;
    status: string;
  } | null;
  event: {
    id: string;
    title: string;
    startDatetime: string;
  } | null;
}

export interface PaymentOptionsConfig {
  tpagoEnabled: boolean;
  tpagoLink?: string | null;
  tpagoLink2?: string | null;
  tpagoLink3?: string | null;
  tpagoLink4?: string | null;
  tpagoLink5?: string | null;
  tpagoInstructions?: string | null;
  tpagoInstructionsEs?: string | null;
  bankTransferEnabled: boolean;
  bankName?: string | null;
  bankAccountHolder?: string | null;
  bankAccountNumber?: string | null;
  bankAlias?: string | null;
  bankPhone?: string | null;
  bankNotes?: string | null;
  bankNotesEs?: string | null;
  lightningEnabled: boolean;
  cashEnabled: boolean;
  cashInstructions?: string | null;
  cashInstructionsEs?: string | null;
  // Booking settings
  allowDuplicateBookings?: boolean;
}

export interface User {
  id: string;
  email: string;
  name: string;
  phone?: string;
  role: 'admin' | 'organizer' | 'staff' | 'marketing' | 'user';
  languagePreference?: string;
  isClaimed?: boolean;
  rucNumber?: string;
  accountStatus?: string;
  createdAt: string;
}

export interface Contact {
  id: string;
  name: string;
  email: string;
  message: string;
  status: 'new' | 'read' | 'replied';
  createdAt: string;
}

export interface AttendeeData {
  firstName: string;
  lastName?: string;
}

export interface BookingData {
  eventId: string;
  firstName: string;
  lastName: string;
  email: string;
  phone: string;
  preferredLanguage?: 'en' | 'es';
  paymentMethod: 'bancard' | 'lightning' | 'cash' | 'bank_transfer' | 'tpago';
  ruc?: string;
  // For multi-ticket bookings
  attendees?: AttendeeData[];
}

export interface DashboardData {
  stats: {
    totalUsers: number;
    totalEvents: number;
    totalTickets: number;
    confirmedTickets: number;
    /** Checkouts opened but never paid nor claimed — informational, holds no seat */
    pendingPayments: number;
    /** Customer says they paid; needs admin verification — actionable */
    awaitingApprovalPayments: number;
    totalRevenue: number;
    newContacts: number;
    totalSubscribers: number;
  };
  upcomingEvents: Event[];
  recentTickets: Ticket[];
}

export interface AnalyticsData {
  events: {
    id: string;
    title: string;
    date: string;
    capacity: number;
    totalBookings: number;
    confirmedBookings: number;
    checkedIn: number;
    revenue: number;
  }[];
}

export interface Media {
  id: string;
  fileUrl: string;
  type: 'image' | 'video' | 'document';
  relatedId?: string;
  relatedType?: string;
  createdAt: string;
}

export interface ExportedTicket {
  ticketId: string;
  ticketStatus: string;
  qrCode: string;
  checkinAt?: string;
  userName: string;
  userEmail: string;
  userPhone?: string;
  eventTitle: string;
  eventDate: string;
  paymentStatus: string;
  paymentAmount: number;
  createdAt: string;
}

export interface EmailTemplate {
  id: string;
  name: string;
  slug: string;
  subject: string;
  subjectEs?: string;
  bodyHtml: string;
  bodyHtmlEs?: string;
  bodyText?: string;
  bodyTextEs?: string;
  description?: string;
  variables: EmailVariable[];
  isSystem: boolean;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

export interface EmailVariable {
  name: string;
  description: string;
  example: string;
}

export interface EmailLog {
  id: string;
  templateId?: string;
  eventId?: string;
  recipientEmail: string;
  recipientName?: string;
  subject: string;
  bodyHtml?: string;
  status: 'pending' | 'sent' | 'failed' | 'bounced';
  errorMessage?: string;
  sentAt?: string;
  sentBy?: string;
  createdAt: string;
  resendAttempts?: number;
  lastResentAt?: string;
}

export interface EmailStats {
  total: number;
  sent: number;
  failed: number;
  pending: number;
}

export interface Pagination {
  total: number;
  limit: number;
  offset: number;
  hasMore: boolean;
}

export interface ExportedPayment {
  paymentId: string;
  amount: number;
  currency: string;
  provider: string;
  status: string;
  reference?: string;
  paidAt?: string;
  createdAt: string;
  ticketId: string;
  attendeeFirstName: string;
  attendeeLastName?: string;
  attendeeEmail?: string;
  attendeeRuc?: string;
  eventId: string;
  eventTitle: string;
  eventDate: string;
}

export interface FinancialSummary {
  totalPayments: number;
  totalPaid: number;
  totalPending: number;
  totalRefunded: number;
  byProvider: {
    bancard: number;
    lightning: number;
    cash: number;
    bank_transfer: number;
    tpago: number;
  };
  paidCount: number;
  pendingCount: number;
  pendingApprovalCount: number;
  refundedCount: number;
  failedCount: number;
}

// ==================== User Dashboard Types ====================

export interface UserProfile {
  id: string;
  email: string;
  name: string;
  phone?: string;
  languagePreference?: string;
  rucNumber?: string;
  isClaimed: boolean;
  accountStatus: string;
  hasPassword: boolean;
  hasGoogleLinked: boolean;
  memberSince: string;
  membershipDays: number;
  createdAt: string;
}

export interface UserTicket extends Ticket {
  invoice?: {
    id: string;
    invoiceNumber: string;
    pdfUrl?: string;
    createdAt: string;
  } | null;
}

export interface UserPayment extends Payment {
  ticket: {
    id: string;
    attendeeFirstName: string;
    attendeeLastName?: string;
    status: string;
  } | null;
  event: {
    id: string;
    title: string;
    titleEs?: string;
    startDatetime: string;
  } | null;
  invoice?: {
    id: string;
    invoiceNumber: string;
    pdfUrl?: string;
  } | null;
}

export interface UserInvoice {
  id: string;
  paymentId: string;
  invoiceNumber: string;
  rucNumber?: string;
  legalName?: string;
  amount: number;
  currency: string;
  pdfUrl?: string;
  status: string;
  createdAt: string;
  event?: {
    id: string;
    title: string;
    titleEs?: string;
    startDatetime: string;
  } | null;
}

export interface UserSession {
  id: string;
  userAgent?: string;
  ipAddress?: string;
  lastActiveAt: string;
  createdAt: string;
  expiresAt?: string;
  /** True for the session backing the current request. */
  current?: boolean;
}

export interface DashboardSummary {
  user: {
    name: string;
    email: string;
    accountStatus: string;
    memberSince: string;
    membershipDays: number;
  };
  stats: {
    totalTickets: number;
    confirmedTickets: number;
    upcomingEvents: number;
    pendingPayments: number;
  };
}

export interface NextEventInfo {
  event: Event;
  ticket: Ticket;
  payment: Payment | null;
}

// ==================== Site Settings Types ====================

export interface SiteSettings {
  id?: string;
  timezone: string;
  siteName: string;
  siteDescription?: string | null;
  siteDescriptionEs?: string | null;
  contactEmail?: string | null;
  contactPhone?: string | null;
  facebookUrl?: string | null;
  instagramUrl?: string | null;
  twitterUrl?: string | null;
  linkedinUrl?: string | null;
  featuredEventId?: string | null;
  maintenanceMode: boolean;
  maintenanceMessage?: string | null;
  maintenanceMessageEs?: string | null;
  updatedAt?: string;
  updatedBy?: string;
}

export interface TimezoneOption {
  value: string;
  label: string;
}

// ==================== Legal Settings Types ====================

export interface LegalSettingsData {
  id?: string;
  companyName?: string | null;
  legalEntityName?: string | null;
  rucNumber?: string | null;
  companyAddress?: string | null;
  companyCity?: string | null;
  companyCountry?: string | null;
  supportEmail?: string | null;
  legalEmail?: string | null;
  governingLaw?: string | null;
  jurisdictionCity?: string | null;
  updatedAt?: string;
  updatedBy?: string;
}

// ==================== Legal Pages Types ====================

export interface LegalPage {
  id: string;
  slug: string;
  title: string;
  titleEs?: string | null;
  contentText: string;
  contentTextEs?: string | null;
  contentMarkdown: string;
  contentMarkdownEs?: string | null;
  updatedAt: string;
  updatedBy?: string | null;
  createdAt: string;
  source?: 'database' | 'filesystem';
  hasEnglish?: boolean;
  hasSpanish?: boolean;
}

export interface LegalPagePublic {
  id?: string;
  slug: string;
  title: string;
  contentMarkdown: string;
  updatedAt?: string;
  source?: 'database' | 'filesystem';
}

export interface LegalPageListItem {
  id: string;
  slug: string;
  title: string;
  updatedAt: string;
  hasEnglish?: boolean;
  hasSpanish?: boolean;
}

// ==================== FAQ Types ====================

export interface FaqItem {
  id: string;
  question: string;
  questionEs?: string | null;
  answer: string;
  answerEs?: string | null;
  rank?: number;
}

export interface FaqItemAdmin extends FaqItem {
  enabled: boolean;
  showOnHomepage: boolean;
  createdAt: string;
  updatedAt: string;
}
