import 'package:flutter_email_sender/flutter_email_sender.dart'; import 'package:intl/intl.dart'; import '../models/work_order.dart'; import 'pdf_service.dart'; class EmailService { Future sendReport({ required WorkOrder order, required String recipient, required String pdfPath, required PdfReportType reportType, }) async { final priceFormat = NumberFormat.decimalPattern('es_CL'); String formatPrice(int value) => '\$${priceFormat.format(value)}'; final buffer = StringBuffer() ..writeln('Orden de trabajo: ${order.title}') ..writeln('Cliente: ${order.details.client}') ..writeln('Dirección: ${order.details.address}') ..writeln() ..writeln('Resumen del trabajo realizado:'); for (var index = 0; index < order.tasks.length; index++) { final task = order.tasks[index]; buffer ..writeln() ..writeln('${index + 1}. ${task.title}') ..writeln('Observación: ${task.observation}') ..writeln( 'Evidencia: ${task.beforePhotos.length} foto(s) antes y ' '${task.afterPhotos.length} foto(s) después.', ); if (reportType == PdfReportType.client && task.parts.isNotEmpty) { buffer.writeln('Repuestos utilizados:'); for (final part in task.parts) { buffer.writeln('- ${part.name}: ${formatPrice(part.price)}'); } buffer.writeln( 'Subtotal de repuestos: ${formatPrice(task.partsTotal)}', ); } } if (reportType == PdfReportType.client) { buffer ..writeln() ..writeln('Costo de mano de obra: ${formatPrice(order.laborCost)}') ..writeln('Total de repuestos: ${formatPrice(order.totalPartsCost)}') ..writeln('Valor total de la OT: ${formatPrice(order.totalCost)}'); } buffer ..writeln() ..writeln('El informe PDF adjunto contiene el detalle y las fotografías.') ..writeln() ..writeln('Enviado desde SMoya Gasfiter.'); final email = Email( body: buffer.toString(), subject: 'Informe para ${reportType.label.toLowerCase()}: ${order.title}', recipients: [recipient], attachmentPaths: [pdfPath], isHTML: false, ); await FlutterEmailSender.send(email); } }