import express from "express";
import SubmitAiWebsite from "../models/submitAiWebsite.js";
import { authenticate } from "../middleware/authMiddleware.js";
import { verifyPayPalOrder } from "../middleware/paypalVerification.js";
import { sendAIWebsiteSubmissionEmail } from "../utils/emailService.js";
import { User } from "../models/user.js";
import { uploadToDropbox, generateUniqueFilename, getFileExtensionFromBase64 } from "../utils/dropboxService.js";
import { getPrice } from "../config/pricing.js";

const router = express.Router();

// ✅ PHASE 1: Create pending AI website submission (no payment required yet)
router.post("/create", authenticate, async (req, res) => {
  try {
    const userId = req.user.id;

    console.log('📝 AI Website submission (PENDING) request received');

    const {
      websiteUrl,
      screenshot,
      title,
      description,
      email,
      contactNumber,
      category,
      pricingOption,
    } = req.body;

    // Check if the website URL already exists
    const existingURL = await SubmitAiWebsite.findOne({ websiteUrl });
    if (existingURL) {
      console.log(`❌ Website URL already exists: ${websiteUrl}`);
      return res.status(409).json({
        ok: false,
        error: "URL_ALREADY_EXISTS",
        message: "An AI website with this URL has already been submitted"
      });
    }

    // ✅ Upload screenshot to Dropbox
    let screenshotUrl;
    if (screenshot) {
      const extension = getFileExtensionFromBase64(screenshot);
      const filename = generateUniqueFilename(`${title}-screenshot.${extension}`, 'ai-website');
      screenshotUrl = await uploadToDropbox(screenshot, filename, '/ai-website-submissions');
      console.log('✅ Screenshot uploaded to Dropbox:', screenshotUrl);
    } else {
      return res.status(400).json({
        ok: false,
        error: "VALIDATION_ERROR",
        message: "Screenshot is required."
      });
    }

    // Create the payload with PENDING status - no payment info yet
    const payload = {
      submittedBy: userId,
      websiteUrl,
      screenshot: screenshotUrl,
      title,
      description,
      email,
      contactNumber,
      category: Array.isArray(category) ? category : [category],
      pricingOption: Array.isArray(pricingOption) ? pricingOption : [pricingOption],
      status: 'pending_payment',
      // ✅ Generate temporary unique paymentOrderID to avoid null duplicate key errors
      paymentOrderID: `PENDING_AI_${Date.now()}_${userId}_${Math.random().toString(36).substr(2, 9)}`,
    };

    // Save the pending AI website submission to the database
    const doc = await SubmitAiWebsite.create(payload);

    console.log("✅ Pending AI website submission created: " + doc._id);

    // Get the price for this service
    const servicePrice = getPrice('aiWebsite');

    return res.status(201).json({
      ok: true,
      data: doc,
      price: servicePrice,
      message: "Submission created successfully. Please complete payment to publish."
    });
  } catch (err) {
    console.error("❌ Pending AI website submission error:", err);

    if (err?.code === 11000) {
      const duplicateField = Object.keys(err.keyValue)[0];
      let message = "A submission with this data already exists.";

      if (duplicateField === 'websiteUrl') {
        message = "This website URL has already been submitted.";
      }

      return res.status(409).json({
        ok: false,
        error: "DUPLICATE_KEY",
        message: message,
        field: duplicateField,
        value: err.keyValue[duplicateField],
      });
    }
    return res.status(500).json({ ok: false, error: "SERVER_ERROR", message: err.message });
  }
});

// ✅ PHASE 2: Confirm payment and publish AI website submission
router.post("/confirm-payment", authenticate, async (req, res) => {
  try {
    const userId = req.user.id;

    console.log('💳 AI Website payment confirmation request received');

    const {
      submissionId,
      paymentOrderID,
      paymentAmount,
      couponCode,
      isFreeSubmission,
    } = req.body;

    if (!submissionId) {
      return res.status(400).json({
        ok: false,
        error: "SUBMISSION_ID_REQUIRED",
        message: "Submission ID is required"
      });
    }

    // Find the pending submission
    const submission = await SubmitAiWebsite.findById(submissionId);

    if (!submission) {
      return res.status(404).json({
        ok: false,
        error: "SUBMISSION_NOT_FOUND",
        message: "Submission not found"
      });
    }

    // Verify the submission belongs to the current user
    if (submission.submittedBy.toString() !== userId.toString()) {
      return res.status(403).json({
        ok: false,
        error: "UNAUTHORIZED",
        message: "You are not authorized to confirm payment for this submission"
      });
    }

    // Check if submission is already published
    if (submission.status === 'published') {
      return res.status(400).json({
        ok: false,
        error: "ALREADY_PUBLISHED",
        message: "This submission has already been published"
      });
    }

    // ✅ CRITICAL: Check if this is a free submission (price = 0 due to coupon)
    const isFree = isFreeSubmission === true && paymentAmount === 0;

    let order = null;

    if (isFree) {
      // For free submissions, verify coupon instead of payment
      if (!couponCode) {
        return res.status(400).json({
          ok: false,
          error: "COUPON_REQUIRED",
          message: "Coupon code is required for free submission"
        });
      }

      console.log('🎟️ Processing free AI website submission with coupon:', couponCode);

      // Update submission with coupon info
      submission.paymentOrderID = `FREE_AI_${Date.now()}_${userId}`;
      submission.paymentStatus = 'FREE_COUPON';
      submission.paymentAmount = 0;
      submission.paymentCurrency = 'USD';
      submission.paymentDate = new Date();
      submission.paymentVerified = true;
      submission.couponCode = couponCode;
      submission.isFreeSubmission = true;
      submission.status = 'published';

    } else {
      // Regular paid submission - verify PayPal payment
      if (!paymentOrderID) {
        submission.status = 'payment_failed';
        await submission.save();

        return res.status(400).json({
          ok: false,
          error: "PAYMENT_REQUIRED",
          message: "Payment order ID is required for submission",
          canRetry: true,
          submissionId: submission._id
        });
      }

      // Check if this payment has already been used
      const existingPayment = await SubmitAiWebsite.findOne({
        paymentOrderID,
        _id: { $ne: submissionId }
      });
      if (existingPayment) {
        console.log(`Payment ID already exists: ${paymentOrderID}`);
        return res.status(409).json({
          ok: false,
          error: "PAYMENT_ALREADY_USED",
          message: "This payment has already been used for another submission"
        });
      }

      // Verify payment with PayPal
      console.log('🔍 Verifying PayPal payment:', paymentOrderID);
      const verificationResult = await verifyPayPalOrder(paymentOrderID);

      if (!verificationResult.success) {
        submission.status = 'payment_failed';
        await submission.save();

        return res.status(400).json({
          ok: false,
          error: "PAYMENT_VERIFICATION_FAILED",
          message: "Payment verification failed. Please try again.",
          canRetry: true,
          submissionId: submission._id
        });
      }

      order = verificationResult.order;

      // Check if payment is completed or approved
      if (order.status !== 'COMPLETED' && order.status !== 'APPROVED') {
        submission.status = 'payment_failed';
        await submission.save();

        return res.status(400).json({
          ok: false,
          error: "PAYMENT_NOT_COMPLETED",
          message: `Payment status is ${order.status}. Expected COMPLETED or APPROVED.`,
          canRetry: true,
          submissionId: submission._id
        });
      }

      // Update submission with payment info
      submission.paymentOrderID = order.id;
      submission.paymentStatus = order.status;
      submission.paymentAmount = parseFloat(order.amount);
      submission.paymentCurrency = order.currency;
      submission.payerEmail = order.payerEmail;
      submission.payerName = order.payerName;
      submission.paymentDate = new Date();
      submission.paymentVerified = true;
      submission.isFreeSubmission = false;
      submission.status = 'published';
    }

    // Save the updated submission
    await submission.save();

    console.log(isFree
      ? "✅ Free AI website submission published with coupon: " + couponCode
      : "✅ AI website submission published with verified payment: " + submission._id
    );

    // Apply coupon if this is a free submission
    if (isFree && couponCode) {
      try {
        const response = await fetch(`${process.env.API_Dep_URL || 'http://localhost:5000'}/api/coupons/apply`, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': req.headers.authorization,
          },
          body: JSON.stringify({
            couponCode: couponCode,
            serviceType: 'aiWebsite',
          }),
        });

        if (response.ok) {
          console.log('✅ Coupon applied successfully:', couponCode);
        } else {
          console.warn('⚠️ Failed to apply coupon, but submission completed:', couponCode);
        }
      } catch (couponError) {
        console.error('❌ Error applying coupon:', couponError.message);
      }
    }

    // Get user information for email
    const user = await User.findById(userId);

    // Send confirmation email
    if (user && user.email) {
      console.log("📧 Attempting to send confirmation email to:", user.email);
      try {
        await sendAIWebsiteSubmissionEmail(user.email, {
          userName: user.userName || user.name || "User",
          websiteTitle: submission.title,
          websiteUrl: submission.websiteUrl,
          category: submission.category.join(', '),
          pricingOption: submission.pricingOption.join(', '),
          submissionDate: new Date(),
          paymentOrderID: isFree ? 'FREE (Coupon Applied)' : order?.id,
          paymentAmount: isFree ? '0.00' : order?.amount,
          paymentCurrency: 'USD',
        });
        console.log("✅ Confirmation email sent successfully");
      } catch (emailError) {
        console.error("❌ Failed to send confirmation email:", emailError.message);
      }
    }

    return res.status(200).json({
      ok: true,
      data: submission,
      message: "Payment confirmed and AI website submission published successfully!"
    });
  } catch (err) {
    console.error("❌ AI website payment confirmation error:", err);
    return res.status(500).json({ ok: false, error: "SERVER_ERROR", message: err.message });
  }
});

// Get all AI websites - only published
router.get("/ai-websites", async (req, res) => {
  try {
    const { category, pricingOption, limit, page } = req.query;

    const filter = {
      // ✅ Show published submissions OR old submissions without status field
      $or: [
        { status: 'published' },
        { status: { $exists: false } } // Old submissions before status field was added
      ]
    };

    if (category) filter.category = category;
    if (pricingOption) filter.pricingOption = pricingOption;

    const limitNum = parseInt(limit) || 0;
    const pageNum = parseInt(page) || 1;
    const skip = limitNum > 0 ? (pageNum - 1) * limitNum : 0;

    let query = SubmitAiWebsite.find(filter)
      .populate("submittedBy", "userName email profilePhoto")
      .sort({ createdAt: -1 })
      .lean();

    if (limitNum > 0) {
      query = query.limit(limitNum).skip(skip);
    }

    const aiWebsites = await query;
    const total = limitNum > 0 ? await SubmitAiWebsite.countDocuments(filter) : aiWebsites.length;

    res.status(200).json({
      data: aiWebsites,
      pagination: limitNum > 0 ? {
        total,
        page: pageNum,
        limit: limitNum,
        pages: Math.ceil(total / limitNum)
      } : null
    });
  } catch (err) {
    res.status(500).json({ message: "Error fetching AI websites", error: err.message });
  }
});

// Get a single AI website by ID
router.get("/:id", async (req, res) => {
  try {
    const { id } = req.params;

    const aiWebsite = await SubmitAiWebsite.findById(id)
      .populate("submittedBy", "userName email profilePhoto")
      .lean();

    if (!aiWebsite) {
      return res.status(404).json({
        ok: false,
        message: "AI website not found",
      });
    }

    res.status(200).json({
      ok: true,
      website: aiWebsite,
    });
  } catch (err) {
    res.status(500).json({
      ok: false,
      message: "Error fetching AI website",
      error: err.message,
    });
  }
});

export default router;
