import express from "express";
import { User } from "../models/user.js";
import SubmitAiWebsite from "../models/submitAiWebsite.js";
import SubmitDirectory from "../models/submitDirectory.js";
import WebsiteSubmission from "../models/websiteSubmission.js";
// import { authMiddleware } from "../middleware/authMiddleware.js";
import {authenticate} from "../middleware/authMiddleware.js"
const router = express.Router();

// GET /api/users/dashboard - Current user's dashboard
router.get("/", authenticate, async (req, res) => {
  try {
    const userId = req.user.id; // set by authMiddleware

    // 1. Get user info (exclude passwordHash)
    const user = await User.findById(userId).select("-passwordHash");
    if (!user) {
      return res.status(404).json({ message: "User not found" });
    }

    // 2. Get all submissions for this user
    const aiWebsites = await SubmitAiWebsite.find({ submittedBy: userId });
    const directories = await SubmitDirectory.find({ submittedBy: userId });
    const websiteSubmissions = await WebsiteSubmission.find({ submittedBy: userId });

    // 3. Send response
    res.json({
      user,
      submissions: {
        aiWebsites,
        directories,
        websiteSubmissions,
      },
    });
  } catch (error) {
    console.error("Error fetching dashboard:", error);
    res.status(500).json({ message: "Server error" });
  }
});

export default router;
