const fs = require("fs");
const path = require("path");

const productsPath = path.join(__dirname, "../data/products.json");
const db = JSON.parse(fs.readFileSync(productsPath, "utf8"));

// Default content for tabs (copy matches current UI text style)
const defaultLong = [
  "Embodying the raw, wayward spirit of rock ‘n’ roll, the Kilburn portable active stereo speaker takes the unmistakable look and sound of Marshall, unplugs the chords, and takes the show on the road.",
  "Weighing in under 7 pounds, the Kilburn is a lightweight piece of vintage styled engineering. Setting the bar as one of the loudest speakers in its class, the Kilburn is a compact, stout-hearted hero with a well-balanced audio which boasts a clear midrange and extended highs for a sound that is both articulate and pronounced. The analogue knobs allow you to fine tune the controls to your personal preferences while the guitar-influenced leather strap enables easy and stylish travel.",
];

const defaultsByCategory = {
  seating: {
    additionalInformation: {
      Material: "Fabric, engineered wood base",
      Upholstery: "Performance fabric",
      Dimensions: "See size options",
      Warranty: "2 years",
    },
  },
  tables: {
    additionalInformation: {
      Material: "Engineered wood, steel frame",
      Finish: "Matte laminate",
      Dimensions: "Small/Medium/Large",
      Warranty: "2 years",
    },
  },
  storage: {
    additionalInformation: {
      Material: "Powder-coated steel",
      Capacity: "Modular shelves",
      Dimensions: "Standard/Large/XL",
      Warranty: "2 years",
    },
  },
  workspace: {
    additionalInformation: {
      Material: "Acoustic panels, aluminum frame",
      Power: "Integrated power module",
      Dimensions: "Standard/Large",
      Warranty: "2 years",
    },
  },
};

function ensureFields(p) {
  const cat =
    p.category && defaultsByCategory[p.category] ? p.category : "seating";
  const d = defaultsByCategory[cat];

  if (!Array.isArray(p.descriptionLong) || p.descriptionLong.length === 0) {
    p.descriptionLong = defaultLong;
  }

  if (
    typeof p.additionalInformation !== "object" ||
    p.additionalInformation === null
  ) {
    p.additionalInformation = { ...d.additionalInformation };
  } else {
    // Fill any missing keys with defaults without overwriting existing
    for (const [k, v] of Object.entries(d.additionalInformation)) {
      if (
        p.additionalInformation[k] == null ||
        p.additionalInformation[k] === ""
      ) {
        p.additionalInformation[k] = v;
      }
    }
  }

  // Ensure reviewsCount mirrors numeric reviews
  const reviewsNum = Number(p.reviews || 0);
  p.reviews = Number.isFinite(reviewsNum) ? reviewsNum : 0;
  if (p.reviewsCount == null) p.reviewsCount = p.reviews;

  // Ensure galleryPairs (two wide images for tabs). Use product image as fallback.
  if (!Array.isArray(p.galleryPairs) || p.galleryPairs.length === 0) {
    const img = p.image || "assets/images/asgaard_sofa.png";
    p.galleryPairs = [{ left: img, right: img }];
  }
}

(db.products || []).forEach(ensureFields);

fs.writeFileSync(productsPath, JSON.stringify(db, null, 2));
console.log(
  "Ensured tabs data for all products: descriptionLong, additionalInformation, reviewsCount, galleryPairs"
);
