import { Request, Response } from 'express';
import { AuthRequest } from '../middleware/auth';
import * as auditService from '../services/auditService';
import * as pendingService from '../services/pendingService';

// Audit endpoints
export const getAuditLogs = async (req: Request, res: Response) => {
    try {
        const { tableName, recordId } = req.query;
        const logs = await auditService.getAuditLogs(
            tableName as string,
            recordId ? parseInt(recordId as string, 10) : undefined
        );
        res.json(logs);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to get audit logs' });
    }
};

export const closeLead = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const leadId = parseInt(req.params.leadId, 10);
        const { closeReason, subStatus } = req.body;
        const lead = await auditService.closeLead(leadId, closeReason, subStatus, req.user.userId);
        res.json(lead);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to close lead' });
    }
};

export const reopenLead = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const leadId = parseInt(req.params.leadId, 10);
        const lead = await auditService.reopenLead(leadId, req.user.userId);
        res.json(lead);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to reopen lead' });
    }
};

// Pendings endpoints
export const getPendings = async (req: Request, res: Response) => {
    try {
        const pendings = await pendingService.getAllPendings(req.query);
        res.json(pendings);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to get pendings' });
    }
};

export const createPending = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const { leadId, pendingType, description, dueDate, assignedTo, slaHours } = req.body;
        const pending = await pendingService.createPending(
            leadId,
            pendingType,
            description,
            dueDate,
            assignedTo,
            req.user.userId,
            slaHours
        );
        res.status(201).json(pending);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to create pending' });
    }
};

export const updatePending = async (req: Request, res: Response) => {
    try {
        const id = parseInt(req.params.id, 10);
        const pending = await pendingService.updatePending(id, req.body);
        res.json(pending);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to update pending' });
    }
};

export const escalatePending = async (req: AuthRequest, res: Response) => {
    try {
        if (!req.user) {
            return res.status(401).json({ error: 'Unauthorized' });
        }

        const id = parseInt(req.params.id, 10);
        const { escalatedTo } = req.body;
        const pending = await pendingService.escalatePending(id, escalatedTo, req.user.userId);
        res.json(pending);
    } catch (error: any) {
        res.status(400).json({ error: error.message || 'Failed to escalate pending' });
    }
};

export const getBreachedPendings = async (req: Request, res: Response) => {
    try {
        const pendings = await pendingService.getBreachedPendings();
        res.json(pendings);
    } catch (error: any) {
        res.status(500).json({ error: error.message || 'Failed to get breached pendings' });
    }
};
