"use strict";
/**
 * AvanceObraController - Controller de avances de obra
 *
 * Endpoints REST para gestión de avances físicos de obra.
 * Incluye workflow de captura -> revisión -> aprobación.
 *
 * @module Progress
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAvanceObraController = createAvanceObraController;
const express_1 = require("express");
const avance_obra_service_1 = require("../services/avance-obra.service");
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
const auth_service_1 = require("../../auth/services/auth.service");
const avance_obra_entity_1 = require("../entities/avance-obra.entity");
const foto_avance_entity_1 = require("../entities/foto-avance.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 avances de obra
 */
function createAvanceObraController(dataSource) {
    const router = (0, express_1.Router)();
    // Repositorios
    const avanceRepository = dataSource.getRepository(avance_obra_entity_1.AvanceObra);
    const fotoRepository = dataSource.getRepository(foto_avance_entity_1.FotoAvance);
    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 avanceService = new avance_obra_service_1.AvanceObraService(avanceRepository, fotoRepository);
    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 /avances
     * Listar avances 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 = {
                loteId: req.query.loteId,
                departamentoId: req.query.departamentoId,
                conceptoId: req.query.conceptoId,
                status: req.query.status,
                dateFrom: req.query.dateFrom ? new Date(req.query.dateFrom) : undefined,
                dateTo: req.query.dateTo ? new Date(req.query.dateTo) : undefined,
            };
            const result = await avanceService.findWithFilters(getContext(req), filters, page, limit);
            res.status(200).json({
                success: true,
                data: result.data,
                pagination: result.meta,
            });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /avances/accumulated
     * Obtener avance acumulado por concepto
     */
    router.get('/accumulated', 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 loteId = req.query.loteId;
            const departamentoId = req.query.departamentoId;
            const progress = await avanceService.getAccumulatedProgress(getContext(req), loteId, departamentoId);
            res.status(200).json({ success: true, data: progress });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /avances/:id
     * Obtener avance por ID con fotos
     */
    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 avance = await avanceService.findWithFotos(getContext(req), req.params.id);
            if (!avance) {
                res.status(404).json({ error: 'Not Found', message: 'Progress record not found' });
                return;
            }
            res.status(200).json({ success: true, data: avance });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /avances
     * Crear avance (captura)
     */
    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.conceptoId) {
                res.status(400).json({ error: 'Bad Request', message: 'conceptoId is required' });
                return;
            }
            if (!dto.loteId && !dto.departamentoId) {
                res.status(400).json({ error: 'Bad Request', message: 'Either loteId or departamentoId is required' });
                return;
            }
            const avance = await avanceService.createAvance(getContext(req), dto);
            res.status(201).json({ success: true, data: avance });
        }
        catch (error) {
            if (error instanceof Error) {
                res.status(400).json({ error: 'Bad Request', message: error.message });
                return;
            }
            next(error);
        }
    });
    /**
     * POST /avances/:id/fotos
     * Agregar foto al avance
     */
    router.post('/:id/fotos', 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.fileUrl) {
                res.status(400).json({ error: 'Bad Request', message: 'fileUrl is required' });
                return;
            }
            const foto = await avanceService.addFoto(getContext(req), req.params.id, dto);
            res.status(201).json({ success: true, data: foto });
        }
        catch (error) {
            if (error instanceof Error && error.message === 'Avance not found') {
                res.status(404).json({ error: 'Not Found', message: error.message });
                return;
            }
            next(error);
        }
    });
    /**
     * POST /avances/:id/review
     * Revisar avance
     */
    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 avance = await avanceService.review(getContext(req), req.params.id);
            if (!avance) {
                res.status(400).json({ error: 'Bad Request', message: 'Cannot review this progress record. It may not exist or is not in captured status.' });
                return;
            }
            res.status(200).json({ success: true, data: avance, message: 'Progress reviewed' });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /avances/:id/approve
     * Aprobar avance
     */
    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 avance = await avanceService.approve(getContext(req), req.params.id);
            if (!avance) {
                res.status(400).json({ error: 'Bad Request', message: 'Cannot approve this progress record. It may not exist or is not in reviewed status.' });
                return;
            }
            res.status(200).json({ success: true, data: avance, message: 'Progress approved' });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /avances/:id/reject
     * Rechazar avance
     */
    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 avance = await avanceService.reject(getContext(req), req.params.id, reason);
            if (!avance) {
                res.status(400).json({ error: 'Bad Request', message: 'Cannot reject this progress record.' });
                return;
            }
            res.status(200).json({ success: true, data: avance, message: 'Progress rejected' });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * DELETE /avances/:id
     * Eliminar avance (soft delete)
     */
    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 deleted = await avanceService.softDelete(getContext(req), req.params.id);
            if (!deleted) {
                res.status(404).json({ error: 'Not Found', message: 'Progress record not found' });
                return;
            }
            res.status(200).json({ success: true, message: 'Progress record deleted' });
        }
        catch (error) {
            next(error);
        }
    });
    return router;
}
exports.default = createAvanceObraController;
//# sourceMappingURL=avance-obra.controller.js.map