import { Injectable, Post, Body } from '@nestjs/common';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class EmailService {
  constructor(private mailerService: MailerService) {}

  async sendWelcomeEmail(email: string, name: string) {
    await this.mailerService.sendMail({
      to: email,
      subject: 'Welcome to IMK Platform',
      html: `
      <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
        <h1 style="color: #2563eb;">Welcome to IMK Platform!</h1>
        <p>Dear ${name},</p>
        <p>Thank you for joining IMK Platform. Your account has been created successfully.</p>
        <p>You can now log in to access your documents and start collaborating with your team.</p>
        <p>If you have any questions or need assistance, please don't hesitate to contact our support team.</p>
        <div style="margin: 30px 0;">
          <a href="${process.env.FRONTEND_URL}/login" 
             style="background-color: #2563eb; color: white; padding: 12px 24px; text-decoration: none; border-radius: 4px;">
            Login to Your Account
          </a>
        </div>
        <p>Best regards,<br>IMK Team</p>
      </div>
    `,
    });
  }

  async sendPasswordResetEmail(email: string, resetToken: string) {
    const resetLink = `${process.env.FRONTEND_URL}/reset-password?token=${resetToken}`;
    await this.mailerService.sendMail({
      to: email,
      subject: 'Password Reset Request',
      html: `
        <h1>Password Reset</h1>
        <p>Click the link below to reset your password:</p>
        <a href="${resetLink}">Reset Password</a>
        <p>This link will expire in 1 hour.</p>
      `,
    });
  }

  async sendDocumentSharedNotification(email: string, documentTitle: string, sharedByName: string) {
    await this.mailerService.sendMail({
      to: email,
      subject: 'New Document Shared With You',
      html: `
        <h1>New Document Available</h1>
        <p>${sharedByName} has shared a document with you: "${documentTitle}"</p>
        <p>Login to your account to view the document.</p>
      `,
    });
  }
  async sendContactEmail(name: string, email: string, message: string) {
    // Send to admin
    await this.mailerService.sendMail({
      to: process.env.ADMIN_EMAIL,
      subject: `New Contact Form Submission from ${name}`,
      html: `
        <h2>New Contact Form Submission</h2>
        <p><strong>From:</strong> ${name}</p>
        <p><strong>Email:</strong> ${email}</p>
        <p><strong>Message:</strong></p>
        <p>${message}</p>
      `,
    });
    await this.mailerService.sendMail({
        to: email,
        subject: 'Thank you for contacting us',
        html: `
          <h2>Thank you for contacting us</h2>
          <p>Dear ${name},</p>
          <p>We have received your message and will get back to you soon.</p>
          <p>Your message:</p>
          <blockquote>${message}</blockquote>
          <p>Best regards,<br>IMK Team</p>
        `,
      });
  }
}
