const db = require("./config/database");

async function addCustomerServicePages() {
  try {
    console.log("Adding customer service pages to database...\n");

    // Helper function to insert or update page
    async function upsertPage(slug, title, html, metatitle, metadescription) {
      const existing = await db.query("SELECT id FROM pages WHERE slug = $1", [
        slug,
      ]);

      if (existing.rows.length > 0) {
        await db.query(
          `
          UPDATE pages 
          SET pagecontent = $1, content = $1, title = $2, metatitle = $3, metadescription = $4, updatedat = NOW()
          WHERE slug = $5
        `,
          [html, title, metatitle, metadescription, slug]
        );
        console.log(`✓ ${title} page updated`);
      } else {
        await db.query(
          `
          INSERT INTO pages (id, slug, title, content, pagecontent, metatitle, metadescription, ispublished, isactive, createdat, updatedat)
          VALUES (gen_random_uuid()::text, $1, $2, $3, $3, $4, $5, true, true, NOW(), NOW())
        `,
          [slug, title, html, metatitle, metadescription]
        );
        console.log(`✓ ${title} page added`);
      }
    }

    // 1. Shipping Info Page
    const shippingHTML = `
      <div style="max-width: 1000px; margin: 0 auto; padding: 40px 20px;">
        <div style="text-align: center; margin-bottom: 60px;">
          <h2 style="font-size: 2.5rem; font-weight: 700; color: #202023; margin-bottom: 16px;">
            Shipping Information
          </h2>
          <p style="font-size: 1.1rem; color: #202023; opacity: 0.7; max-width: 700px; margin: 0 auto;">
            Everything you need to know about our shipping policies and delivery
          </p>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-truck" style="font-size: 1.5rem;"></i> Shipping Methods
          </h3>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;"><strong>Standard Shipping:</strong> 5-7 business days - $5.99</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;"><strong>Express Shipping:</strong> 2-3 business days - $12.99</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;"><strong>Priority Overnight:</strong> 1 business day - $24.99</li>
            <li style="padding: 12px 0;"><strong>Free Shipping:</strong> Orders over $50 (Standard shipping)</li>
          </ul>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-geo-alt" style="font-size: 1.5rem;"></i> Delivery Areas
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            We currently ship to all 50 states in the United States. International shipping is available to Canada, UK, and Australia. Additional fees may apply for international orders.
          </p>
        </div>

        <div>
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-clock" style="font-size: 1.5rem;"></i> Processing Time
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Orders are typically processed within 1-2 business days. You will receive a tracking number via email once your order ships. Custom or personalized items may require additional processing time (3-5 business days).
          </p>
        </div>
      </div>
    `;

    await upsertPage(
      "shipping-info",
      "Shipping Info",
      shippingHTML,
      "Shipping Information - Sky Art Shop",
      "Learn about our shipping methods, delivery times, and policies."
    );

    // 2. Returns Page
    const returnsHTML = `
      <div style="max-width: 1000px; margin: 0 auto; padding: 40px 20px;">
        <div style="text-align: center; margin-bottom: 60px;">
          <h2 style="font-size: 2.5rem; font-weight: 700; color: #202023; margin-bottom: 16px;">
            Returns & Refunds
          </h2>
          <p style="font-size: 1.1rem; color: #202023; opacity: 0.7; max-width: 700px; margin: 0 auto;">
            Our hassle-free return policy to ensure your satisfaction
          </p>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-arrow-counterclockwise" style="font-size: 1.5rem;"></i> Return Policy
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 20px; font-size: 1.05rem;">
            We accept returns within <strong>30 days</strong> of purchase. Items must be in original condition, unused, and in original packaging.
          </p>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">✓ Item must be unused and in original condition</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">✓ Original packaging must be intact</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">✓ Include receipt or proof of purchase</li>
            <li style="padding: 12px 0;">✓ Custom or personalized items cannot be returned</li>
          </ul>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-cash-coin" style="font-size: 1.5rem;"></i> Refund Process
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Once we receive your return, we will inspect the item and process your refund within 5-7 business days. Refunds will be issued to the original payment method. Shipping costs are non-refundable unless the return is due to our error.
          </p>
        </div>

        <div>
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px; display: flex; align-items: center; gap: 12px;">
            <i class="bi bi-box-seam" style="font-size: 1.5rem;"></i> How to Return
          </h3>
          <ol style="line-height: 2; color: #202023; font-size: 1.05rem; padding-left: 20px;">
            <li style="padding: 8px 0;">Contact us at <a href="mailto:returns@skyartshop.com" style="color: #FCB1D8; text-decoration: none; font-weight: 500;">returns@skyartshop.com</a> to initiate a return</li>
            <li style="padding: 8px 0;">Pack the item securely in original packaging</li>
            <li style="padding: 8px 0;">Ship to the address provided in our return confirmation email</li>
            <li style="padding: 8px 0;">We recommend using a trackable shipping method</li>
          </ol>
        </div>
      </div>
    `;

    await upsertPage(
      "returns",
      "Returns",
      returnsHTML,
      "Returns & Refunds - Sky Art Shop",
      "Our return policy and refund process explained."
    );

    // 3. FAQ Page
    const faqHTML = `
      <div style="max-width: 1000px; margin: 0 auto; padding: 40px 20px;">
        <div style="text-align: center; margin-bottom: 60px;">
          <h2 style="font-size: 2.5rem; font-weight: 700; color: #202023; margin-bottom: 16px;">
            Frequently Asked Questions
          </h2>
          <p style="font-size: 1.1rem; color: #202023; opacity: 0.7; max-width: 700px; margin: 0 auto;">
            Find answers to common questions about our products and services
          </p>
        </div>

        <div style="margin-bottom: 40px; padding-bottom: 40px; border-bottom: 2px solid #FFD0D0;">
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> How do I place an order?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Simply browse our shop, add items to your cart, and proceed to checkout. You can pay securely with credit card, debit card, or PayPal.
          </p>
        </div>

        <div style="margin-bottom: 40px; padding-bottom: 40px; border-bottom: 2px solid #FFD0D0;">
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> Do you offer custom artwork?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Yes! We offer custom commissions for paintings and artwork. Contact us with your vision and we'll provide a quote and timeline.
          </p>
        </div>

        <div style="margin-bottom: 40px; padding-bottom: 40px; border-bottom: 2px solid #FFD0D0;">
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> How long does shipping take?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Standard shipping takes 5-7 business days. Express shipping (2-3 days) and overnight options are available. Processing time is 1-2 business days.
          </p>
        </div>

        <div style="margin-bottom: 40px; padding-bottom: 40px; border-bottom: 2px solid #FFD0D0;">
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> What payment methods do you accept?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            We accept all major credit cards (Visa, Mastercard, American Express, Discover), debit cards, and PayPal.
          </p>
        </div>

        <div style="margin-bottom: 40px; padding-bottom: 40px; border-bottom: 2px solid #FFD0D0;">
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> Can I cancel or modify my order?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            You can cancel or modify your order within 24 hours of placing it. Contact us immediately at <a href="mailto:contact@skyartshop.com" style="color: #FCB1D8; text-decoration: none; font-weight: 500;">contact@skyartshop.com</a>.
          </p>
        </div>

        <div>
          <h3 style="font-size: 1.4rem; font-weight: 600; color: #FCB1D8; margin-bottom: 16px; display: flex; align-items: center; gap: 10px;">
            <i class="bi bi-question-circle" style="font-size: 1.3rem;"></i> Do you ship internationally?
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            Yes, we ship to Canada, UK, and Australia. International shipping costs vary by location and are calculated at checkout.
          </p>
        </div>
      </div>
    `;

    await upsertPage(
      "faq",
      "FAQ",
      faqHTML,
      "Frequently Asked Questions - Sky Art Shop",
      "Answers to common questions about orders, shipping, and our services."
    );

    // 4. Privacy Policy Page
    const privacyHTML = `
      <div style="max-width: 1000px; margin: 0 auto; padding: 40px 20px;">
        <div style="text-align: center; margin-bottom: 60px;">
          <h2 style="font-size: 2.5rem; font-weight: 700; color: #202023; margin-bottom: 16px;">
            Privacy Policy
          </h2>
          <p style="font-size: 1.1rem; color: #202023; opacity: 0.7; max-width: 700px; margin: 0 auto;">
            How we collect, use, and protect your information
          </p>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            Information We Collect
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 16px; font-size: 1.05rem;">
            We collect information you provide directly to us, including:
          </p>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Name, email address, and contact information</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Billing and shipping addresses</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Payment information (processed securely)</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Order history and preferences</li>
            <li style="padding: 12px 0;">• Communications with our customer service</li>
          </ul>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            How We Use Your Information
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 16px; font-size: 1.05rem;">
            We use the information we collect to:
          </p>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Process and fulfill your orders</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Communicate with you about your orders</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Send promotional emails (with your consent)</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Improve our website and services</li>
            <li style="padding: 12px 0;">• Prevent fraud and enhance security</li>
          </ul>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            Information Sharing
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 16px; font-size: 1.05rem;">
            We do not sell your personal information. We may share your information with:
          </p>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Service providers who help us operate our business</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Payment processors for secure transactions</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Shipping companies to deliver your orders</li>
            <li style="padding: 12px 0;">• Law enforcement when required by law</li>
          </ul>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            Data Security
          </h3>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            We implement appropriate security measures to protect your personal information. All payment information is encrypted using SSL technology. However, no method of transmission over the internet is 100% secure.
          </p>
        </div>

        <div style="margin-bottom: 50px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            Your Rights
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 16px; font-size: 1.05rem;">
            You have the right to:
          </p>
          <ul style="line-height: 2; color: #202023; font-size: 1.05rem; list-style: none; padding: 0;">
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Access your personal information</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Correct inaccurate information</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Request deletion of your data</li>
            <li style="padding: 12px 0; border-bottom: 1px solid #FFD0D0;">• Opt-out of marketing communications</li>
            <li style="padding: 12px 0;">• Lodge a complaint with a supervisory authority</li>
          </ul>
        </div>

        <div style="padding: 30px; background: #FFEBEB; border-left: 4px solid #FCB1D8; border-radius: 8px;">
          <h3 style="font-size: 1.75rem; font-weight: 600; color: #FCB1D8; margin-bottom: 20px;">
            Contact Us
          </h3>
          <p style="line-height: 1.8; color: #202023; margin-bottom: 16px; font-size: 1.05rem;">
            If you have questions about this Privacy Policy, please contact us at:
          </p>
          <p style="line-height: 1.8; color: #202023; font-size: 1.05rem;">
            <strong>Email:</strong> <a href="mailto:privacy@skyartshop.com" style="color: #FCB1D8; text-decoration: none; font-weight: 500;">privacy@skyartshop.com</a><br>
            <strong>Phone:</strong> +1 (555) 123-4567<br>
            <strong>Last Updated:</strong> January 1, 2026
          </p>
        </div>
      </div>
    `;

    await upsertPage(
      "privacy",
      "Privacy Policy",
      privacyHTML,
      "Privacy Policy - Sky Art Shop",
      "Our privacy policy and how we protect your information."
    );

    console.log("\n✅ All customer service pages added successfully!");
    console.log("\nPages available at:");
    console.log("  - http://localhost:5000/shipping-info.html");
    console.log("  - http://localhost:5000/returns.html");
    console.log("  - http://localhost:5000/faq.html");
    console.log("  - http://localhost:5000/privacy.html");
    console.log(
      "\nThese pages are now editable in the admin panel under Custom Pages!"
    );

    process.exit(0);
  } catch (error) {
    console.error("Error adding customer service pages:", error);
    process.exit(1);
  }
}

addCustomerServicePages();
