import type { Express } from "express";
import { createServer, type Server } from "http";
import { storage } from "./storage";
import { setupAuth, isAuthenticated } from "./simpleAuth";
import { insertArticleSchema, insertMediaOutletRequestSchema, insertBidSchema, insertCommentSchema } from "@shared/schema";

export async function registerRoutes(app: Express): Promise<Server> {
  // Auth middleware
  await setupAuth(app);

  // Auth routes
  app.get('/api/auth/user', isAuthenticated, async (req: any, res) => {
    try {
      const user = req.user;
      res.json(user);
    } catch (error) {
      console.error("Error fetching user:", error);
      res.status(500).json({ message: "Failed to fetch user" });
    }
  });

  // Media outlet routes
  app.get('/api/media-outlets', async (req, res) => {
    try {
      const category = req.query.category as string;
      const outlets = await storage.getMediaOutlets(category);
      res.json(outlets);
    } catch (error) {
      console.error("Error fetching media outlets:", error);
      res.status(500).json({ message: "Failed to fetch media outlets" });
    }
  });

  app.get('/api/media-outlets/:slug', async (req, res) => {
    try {
      const outlet = await storage.getMediaOutletBySlug(req.params.slug);
      if (!outlet) {
        return res.status(404).json({ message: "Media outlet not found" });
      }
      res.json(outlet);
    } catch (error) {
      console.error("Error fetching media outlet:", error);
      res.status(500).json({ message: "Failed to fetch media outlet" });
    }
  });

  // Article routes
  app.get('/api/media-outlets/:outletId/articles', async (req, res) => {
    try {
      const articles = await storage.getArticlesByOutlet(req.params.outletId);
      res.json(articles);
    } catch (error) {
      console.error("Error fetching articles:", error);
      res.status(500).json({ message: "Failed to fetch articles" });
    }
  });

  app.get('/api/articles/:slug', async (req, res) => {
    try {
      const article = await storage.getArticleBySlug(req.params.slug);
      if (!article) {
        return res.status(404).json({ message: "Article not found" });
      }
      res.json(article);
    } catch (error) {
      console.error("Error fetching article:", error);
      res.status(500).json({ message: "Failed to fetch article" });
    }
  });

  app.get('/api/articles/featured', async (req, res) => {
    try {
      const limit = parseInt(req.query.limit as string) || 10;
      const articles = await storage.getFeaturedArticles(limit);
      res.json(articles);
    } catch (error) {
      console.error("Error fetching featured articles:", error);
      res.status(500).json({ message: "Failed to fetch featured articles" });
    }
  });

  app.post('/api/articles', isAuthenticated, async (req: any, res) => {
    try {
      const user = req.user;
      
      if (!user || (user.role !== 'admin' && user.role !== 'superadmin')) {
        return res.status(403).json({ message: "Insufficient permissions" });
      }

      const articleData = insertArticleSchema.parse({
        ...req.body,
        authorId: user.id
      });
      
      const article = await storage.createArticle(articleData);
      res.status(201).json(article);
    } catch (error) {
      console.error("Error creating article:", error);
      res.status(500).json({ message: "Failed to create article" });
    }
  });

  // Prediction market routes
  app.get('/api/prediction-markets', async (req, res) => {
    try {
      const articleId = req.query.articleId as string;
      const markets = await storage.getPredictionMarkets(articleId);
      res.json(markets);
    } catch (error) {
      console.error("Error fetching prediction markets:", error);
      res.status(500).json({ message: "Failed to fetch prediction markets" });
    }
  });

  // Auction routes
  app.get('/api/auctions', async (req, res) => {
    try {
      const auctions = await storage.getActiveAuctions();
      res.json(auctions);
    } catch (error) {
      console.error("Error fetching auctions:", error);
      res.status(500).json({ message: "Failed to fetch auctions" });
    }
  });

  app.post('/api/auctions/:id/bid', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const bidData = insertBidSchema.parse({
        ...req.body,
        auctionId: req.params.id,
        bidderId: userId
      });
      
      const bid = await storage.placeBid(bidData);
      res.status(201).json(bid);
    } catch (error) {
      console.error("Error placing bid:", error);
      res.status(500).json({ message: "Failed to place bid" });
    }
  });

  // Media outlet request routes
  app.get('/api/media-outlet-requests', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const user = await storage.getUser(userId);
      
      if (!user || user.role !== 'superadmin') {
        return res.status(403).json({ message: "Insufficient permissions" });
      }

      const status = req.query.status as string;
      const requests = await storage.getMediaOutletRequests(status);
      res.json(requests);
    } catch (error) {
      console.error("Error fetching requests:", error);
      res.status(500).json({ message: "Failed to fetch requests" });
    }
  });

  app.post('/api/media-outlet-requests', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const requestData = insertMediaOutletRequestSchema.parse({
        ...req.body,
        requesterId: userId
      });
      
      const request = await storage.createMediaOutletRequest(requestData);
      res.status(201).json(request);
    } catch (error) {
      console.error("Error creating request:", error);
      res.status(500).json({ message: "Failed to create request" });
    }
  });

  app.patch('/api/media-outlet-requests/:id', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const user = await storage.getUser(userId);
      
      if (!user || user.role !== 'superadmin') {
        return res.status(403).json({ message: "Insufficient permissions" });
      }

      const { status } = req.body;
      const request = await storage.updateMediaOutletRequestStatus(req.params.id, status, userId);
      res.json(request);
    } catch (error) {
      console.error("Error updating request:", error);
      res.status(500).json({ message: "Failed to update request" });
    }
  });

  // Comment routes
  app.get('/api/articles/:articleId/comments', async (req, res) => {
    try {
      const comments = await storage.getCommentsByArticle(req.params.articleId);
      res.json(comments);
    } catch (error) {
      console.error("Error fetching comments:", error);
      res.status(500).json({ message: "Failed to fetch comments" });
    }
  });

  app.post('/api/articles/:articleId/comments', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const commentData = insertCommentSchema.parse({
        ...req.body,
        articleId: req.params.articleId,
        authorId: userId
      });
      
      const comment = await storage.createComment(commentData);
      res.status(201).json(comment);
    } catch (error) {
      console.error("Error creating comment:", error);
      res.status(500).json({ message: "Failed to create comment" });
    }
  });

  // Analytics routes
  app.get('/api/analytics', isAuthenticated, async (req: any, res) => {
    try {
      const userId = req.user.claims.sub;
      const user = await storage.getUser(userId);
      
      if (!user || (user.role !== 'admin' && user.role !== 'superadmin')) {
        return res.status(403).json({ message: "Insufficient permissions" });
      }

      const analytics = await storage.getAnalytics();
      res.json(analytics);
    } catch (error) {
      console.error("Error fetching analytics:", error);
      res.status(500).json({ message: "Failed to fetch analytics" });
    }
  });

  const httpServer = createServer(app);
  return httpServer;
}
