const FidoKeyService = require('../services/fidoKey.service');
const { asyncHandler } = require('../middleware/errorHandler');

class FidoKeyController {
    /**
     * Get all FIDO keys
     * GET /api/fido-keys
     */
    static getAllKeys = asyncHandler(async (req, res) => {
        const { user_id } = req.query;
        const keys = user_id
            ? FidoKeyService.getKeysByUser(parseInt(user_id))
            : FidoKeyService.getAllKeys();

        res.json({
            status: 'success',
            data: keys
        });
    });

    /**
     * Get FIDO key by ID
     * GET /api/fido-keys/:id
     */
    static getKeyById = asyncHandler(async (req, res) => {
        const { id } = req.params;
        const key = FidoKeyService.getKeyById(parseInt(id));

        res.json({
            status: 'success',
            data: key
        });
    });

    /**
     * Get FIDO key by serial number
     * GET /api/fido-keys/serial/:serialNumber
     */
    static getKeyBySerial = asyncHandler(async (req, res) => {
        const { serialNumber } = req.params;
        const key = FidoKeyService.getKeyBySerial(serialNumber);

        res.json({
            status: 'success',
            data: key
        });
    });

    /**
     * Get FIDO keys by status
     * GET /api/fido-keys/status/:status
     */
    static getKeysByStatus = asyncHandler(async (req, res) => {
        const { status } = req.params;
        const keys = FidoKeyService.getKeysByStatus(status);

        res.json({
            status: 'success',
            data: keys
        });
    });

    /**
     * Create new FIDO key
     * POST /api/fido-keys
     */
    static createKey = asyncHandler(async (req, res) => {
        const key = FidoKeyService.createKey(
            req.body,
            req.user.id,
            req.ip
        );

        res.status(201).json({
            status: 'success',
            data: key
        });
    });

    /**
     * Update FIDO key
     * PUT /api/fido-keys/:id
     */
    static updateKey = asyncHandler(async (req, res) => {
        const { id } = req.params;

        const key = FidoKeyService.updateKey(
            parseInt(id),
            req.body,
            req.user.id,
            req.ip
        );

        res.json({
            status: 'success',
            data: key
        });
    });

    /**
     * Update FIDO key status
     * PUT /api/fido-keys/:id/status
     */
    static updateKeyStatus = asyncHandler(async (req, res) => {
        const { id } = req.params;
        const { status } = req.body;

        if (!status) {
            return res.status(400).json({
                status: 'error',
                message: 'Status is required'
            });
        }

        const key = FidoKeyService.updateKeyStatus(
            parseInt(id),
            status,
            req.user.id,
            req.ip
        );

        res.json({
            status: 'success',
            data: key
        });
    });

    /**
     * Delete FIDO key
     * DELETE /api/fido-keys/:id
     */
    static deleteKey = asyncHandler(async (req, res) => {
        const { id } = req.params;

        const deleted = FidoKeyService.deleteKey(
            parseInt(id),
            req.user.id,
            req.ip
        );

        if (!deleted) {
            return res.status(404).json({
                status: 'error',
                message: 'FIDO key not found'
            });
        }

        res.json({
            status: 'success',
            message: 'FIDO key deleted successfully'
        });
    });

    /**
     * Get FIDO key statistics
     * GET /api/fido-keys/stats
     */
    static getStatistics = asyncHandler(async (req, res) => {
        const stats = FidoKeyService.getStatistics();

        res.json({
            status: 'success',
            data: stats
        });
    });
}

module.exports = FidoKeyController;
