// Marketplace email notifications
// Uses existing SMTP environment variables

export interface MarketplaceEmailPayload {
  to: string;
  subject: string;
  html: string;
}

async function sendEmail(payload: MarketplaceEmailPayload): Promise<void> {
  const res = await fetch('/api/marketplace/send-email', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(payload),
  });
  if (!res.ok) {
    const err = await res.json().catch(() => ({}));
    throw new Error(err.error || 'Failed to send email');
  }
}

export async function sendProfileApprovedEmail(to: string, name: string): Promise<void> {
  await sendEmail({
    to,
    subject: 'Your AV Beat Marketplace Profile is Approved!',
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; background: #0d0d0d; color: #fff; padding: 32px; border-radius: 8px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <h1 style="color: #CC0000; font-size: 24px; margin: 0;">BROADCAST BEAT</h1>
          <p style="color: #999; font-size: 12px; margin: 4px 0 0;">Production Crew & Vendor Marketplace</p>
        </div>
        <h2 style="color: #fff; font-size: 20px;">Welcome to the Marketplace, ${name}!</h2>
        <p style="color: #ccc; line-height: 1.6;">
          Your profile has been reviewed and approved by our team. You are now live in the 
          AV Beat Production Marketplace and visible to clients, employers, and production companies.
        </p>
        <div style="background: #1a1a1a; border-left: 4px solid #CC0000; padding: 16px; margin: 24px 0; border-radius: 4px;">
          <p style="color: #fff; margin: 0; font-weight: bold;">Next Steps:</p>
          <ul style="color: #ccc; margin: 8px 0 0; padding-left: 20px; line-height: 1.8;">
            <li>Complete your profile with a reel, credits, and gear list</li>
            <li>Upgrade to Pro for priority placement and unlimited gig responses</li>
            <li>Browse open gig requests in your specialty</li>
          </ul>
        </div>
        <a href="${process.env.NEXT_PUBLIC_SITE_URL}/marketplace" 
           style="display: inline-block; background: #CC0000; color: #fff; padding: 12px 24px; border-radius: 4px; text-decoration: none; font-weight: bold;">
          View Your Profile
        </a>
        <p style="color: #666; font-size: 12px; margin-top: 32px;">
          AV Beat — Official NAB Show Media Partner<br>
          <a href="${process.env.NEXT_PUBLIC_SITE_URL}" style="color: #CC0000;">avbeat.com</a>
        </p>
      </div>
    `,
  });
}

export async function sendProfileRejectedEmail(
  to: string,
  name: string,
  reason: string
): Promise<void> {
  await sendEmail({
    to,
    subject: 'Update Required: Your AV Beat Marketplace Profile',
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; background: #0d0d0d; color: #fff; padding: 32px; border-radius: 8px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <h1 style="color: #CC0000; font-size: 24px; margin: 0;">BROADCAST BEAT</h1>
          <p style="color: #999; font-size: 12px; margin: 4px 0 0;">Production Crew & Vendor Marketplace</p>
        </div>
        <h2 style="color: #fff; font-size: 20px;">Profile Review — Action Required</h2>
        <p style="color: #ccc; line-height: 1.6;">
          Hi ${name}, our team has reviewed your marketplace profile and we need a few updates before it can go live.
        </p>
        <div style="background: #1a1a1a; border-left: 4px solid #f59e0b; padding: 16px; margin: 24px 0; border-radius: 4px;">
          <p style="color: #f59e0b; margin: 0 0 8px; font-weight: bold;">Feedback from our team:</p>
          <p style="color: #ccc; margin: 0; line-height: 1.6;">${reason}</p>
        </div>
        <p style="color: #ccc; line-height: 1.6;">
          Please log in to your account, update your profile, and resubmit for review. 
          Our team typically reviews profiles within 24 hours.
        </p>
        <a href="${process.env.NEXT_PUBLIC_SITE_URL}/marketplace/account" 
           style="display: inline-block; background: #CC0000; color: #fff; padding: 12px 24px; border-radius: 4px; text-decoration: none; font-weight: bold;">
          Update My Profile
        </a>
        <p style="color: #666; font-size: 12px; margin-top: 32px;">
          Questions? Reply to this email or contact us at <a href="mailto:marketplace@avbeat.com" style="color: #CC0000;">marketplace@avbeat.com</a>
        </p>
      </div>
    `,
  });
}

export async function sendListingConfirmationEmail(
  to: string,
  name: string,
  listingType: 'job' | 'gig',
  title: string
): Promise<void> {
  await sendEmail({
    to,
    subject: `Your ${listingType === 'job' ? 'Job Post' : 'Gig Request'} is Live — ${title}`,
    html: `
      <div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; background: #0d0d0d; color: #fff; padding: 32px; border-radius: 8px;">
        <div style="text-align: center; margin-bottom: 24px;">
          <h1 style="color: #CC0000; font-size: 24px; margin: 0;">BROADCAST BEAT</h1>
          <p style="color: #999; font-size: 12px; margin: 4px 0 0;">Production Crew & Vendor Marketplace</p>
        </div>
        <h2 style="color: #fff; font-size: 20px;">
          Your ${listingType === 'job' ? 'Job Post' : 'Gig Request'} is Live!
        </h2>
        <p style="color: #ccc; line-height: 1.6;">
          <strong style="color: #fff;">"${title}"</strong> has been posted to the AV Beat Marketplace 
          and is now visible to our network of broadcast and production professionals.
        </p>
        <a href="${process.env.NEXT_PUBLIC_SITE_URL}/marketplace/${listingType === 'job' ? 'jobs' : 'gigs'}" 
           style="display: inline-block; background: #CC0000; color: #fff; padding: 12px 24px; border-radius: 4px; text-decoration: none; font-weight: bold;">
          View ${listingType === 'job' ? 'Job Board' : 'Gig Board'}
        </a>
        <p style="color: #666; font-size: 12px; margin-top: 32px;">
          AV Beat — Official NAB Show Media Partner
        </p>
      </div>
    `,
  });
}
