"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// backend/src/api/auth.ts
const express_1 = __importDefault(require("express"));
const dotenv = __importStar(require("dotenv"));
const path = __importStar(require("path"));
const AuthService_1 = require("../services/AuthService");
const SessionService_1 = require("../services/SessionService");
const AuthLogger_1 = require("../services/AuthLogger");
dotenv.config({ path: path.resolve(__dirname, '../../.env') });
const SESSION_SECRET = process.env.SESSION_SECRET;
const JWT_SECRET = process.env.JWT_SECRET;
if (!SESSION_SECRET) {
    throw new Error('SESSION_SECRET is not defined in the environment variables.');
}
if (!JWT_SECRET) {
    throw new Error('JWT_SECRET is not defined in the environment variables.');
}
const router = express_1.default.Router();
router.post('/passphrase', (req, res) => {
    const { passphrase } = req.body;
    const ipAddress = req.ip || ''; // Get IP address for logging, default to empty string if undefined
    if (!passphrase) {
        AuthLogger_1.AuthLogger.logAttempt('failure', ipAddress);
        return res.status(400).json({ message: 'Passphrase is required.' });
    }
    if (AuthService_1.AuthService.validatePassphrase(passphrase)) {
        const session = SessionService_1.SessionService.createSession();
        SessionService_1.SessionService.authenticateSession(session.id);
        AuthLogger_1.AuthLogger.logAttempt('success', ipAddress);
        return res.status(200).json({ message: 'Authentication successful', sessionToken: session.id });
    }
    else {
        AuthLogger_1.AuthLogger.logAttempt('failure', ipAddress);
        return res.status(401).json({ message: 'Invalid passphrase' });
    }
});
exports.default = router;
