import type { NextFunction, Request, Response } from "express";
import { readSession } from "./session";

export function requireAuth(req: Request, res: Response, next: NextFunction): void {
  const user = readSession(req);
  if (!user) {
    res.status(401).json({ error: "Not authenticated" });
    return;
  }
  req.user = user;
  next();
}

export function requireAdmin(req: Request, res: Response, next: NextFunction): void {
  if (req.user?.role !== "admin") {
    res.status(403).json({ error: "Admin access required" });
    return;
  }
  next();
}

export function requireEmployee(req: Request, res: Response, next: NextFunction): void {
  if (req.user?.employeeId == null) {
    res.status(403).json({ error: "No employee profile linked to this account" });
    return;
  }
  next();
}
