/**
 * Default Sales Side Effects Port Adapter
 * Handles notifications, emails, and other side effects
 * Implementations depend on notification system availability
 */

import {
  SalesSideEffectsPort,
  SalesNotificationContext,
} from '../application/sales-side-effects.port';
import { SalesInvoice } from '../domain/sales-invoice.model';

export class DefaultSalesSideEffectsPort implements SalesSideEffectsPort {
  /**
   * Notify when invoice is created
   */
  async onInvoiceCreated(context: SalesNotificationContext): Promise<void> {
    try {
      console.log(`[Sales] Invoice created: ${context.invoiceNumber}`);
      // TODO: Send email to customer if email available
      // TODO: Push notification to dashboard
      // TODO: Log to audit trail
    } catch (error) {
      console.error('Failed to handle onInvoiceCreated:', error);
      // Non-blocking
    }
  }

  /**
   * Notify when invoice is shipped
   */
  async onInvoiceShipped(context: SalesNotificationContext): Promise<void> {
    try {
      console.log(`[Sales] Invoice shipped: ${context.invoiceNumber}`);
      // TODO: Send shipping notification email
      // TODO: Update status in dashboard
    } catch (error) {
      console.error('Failed to handle onInvoiceShipped:', error);
    }
  }

  /**
   * Notify when payment is received
   */
  async onPaymentReceived(
    context: SalesNotificationContext & { amountReceived: number }
  ): Promise<void> {
    try {
      console.log(
        `[Sales] Payment received for ${context.invoiceNumber}: ${context.amountReceived}`
      );
      // TODO: Send payment confirmation
      // TODO: Update balance display
    } catch (error) {
      console.error('Failed to handle onPaymentReceived:', error);
    }
  }

  /**
   * Notify when invoice is overdue
   */
  async onInvoiceOverdue(
    context: SalesNotificationContext & { daysOverdue: number }
  ): Promise<void> {
    try {
      console.log(
        `[Sales] Invoice overdue: ${context.invoiceNumber} (${context.daysOverdue} days)`
      );
      // TODO: Send overdue payment reminder email
      // TODO: Mark in system for follow-up
    } catch (error) {
      console.error('Failed to handle onInvoiceOverdue:', error);
    }
  }

  /**
   * Notify when invoice is cancelled
   */
  async onInvoiceCancelled(
    context: SalesNotificationContext & { reason: string }
  ): Promise<void> {
    try {
      console.log(
        `[Sales] Invoice cancelled: ${context.invoiceNumber} - Reason: ${context.reason}`
      );
      // TODO: Send cancellation notification
      // TODO: Update audit trail
    } catch (error) {
      console.error('Failed to handle onInvoiceCancelled:', error);
    }
  }

  /**
   * Generate PDF for invoice (optional)
   */
  async generateInvoicePDF?(invoice: SalesInvoice): Promise<Buffer | null> {
    try {
      console.log(`[Sales] Generating PDF for invoice: ${invoice.invoiceNumber}`);
      // TODO: Implement PDF generation using puppeteer or similar
      return null;
    } catch (error) {
      console.error('Failed to generate PDF:', error);
      return null;
    }
  }
}
