/**
 * @ai-summary TypeScript types for the email ingestion feature
 * @ai-context Covers database records, status enums, and Resend webhook payloads
 */

// ========================
// Status Enums
// ========================

export type EmailIngestionStatus = 'pending' | 'processing' | 'completed' | 'failed';

export type PendingAssociationStatus = 'pending' | 'resolved' | 'expired';

export type EmailRecordType = 'fuel_log' | 'maintenance_record';

// ========================
// Receipt Classification
// ========================

export type ReceiptClassificationType = 'fuel' | 'maintenance' | 'unclassified';

export interface ClassificationResult {
  type: ReceiptClassificationType;
  confidence: number;
}

// ========================
// Database Records
// ========================

export interface EmailIngestionQueueRecord {
  id: string;
  emailId: string;
  senderEmail: string;
  userId: string;
  receivedAt: string;
  subject: string | null;
  status: EmailIngestionStatus;
  processingResult: EmailProcessingResult | null;
  errorMessage: string | null;
  retryCount: number;
  createdAt: string;
  updatedAt: string;
}

export interface PendingVehicleAssociation {
  id: string;
  userId: string;
  recordType: EmailRecordType;
  extractedData: ExtractedReceiptData;
  documentId: string | null;
  status: PendingAssociationStatus;
  createdAt: string;
  resolvedAt: string | null;
}

// ========================
// Processing Results
// ========================

export interface EmailProcessingResult {
  recordType: EmailRecordType;
  vehicleId: string | null;
  recordId: string | null;
  documentId: string | null;
  pendingAssociationId: string | null;
  extractedData: ExtractedReceiptData;
}

export interface ExtractedReceiptData {
  vendor: string | null;
  date: string | null;
  total: number | null;
  odometerReading: number | null;
  /** Fuel-specific fields */
  gallons: number | null;
  pricePerGallon: number | null;
  fuelType: string | null;
  /** Maintenance-specific fields */
  category: string | null;
  subtypes: string[] | null;
  shopName: string | null;
  description: string | null;
}

// ========================
// Resend Webhook Payloads
// ========================

/** Top-level Resend webhook event envelope */
export interface ResendWebhookEvent {
  type: string;
  created_at: string;
  data: ResendWebhookEventData;
}

/** Resend email.received webhook event data */
export interface ResendWebhookEventData {
  email_id: string;
  from: string;
  to: string[];
  subject: string;
  text: string | null;
  html: string | null;
  created_at: string;
  attachments: ResendEmailAttachment[];
}

/** Attachment metadata from Resend inbound email */
export interface ResendEmailAttachment {
  filename: string;
  content_type: string;
  content: string;
}
