const express = require("express");
const { query } = require("../config/database");
const router = express.Router();

// Get all products
router.get("/products", async (req, res) => {
  try {
    const result = await query(
      "SELECT id, name, description, shortdescription, price, imageurl, images, category, color, stockquantity, isactive, createdat FROM products WHERE isactive = true ORDER BY createdat DESC"
    );
    res.json({
      success: true,
      products: result.rows,
    });
  } catch (error) {
    console.error("Products API error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

// Get featured products
router.get("/products/featured", async (req, res) => {
  try {
    const limit = parseInt(req.query.limit) || 4;
    const result = await query(
      "SELECT id, name, description, price, imageurl, images FROM products WHERE isactive = true ORDER BY createdat DESC LIMIT $1",
      [limit]
    );
    res.json({
      success: true,
      products: result.rows,
    });
  } catch (error) {
    console.error("Featured products error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

// Get single product
router.get("/products/:id", async (req, res) => {
  try {
    const result = await query(
      "SELECT * FROM products WHERE id = $1 AND isactive = true",
      [req.params.id]
    );
    if (result.rows.length === 0) {
      return res
        .status(404)
        .json({ success: false, message: "Product not found" });
    }
    res.json({
      success: true,
      product: result.rows[0],
    });
  } catch (error) {
    console.error("Product detail error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

// Get site settings
router.get("/settings", async (req, res) => {
  try {
    const result = await query("SELECT * FROM sitesettings LIMIT 1");
    res.json({
      success: true,
      settings: result.rows[0] || {},
    });
  } catch (error) {
    console.error("Settings error:", error);
    res.json({ success: true, settings: {} });
  }
});

// Get homepage sections
router.get("/homepage/sections", async (req, res) => {
  try {
    const result = await query(
      "SELECT * FROM homepagesections ORDER BY displayorder ASC"
    );
    res.json({
      success: true,
      sections: result.rows,
    });
  } catch (error) {
    console.error("Homepage sections error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

// Get portfolio projects
router.get("/portfolio/projects", async (req, res) => {
  try {
    const result = await query(
      "SELECT id, title, description, imageurl, categoryid, createdat FROM portfolioprojects ORDER BY createdat DESC"
    );
    res.json({
      success: true,
      projects: result.rows,
    });
  } catch (error) {
    console.error("Portfolio error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

// Get blog posts
router.get("/blog/posts", async (req, res) => {
  try {
    const result = await query(
      "SELECT id, title, slug, excerpt, content, imageurl, ispublished, createdat FROM blogposts WHERE ispublished = true ORDER BY createdat DESC"
    );
    res.json({
      success: true,
      posts: result.rows,
    });
  } catch (error) {
    console.error("Blog posts error:", error);
    res.status(500).json({ success: false, message: "Server error" });
  }
});

module.exports = router;
