/**
 * EstimacionController - Controller de estimaciones de obra
 *
 * Endpoints REST para gestión de estimaciones periódicas.
 * Incluye workflow de aprobación: draft -> submitted -> reviewed -> approved -> invoiced -> paid
 *
 * @module Estimates
 */

import { Router, Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import {
  EstimacionService,
  CreateEstimacionDto,
  AddConceptoDto,
  AddGeneradorDto,
  EstimacionFilters,
} from '../services/estimacion.service';
import { AuthMiddleware } from '../../auth/middleware/auth.middleware';
import { AuthService } from '../../auth/services/auth.service';
import { User } from '../../core/entities/user.entity';
import { Tenant } from '../../core/entities/tenant.entity';
import { RefreshToken } from '../../auth/entities/refresh-token.entity';

/**
 * Local interface for service context
 */
interface ServiceContext {
  tenantId: string;
  userId?: string;
}

/**
 * Crear router de estimaciones
 */
export function createEstimacionController(dataSource: DataSource): Router {
  const router = Router();

  // Repositorios
  const userRepository = dataSource.getRepository(User);
  const tenantRepository = dataSource.getRepository(Tenant);
  const refreshTokenRepository = dataSource.getRepository(RefreshToken);

  // Servicios
  const estimacionService = new EstimacionService(dataSource);
  const authService = new AuthService(userRepository, tenantRepository, refreshTokenRepository as any);
  const authMiddleware = new AuthMiddleware(authService, dataSource);

  // Helper para crear contexto de servicio
  const getContext = (req: Request): ServiceContext => {
    if (!req.tenantId) {
      throw new Error('Tenant ID is required');
    }
    return {
      tenantId: req.tenantId,
      userId: req.user?.sub,
    };
  };

  /**
   * GET /estimaciones
   * Listar estimaciones con filtros
   */
  router.get('/', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const page = parseInt(req.query.page as string) || 1;
      const limit = Math.min(parseInt(req.query.limit as string) || 20, 100);

      const filters: EstimacionFilters = {
        contratoId: req.query.contratoId as string,
        fraccionamientoId: req.query.fraccionamientoId as string,
        status: req.query.status as any,
        periodFrom: req.query.periodFrom ? new Date(req.query.periodFrom as string) : undefined,
        periodTo: req.query.periodTo ? new Date(req.query.periodTo as string) : undefined,
      };

      const result = await estimacionService.findWithFilters(getContext(req), filters, page, limit);

      res.status(200).json({
        success: true,
        data: result.data,
        pagination: {
          total: result.total,
          page: result.page,
          limit: result.limit,
          totalPages: result.totalPages,
        },
      });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /estimaciones/summary/:contratoId
   * Obtener resumen de estimaciones por contrato
   */
  router.get('/summary/:contratoId', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const summary = await estimacionService.getContractSummary(getContext(req), req.params.contratoId);
      res.status(200).json({ success: true, data: summary });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /estimaciones/:id
   * Obtener estimación con detalles completos
   */
  router.get('/:id', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const estimacion = await estimacionService.findWithDetails(getContext(req), req.params.id);
      if (!estimacion) {
        res.status(404).json({ error: 'Not Found', message: 'Estimate not found' });
        return;
      }

      res.status(200).json({ success: true, data: estimacion });
    } catch (error) {
      next(error);
    }
  });

  /**
   * POST /estimaciones
   * Crear estimación
   */
  router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'resident'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: CreateEstimacionDto = req.body;

      if (!dto.contratoId || !dto.fraccionamientoId || !dto.periodStart || !dto.periodEnd) {
        res.status(400).json({ error: 'Bad Request', message: 'contratoId, fraccionamientoId, periodStart and periodEnd are required' });
        return;
      }

      const estimacion = await estimacionService.createEstimacion(getContext(req), dto);
      res.status(201).json({ success: true, data: estimacion });
    } catch (error) {
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/conceptos
   * Agregar concepto a estimación
   */
  router.post('/:id/conceptos', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'resident'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: AddConceptoDto = req.body;

      if (!dto.conceptoId || dto.quantityCurrent === undefined || dto.unitPrice === undefined) {
        res.status(400).json({ error: 'Bad Request', message: 'conceptoId, quantityCurrent and unitPrice are required' });
        return;
      }

      const concepto = await estimacionService.addConcepto(getContext(req), req.params.id, dto);
      res.status(201).json({ success: true, data: concepto });
    } catch (error) {
      if (error instanceof Error && error.message.includes('non-draft')) {
        res.status(400).json({ error: 'Bad Request', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/conceptos/:conceptoId/generadores
   * Agregar generador a concepto de estimación
   */
  router.post('/conceptos/:conceptoId/generadores', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'resident'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: AddGeneradorDto = req.body;

      if (!dto.generatorNumber || dto.quantity === undefined) {
        res.status(400).json({ error: 'Bad Request', message: 'generatorNumber and quantity are required' });
        return;
      }

      const generador = await estimacionService.addGenerador(getContext(req), req.params.conceptoId, dto);
      res.status(201).json({ success: true, data: generador });
    } catch (error) {
      if (error instanceof Error && error.message === 'Concepto not found') {
        res.status(404).json({ error: 'Not Found', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/submit
   * Enviar estimación para revisión
   */
  router.post('/:id/submit', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'resident'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const estimacion = await estimacionService.submit(getContext(req), req.params.id);
      if (!estimacion) {
        res.status(400).json({ error: 'Bad Request', message: 'Cannot submit this estimate' });
        return;
      }

      res.status(200).json({ success: true, data: estimacion, message: 'Estimate submitted for review' });
    } catch (error) {
      if (error instanceof Error && error.message.includes('Invalid status')) {
        res.status(400).json({ error: 'Bad Request', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/review
   * Revisar estimación
   */
  router.post('/:id/review', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const estimacion = await estimacionService.review(getContext(req), req.params.id);
      if (!estimacion) {
        res.status(400).json({ error: 'Bad Request', message: 'Cannot review this estimate' });
        return;
      }

      res.status(200).json({ success: true, data: estimacion, message: 'Estimate reviewed' });
    } catch (error) {
      if (error instanceof Error && error.message.includes('Invalid status')) {
        res.status(400).json({ error: 'Bad Request', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/approve
   * Aprobar estimación
   */
  router.post('/:id/approve', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const estimacion = await estimacionService.approve(getContext(req), req.params.id);
      if (!estimacion) {
        res.status(400).json({ error: 'Bad Request', message: 'Cannot approve this estimate' });
        return;
      }

      res.status(200).json({ success: true, data: estimacion, message: 'Estimate approved' });
    } catch (error) {
      if (error instanceof Error && error.message.includes('Invalid status')) {
        res.status(400).json({ error: 'Bad Request', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/reject
   * Rechazar estimación
   */
  router.post('/:id/reject', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const { reason } = req.body;
      if (!reason) {
        res.status(400).json({ error: 'Bad Request', message: 'reason is required' });
        return;
      }

      const estimacion = await estimacionService.reject(getContext(req), req.params.id, reason);
      if (!estimacion) {
        res.status(400).json({ error: 'Bad Request', message: 'Cannot reject this estimate' });
        return;
      }

      res.status(200).json({ success: true, data: estimacion, message: 'Estimate rejected' });
    } catch (error) {
      if (error instanceof Error && error.message.includes('Invalid status')) {
        res.status(400).json({ error: 'Bad Request', message: error.message });
        return;
      }
      next(error);
    }
  });

  /**
   * POST /estimaciones/:id/recalculate
   * Recalcular totales de estimación
   */
  router.post('/:id/recalculate', authMiddleware.authenticate, authMiddleware.authorize('admin', 'engineer'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      await estimacionService.recalculateTotals(getContext(req), req.params.id);
      const estimacion = await estimacionService.findWithDetails(getContext(req), req.params.id);

      res.status(200).json({ success: true, data: estimacion, message: 'Totals recalculated' });
    } catch (error) {
      next(error);
    }
  });

  /**
   * DELETE /estimaciones/:id
   * Eliminar estimación (solo draft)
   */
  router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      const tenantId = req.tenantId;
      if (!tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const estimacion = await estimacionService.findById(getContext(req), req.params.id);
      if (!estimacion) {
        res.status(404).json({ error: 'Not Found', message: 'Estimate not found' });
        return;
      }

      if (estimacion.status !== 'draft') {
        res.status(400).json({ error: 'Bad Request', message: 'Only draft estimates can be deleted' });
        return;
      }

      const deleted = await estimacionService.softDelete(getContext(req), req.params.id);
      if (!deleted) {
        res.status(404).json({ error: 'Not Found', message: 'Estimate not found' });
        return;
      }

      res.status(200).json({ success: true, message: 'Estimate deleted' });
    } catch (error) {
      next(error);
    }
  });

  return router;
}

export default createEstimacionController;
