console.log("Lecture routes loaded")
const express = require("express")
const requireUser = require("../../middleware/requireUser")
const requireAdmin = require("../../middleware/requireAdmin")
const router = express.Router()
const Lecture = require("../../models/Lecture")
const Chapter = require("../../models/Chapter")
const Topic = require("../../models/Topic")

// CREATE LECTURE (ADMIN ONLY)
router.post("/lectures", requireAdmin, async (req, res) => {
  try {
    const {
      title,
      topic,
      contentType,
      contentUrl,
      order,
      isFreePreview
    } = req.body

    const topicDoc = await Topic.findById(topic)

    if (!topicDoc) {
      return res.status(404).json({message: "Topic not found"})
    }

    const chapterDoc = await Chapter.findById(topicDoc.chapterId)

    const lecture = await Lecture.create({
      title,
      chapterId: topicDoc.chapterId,
      topicId: topic,
      course: chapterDoc.course,
      contentType,
      contentUrl,
      order,
      isFreePreview,
      isActive: true
    })

    res.status(201).json(lecture)
    
  } catch (error) {
    console.error("ADMIN CREATE LECTURE ERROR:", error)
    res.status(500).json({
      message: "Failed to create lecture",
      error: error.message
    })
  }
})

// DELETE LECTURE (ADMIN ONLY)
router.delete("/lectures/:id", requireAdmin, async (req, res) => {
  try {
    await Lecture.findByIdAndDelete(req.params.id)
    res.json({ message: "Lecture deleted successfully" })
  } catch (error) {
    res.status(500).json({
      message: "Failed to delete lecture",
      error: error.message
    })
  }
})

// UPDATE LECTURE (ADMIN ONLY)
router.put("/lectures/:id", requireAdmin, async (req, res) => {
  try {
    const { title, contentUrl } = req.body

    const lecture = await Lecture.findByIdAndUpdate(
      req.params.id,
      { title, contentUrl },
      { new: true }
    )

    if (!lecture) {
      return res.status(404).json({ message: "Lecture not found" })
    }

    res.json(lecture)
  } catch (error) {
    res.status(500).json({
      message: "Failed to update lecture",
      error: error.message
    })
  }
})

// GET SINGLE LECTURE (ADMIN)
router.get("/lectures/single/:lectureId", requireAdmin, async (req, res) => {
  try {
    const lecture = await Lecture.findById(req.params.lectureId)

    if (!lecture) {
      return res.status(404).json({ message: "Lecture not found" })
    }

    res.json(lecture)
  } catch (err) {
    res.status(500).json({ message: err.message })
  }
})

// GET LECTURES BY CHAPTER (ADMIN)
router.get("/user/lectures/:chapterId", requireUser, async (req, res) => {
  try {
    const lectures = await Lecture.find({
      chapterId: req.params.chapterId,
      isActive: true
    }).sort({ order: 1 })

    res.json(lectures)
  } catch (error) {
    res.status(500).json({ message: error.message })
  }
})

router.get("/lectures/:chapterId", requireAdmin, async (req, res) => {
  try {
    const lectures = await Lecture.find({
      chapterId: req.params.chapterId
    }).sort({ order: 1 })

    res.json(lectures)
  } catch (error) {
    res.status(500).json({ message: error.message })
  }
})

router.get("/lectures/topic/:topicId", requireAdmin, async (req, res) => {
  try {
    const lectures = await Lecture.find({
      topicId: req.params.topicId
    }).sort({ order: 1 })

    res.json(lectures)
  } catch (error) {
    res.status(500).json({ message: error.message })
  }
})

router.get("/user/lectures/topic/:topicId", requireUser, async (req, res) => {
  try {
    const lectures = await Lecture.find({
      topicId: req.params.topicId,
      isActive: true
    }).sort({ order: 1 })

    res.json(lectures)
  } catch (error) {
    res.status(500).json({ message: error.message })
  }
})

module.exports = router
