"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const adminService_1 = __importDefault(require("../services/adminService"));
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
const SECRET_KEY = "Fwcyfyl123."; // 生产环境请务必使用环境变量
/**
 * 管理员控制器类，处理与管理员相关的HTTP请求
 */
class AdminController {
    /** 注册新管理员 */
    async register(req, res) {
        try {
            const { username, password } = req.body;
            const newAdmin = await adminService_1.default.registerAdmin({ username, password });
            res.status(201).json(newAdmin);
        }
        catch (error) {
            res.status(400).json({ message: error.message });
        }
    }
    /** 管理员登录 */
    async login(req, res) {
        try {
            const { username, password } = req.body;
            const admin = await adminService_1.default.loginAdmin(username, password);
            // 生成JWT Token
            const token = jsonwebtoken_1.default.sign({ id: admin._id, username: admin.username }, SECRET_KEY, { expiresIn: "1d" });
            res.status(200).json({ message: "Login successful", token });
        }
        catch (error) {
            res.status(401).json({ message: error.message });
        }
    }
    /** 获取所有管理员 */
    async getAdmins(req, res) {
        try {
            const admins = await adminService_1.default.getAdmins();
            res.status(200).json(admins);
        }
        catch (error) {
            res.status(500).json({ message: error.message });
        }
    }
    /** 根据ID获取单个管理员 */
    async getAdminById(req, res) {
        try {
            const admin = await adminService_1.default.getAdminById(req.params.id);
            if (!admin) {
                res.status(404).json({ message: "Admin not found" });
            }
            else {
                res.status(200).json(admin);
            }
        }
        catch (error) {
            res.status(500).json({ message: error.message });
        }
    }
    /** 根据ID更新管理员 */
    async updateAdmin(req, res) {
        try {
            const updatedAdmin = await adminService_1.default.updateAdmin(req.params.id, req.body);
            if (!updatedAdmin) {
                res.status(404).json({ message: "Admin not found" });
            }
            else {
                res.status(200).json(updatedAdmin);
            }
        }
        catch (error) {
            res.status(400).json({ message: error.message });
        }
    }
    /** 根据ID删除管理员 */
    async deleteAdmin(req, res) {
        try {
            const deletedAdmin = await adminService_1.default.deleteAdmin(req.params.id);
            if (!deletedAdmin) {
                res.status(404).json({ message: "Admin not found" });
            }
            else {
                res.status(200).json({ message: "Admin deleted successfully" });
            }
        }
        catch (error) {
            res.status(500).json({ message: error.message });
        }
    }
}
exports.default = new AdminController();
