const express = require("express");
const path = require("path");
const router = express.Router();
const homeController = require("../controllers/homeController");
const aboutController = require("../controllers/aboutController");
const aboutUsController = require("../controllers/aboutUsController");
const headerController = require("../controllers/headerController");
const footerController = require("../controllers/footerController");
const contactController = require("../controllers/contactController");
const faqController = require("../controllers/faqController");
const visaController = require("../controllers/visaController");
const safetyController = require("../controllers/safetyController");
const campLocationController = require("../controllers/campLocationController");
// Booking flow removed

const insuranceController = require("../controllers/insuranceController");
const termsController = require("../controllers/termsController"); // <-- IMPORT ĐÃ CÓ
const activityController = require("../controllers/activityController");
const travelController = require("../controllers/travelController");
const bookingSubmissionController = require("../controllers/bookingSubmissionController");

const serviceController = require("../controllers/serviceController");
// Blog controllers
const blogController = require("../controllers/blogController");
const blogCategoryController = require("../controllers/blogCategoryController");
const blogTagController = require("../controllers/blogTagController");

// Trang chủ
router.get("/", (req, res) => {
  res.render("index", {
    title: "Welcome",
    layout: "layouts/main",
  });
});

// API để lấy dữ liệu trang chủ
router.get("/api/home", homeController.api);

// API để lấy dữ liệu about
router.get("/api/about", aboutController.api);

// Public about-us page and API (aboutUs.json flow)
router.get("/about-us", aboutUsController.page);
// Return a list/array of AboutUs records for frontend consumption
router.get("/api/about-us", aboutUsController.apiList);

// Header API route
router.get("/api/header", headerController.api);

// Menu Tree API route (for frontend)
router.get("/api/menu-tree", headerController.getMenuTreeAPI);

// Footer API route
router.get("/api/footer", footerController.api);

// Contact API route
router.get("/api/contact", contactController.api);

// Contact form submission (public)
router.post("/api/contact/submit", contactController.submitForm);

// Appointment API
const appointmentController = require("../controllers/appointmentController");
router.get("/api/appointment", appointmentController.api);
router.post("/api/appointment/submit", appointmentController.submitAppointment);

// Pricing API
const pricingController = require("../controllers/pricingController");
router.get("/api/pricing", pricingController.api);

router.get("/api/faq", faqController.api);
// Safety API route
router.get("/api/safety", safetyController.api);
// Activity API routes
router.get("/api/activities", activityController.api);
router.get("/api/activities/:id", activityController.apiDetail);

// Camp Location API route
router.get("/api/camp-location", campLocationController.api);
// Booking routes removed
// Insurance APi route
router.get("/api/insurance", insuranceController.api);

router.get("/api/terms", termsController.api);

// Travel public page and API
router.get("/travel", async (req, res) => {
  try {
    const Travel = require("../models/travel");
    const travel = await Travel.findOne();

    if (!travel) {
      return res.status(404).render("errors/404", {
        title: "Page Not Found",
        message: "Travel information not found",
      });
    }

    res.render("page/travel", {
      title: travel.page.title,
      data: travel.toObject(),
    });
  } catch (error) {
    console.error("Error loading travel page:", error);
    res.status(500).render("errors/500", {
      title: "Server Error",
      message: "Error loading travel page",
    });
  }
});
router.get("/api/travel", travelController.api);

// Booking submission APIs (public endpoints)
router.post("/api/booking/submit", bookingSubmissionController.submitBooking);
router.get(
  "/api/activity/:activityId/sessions",
  bookingSubmissionController.getAvailableSessions,
);
router.get(
  "/api/activity/:activityId/session/:sessionId/availability",
  bookingSubmissionController.getSessionAvailability,
);

// New API for creating bookings directly into camp sessions (by program)
router.post(
  "/api/camps/:program/sessions/:sessionId/bookings",
  activityController.createSessionBookingByProgram,
);
router.get(
  "/api/camps/:program/sessions/:sessionId/bookings",
  activityController.getSessionBookingsByProgram,
);
// Keep admin-style update/delete by activityId (protected) if needed
router.put(
  "/api/camps/:activityId/sessions/:sessionId/bookings/:bookingId",
  activityController.updateSessionBooking,
);
router.delete(
  "/api/camps/:activityId/sessions/:sessionId/bookings/:bookingId",
  activityController.deleteSessionBooking,
);

// Demo booking form
router.get("/demo/booking-form", (req, res) => {
  res.sendFile(path.join(__dirname, "../views/demo/booking-form.html"));
});

// Demo session booking API
router.get("/demo/session-booking-api", (req, res) => {
  res.sendFile(path.join(__dirname, "../views/demo/session-booking-api.html"));
});

// Blog API Routes
router.get("/api/blog", blogController.api);
router.get("/api/blog/featured", blogController.apiFeatured);
router.get("/api/blog/recent", blogController.apiRecent);

// Blog Categories API (must come before /api/blog/:slug)
router.get("/api/blog/categories", blogCategoryController.api);
router.get("/api/blog/categories/:slug", blogCategoryController.apiShow);

// Blog Tags API (must come before /api/blog/:slug)
router.get("/api/blog/tags", blogTagController.api);
router.get("/api/blog/tags/popular", blogTagController.apiPopular);
router.get("/api/blog/tags/:slug", blogTagController.apiShow);

// Blog post specific APIs (must come before /api/blog/:slug)
router.get("/api/blog/:id/categories", blogController.apiCategories);
router.get("/api/blog/:id/tags", blogController.apiTags);

// Blog comments (must come before /api/blog/:slug)
router.post("/api/blog/:slug/comments", blogController.apiCreateComment);

// Blog detail by slug (must come last among blog routes)
router.get("/api/blog/:slug", blogController.apiShow);

// // API route cho blog detail
// router.get('/api/blog-detail', blogDetailController.api);

/* CMS - Hailearning
 */
// service
router.get("/service", serviceController.index);
router.post("/service", serviceController.update);
router.get("/api/service", serviceController.api);

// Service details by slug
router.get("/api/service/:slug", serviceController.getServiceBySlug);

// Service slugs list
router.get("/api/service-slugs", serviceController.getServiceSlugs);

router.get("/api/visa", visaController.api);
router.get("/api/visa/country", visaController.apiCountries);

module.exports = router;
