"use strict";
/**
 * LoteController - Controller de lotes
 *
 * Endpoints REST para gestión de lotes/terrenos.
 *
 * @module Construction
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createLoteController = createLoteController;
const express_1 = require("express");
const lote_service_1 = require("../services/lote.service");
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
const auth_service_1 = require("../../auth/services/auth.service");
const lote_entity_1 = require("../entities/lote.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 lotes
 */
function createLoteController(dataSource) {
    const router = (0, express_1.Router)();
    // Repositorios
    const loteRepository = dataSource.getRepository(lote_entity_1.Lote);
    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 loteService = new lote_service_1.LoteService(loteRepository);
    const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
    const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
    /**
     * GET /lotes
     * Listar lotes
     */
    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 search = req.query.search;
            const status = req.query.status;
            const manzanaId = req.query.manzanaId;
            const prototipoId = req.query.prototipoId;
            const result = await loteService.findAll({ tenantId, page, limit, search, status, manzanaId, prototipoId });
            res.status(200).json({
                success: true,
                data: result.items,
                pagination: {
                    page,
                    limit,
                    total: result.total,
                    totalPages: Math.ceil(result.total / limit),
                },
            });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /lotes/stats
     * Estadísticas de lotes por estado
     */
    router.get('/stats', 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 manzanaId = req.query.manzanaId;
            const stats = await loteService.getStatsByStatus(tenantId, manzanaId);
            res.status(200).json({ success: true, data: stats });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /lotes/:id
     * Obtener lote por ID
     */
    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 lote = await loteService.findById(req.params.id, tenantId);
            if (!lote) {
                res.status(404).json({ error: 'Not Found', message: 'Lot not found' });
                return;
            }
            res.status(200).json({ success: true, data: lote });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /lotes
     * Crear lote
     */
    router.post('/', 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 dto = req.body;
            if (!dto.manzanaId || !dto.code) {
                res.status(400).json({ error: 'Bad Request', message: 'manzanaId and code are required' });
                return;
            }
            const lote = await loteService.create(tenantId, dto, req.user?.sub);
            res.status(201).json({ success: true, data: lote });
        }
        catch (error) {
            if (error instanceof Error && error.message.includes('already exists')) {
                res.status(409).json({ error: 'Conflict', message: error.message });
                return;
            }
            next(error);
        }
    });
    /**
     * PATCH /lotes/:id
     * Actualizar lote
     */
    router.patch('/:id', 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 dto = req.body;
            const lote = await loteService.update(req.params.id, tenantId, dto, req.user?.sub);
            res.status(200).json({ success: true, data: lote });
        }
        catch (error) {
            if (error instanceof Error) {
                if (error.message === 'Lot not found') {
                    res.status(404).json({ error: 'Not Found', message: error.message });
                    return;
                }
                if (error.message.includes('already exists')) {
                    res.status(409).json({ error: 'Conflict', message: error.message });
                    return;
                }
            }
            next(error);
        }
    });
    /**
     * PATCH /lotes/:id/prototipo
     * Asignar prototipo a lote
     */
    router.patch('/:id/prototipo', 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 { prototipoId } = req.body;
            if (!prototipoId) {
                res.status(400).json({ error: 'Bad Request', message: 'prototipoId is required' });
                return;
            }
            const lote = await loteService.assignPrototipo(req.params.id, tenantId, prototipoId, req.user?.sub);
            res.status(200).json({ success: true, data: lote });
        }
        catch (error) {
            if (error instanceof Error && error.message === 'Lot not found') {
                res.status(404).json({ error: 'Not Found', message: error.message });
                return;
            }
            next(error);
        }
    });
    /**
     * PATCH /lotes/:id/status
     * Cambiar estado del lote
     */
    router.patch('/:id/status', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'finance'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const { status } = req.body;
            if (!status) {
                res.status(400).json({ error: 'Bad Request', message: 'status is required' });
                return;
            }
            const validStatuses = ['available', 'reserved', 'sold', 'blocked', 'in_construction'];
            if (!validStatuses.includes(status)) {
                res.status(400).json({ error: 'Bad Request', message: `Invalid status. Must be one of: ${validStatuses.join(', ')}` });
                return;
            }
            const lote = await loteService.changeStatus(req.params.id, tenantId, status, req.user?.sub);
            res.status(200).json({ success: true, data: lote });
        }
        catch (error) {
            if (error instanceof Error && error.message === 'Lot not found') {
                res.status(404).json({ error: 'Not Found', message: error.message });
                return;
            }
            next(error);
        }
    });
    /**
     * DELETE /lotes/:id
     * Eliminar lote
     */
    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;
            }
            await loteService.delete(req.params.id, tenantId, req.user?.sub);
            res.status(200).json({ success: true, message: 'Lot deleted' });
        }
        catch (error) {
            if (error instanceof Error) {
                if (error.message === 'Lot not found') {
                    res.status(404).json({ error: 'Not Found', message: error.message });
                    return;
                }
                if (error.message === 'Cannot delete a sold lot') {
                    res.status(400).json({ error: 'Bad Request', message: error.message });
                    return;
                }
            }
            next(error);
        }
    });
    return router;
}
exports.default = createLoteController;
//# sourceMappingURL=lote.controller.js.map