"use strict";
/**
 * 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
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEstimacionController = createEstimacionController;
const express_1 = require("express");
const estimacion_service_1 = require("../services/estimacion.service");
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
const auth_service_1 = require("../../auth/services/auth.service");
const estimacion_entity_1 = require("../entities/estimacion.entity");
const estimacion_concepto_entity_1 = require("../entities/estimacion-concepto.entity");
const generador_entity_1 = require("../entities/generador.entity");
const estimacion_workflow_entity_1 = require("../entities/estimacion-workflow.entity");
const user_entity_1 = require("../../core/entities/user.entity");
const tenant_entity_1 = require("../../core/entities/tenant.entity");
const refresh_token_entity_1 = require("../../auth/entities/refresh-token.entity");
/**
 * Crear router de estimaciones
 */
function createEstimacionController(dataSource) {
    const router = (0, express_1.Router)();
    // Repositorios
    const estimacionRepository = dataSource.getRepository(estimacion_entity_1.Estimacion);
    const conceptoRepository = dataSource.getRepository(estimacion_concepto_entity_1.EstimacionConcepto);
    const generadorRepository = dataSource.getRepository(generador_entity_1.Generador);
    const workflowRepository = dataSource.getRepository(estimacion_workflow_entity_1.EstimacionWorkflow);
    const userRepository = dataSource.getRepository(user_entity_1.User);
    const tenantRepository = dataSource.getRepository(tenant_entity_1.Tenant);
    const refreshTokenRepository = dataSource.getRepository(refresh_token_entity_1.RefreshToken);
    // Servicios
    const estimacionService = new estimacion_service_1.EstimacionService(estimacionRepository, conceptoRepository, generadorRepository, workflowRepository, dataSource);
    const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
    const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
    // Helper para crear contexto de servicio
    const getContext = (req) => {
        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, res, next) => {
        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) || 1;
            const limit = Math.min(parseInt(req.query.limit) || 20, 100);
            const filters = {
                contratoId: req.query.contratoId,
                fraccionamientoId: req.query.fraccionamientoId,
                status: req.query.status,
                periodFrom: req.query.periodFrom ? new Date(req.query.periodFrom) : undefined,
                periodTo: req.query.periodTo ? new Date(req.query.periodTo) : undefined,
            };
            const result = await estimacionService.findWithFilters(getContext(req), filters, page, limit);
            res.status(200).json({
                success: true,
                data: result.data,
                pagination: result.meta,
            });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /estimaciones/summary/:contratoId
     * Obtener resumen de estimaciones por contrato
     */
    router.get('/summary/:contratoId', authMiddleware.authenticate, async (req, res, next) => {
        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, res, next) => {
        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, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const dto = 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, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const dto = 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, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const dto = 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, res, next) => {
        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, res, next) => {
        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, res, next) => {
        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, res, next) => {
        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, res, next) => {
        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, res, next) => {
        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;
}
exports.default = createEstimacionController;
//# sourceMappingURL=estimacion.controller.js.map