import { Request, Response, NextFunction } from 'express';
import * as paymentsService from '../services/payments.service';
import { sendSuccess, sendError } from '../utils/apiResponse';
import { createPaymentSchema, updatePaymentSchema } from '../validators/payments.validator';

export const listAll = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const payments = await paymentsService.getAllPayments(req.user!.id, req.user!.role);
    return sendSuccess(res, 200, 'Liste globale des paiements', payments);
  } catch (error: any) {
    if (error.message.includes('non autorisé')) return sendError(res, 403, error.message);
    return sendError(res, 400, error.message);
  }
};

export const listByItem = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const payments = await paymentsService.getPaymentsByItem(parseInt(req.params.id as string));
    return sendSuccess(res, 200, 'Liste des paiements', payments);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const getOne = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const payment = await paymentsService.getPaymentById(parseInt(req.params.id as string));
    if (!payment) return sendError(res, 404, 'Paiement introuvable');
    return sendSuccess(res, 200, 'Détail paiement', payment);
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const create = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const validated = createPaymentSchema.parse(req);
    const result = await paymentsService.createPayment(parseInt(req.params.id as string), validated.body, req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 201, 'Paiement enregistré', result);
  } catch (error: any) {
    if (error.name === 'ZodError') return sendError(res, 400, 'Données invalides', error.errors);
    return sendError(res, 400, error.message);
  }
};

export const update = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const validated = updatePaymentSchema.parse(req);
    await paymentsService.updatePayment(parseInt(req.params.id as string), validated.body, req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 200, 'Paiement mis à jour');
  } catch (error: any) {
    if (error.name === 'ZodError') return sendError(res, 400, 'Données invalides', error.errors);
    return sendError(res, 400, error.message);
  }
};

export const remove = async (req: Request, res: Response, next: NextFunction) => {
  try {
    await paymentsService.deletePayment(parseInt(req.params.id as string), req.user!.id, req.ip, req.headers['user-agent'] as unknown as string | undefined | undefined);
    return sendSuccess(res, 200, 'Paiement supprimé');
  } catch (error: any) {
    return sendError(res, 400, error.message);
  }
};

export const triggerReminders = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const { checkAndSendPaymentReminders } = await import('../services/paymentReminders.service');
    const result = await checkAndSendPaymentReminders();
    return sendSuccess(res, 200, 'Vérification des rappels d\'échéance terminée', result);
  } catch (error: any) {
    return sendError(res, 500, error.message);
  }
};
