"use strict";
/**
 * ContractController - REST API for contracts
 *
 * Endpoints para gestión de contratos.
 *
 * @module Contracts
 * @routes /api/contracts
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.createContractController = createContractController;
const express_1 = require("express");
const contract_service_1 = require("../services/contract.service");
const contract_entity_1 = require("../entities/contract.entity");
const contract_addendum_entity_1 = require("../entities/contract-addendum.entity");
const auth_middleware_1 = require("../../auth/middleware/auth.middleware");
const auth_service_1 = require("../../auth/services/auth.service");
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");
function createContractController(dataSource) {
    const router = (0, express_1.Router)();
    // Repositories
    const contractRepo = dataSource.getRepository(contract_entity_1.Contract);
    const addendumRepo = dataSource.getRepository(contract_addendum_entity_1.ContractAddendum);
    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);
    // Services
    const service = new contract_service_1.ContractService(contractRepo, addendumRepo);
    const authService = new auth_service_1.AuthService(userRepository, tenantRepository, refreshTokenRepository);
    const authMiddleware = new auth_middleware_1.AuthMiddleware(authService, dataSource);
    // Helper for service context
    const getContext = (req) => {
        if (!req.tenantId) {
            throw new Error('Tenant ID is required');
        }
        return {
            tenantId: req.tenantId,
            userId: req.user?.sub,
        };
    };
    /**
     * GET /api/contracts
     * List contracts with filters
     */
    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 filters = {};
            if (req.query.projectId)
                filters.projectId = req.query.projectId;
            if (req.query.fraccionamientoId)
                filters.fraccionamientoId = req.query.fraccionamientoId;
            if (req.query.contractType)
                filters.contractType = req.query.contractType;
            if (req.query.subcontractorId)
                filters.subcontractorId = req.query.subcontractorId;
            if (req.query.status)
                filters.status = req.query.status;
            if (req.query.expiringInDays)
                filters.expiringInDays = parseInt(req.query.expiringInDays);
            const page = parseInt(req.query.page) || 1;
            const limit = Math.min(parseInt(req.query.limit) || 20, 100);
            const result = await service.findWithFilters(getContext(req), filters, page, limit);
            res.status(200).json({ success: true, data: result.data, pagination: result.meta });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /api/contracts/expiring
     * Get contracts expiring soon
     */
    router.get('/expiring', 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 days = parseInt(req.query.days) || 30;
            const contracts = await service.getExpiringContracts(getContext(req), days);
            res.status(200).json({ success: true, data: contracts });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * GET /api/contracts/:id
     * Get contract with details
     */
    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 contract = await service.findWithDetails(getContext(req), req.params.id);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts
     * Create new contract
     */
    router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', 'contracts'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const contract = await service.create(getContext(req), {
                projectId: req.body.projectId,
                fraccionamientoId: req.body.fraccionamientoId,
                contractType: req.body.contractType,
                clientContractType: req.body.clientContractType,
                name: req.body.name,
                description: req.body.description,
                clientName: req.body.clientName,
                clientRfc: req.body.clientRfc,
                clientAddress: req.body.clientAddress,
                subcontractorId: req.body.subcontractorId,
                specialty: req.body.specialty,
                startDate: new Date(req.body.startDate),
                endDate: new Date(req.body.endDate),
                contractAmount: req.body.contractAmount,
                currency: req.body.currency,
                paymentTerms: req.body.paymentTerms,
                retentionPercentage: req.body.retentionPercentage,
                advancePercentage: req.body.advancePercentage,
                notes: req.body.notes,
            });
            res.status(201).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/submit
     * Submit contract for review
     */
    router.post('/:id/submit', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', 'contracts'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const contract = await service.submitForReview(getContext(req), req.params.id);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/approve-legal
     * Legal approval
     */
    router.post('/:id/approve-legal', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const contract = await service.approveLegal(getContext(req), req.params.id);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/approve
     * Final approval
     */
    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 contract = await service.approve(getContext(req), req.params.id);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/activate
     * Activate signed contract
     */
    router.post('/:id/activate', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', 'contracts'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const contract = await service.activate(getContext(req), req.params.id, req.body.signedDocumentUrl);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/complete
     * Mark contract as completed
     */
    router.post('/:id/complete', authMiddleware.authenticate, authMiddleware.authorize('admin', 'contracts'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const contract = await service.complete(getContext(req), req.params.id);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/terminate
     * Terminate contract
     */
    router.post('/:id/terminate', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', '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 contract = await service.terminate(getContext(req), req.params.id, req.body.reason);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * PUT /api/contracts/:id/progress
     * Update contract progress
     */
    router.put('/:id/progress', authMiddleware.authenticate, authMiddleware.authorize('admin', 'contracts', '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 contract = await service.updateProgress(getContext(req), req.params.id, req.body.progressPercentage, req.body.invoicedAmount, req.body.paidAmount);
            if (!contract) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(200).json({ success: true, data: contract });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/:id/addendums
     * Create contract addendum
     */
    router.post('/:id/addendums', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', 'contracts'), async (req, res, next) => {
        try {
            const tenantId = req.tenantId;
            if (!tenantId) {
                res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
                return;
            }
            const addendum = await service.createAddendum(getContext(req), req.params.id, {
                addendumType: req.body.addendumType,
                title: req.body.title,
                description: req.body.description,
                effectiveDate: new Date(req.body.effectiveDate),
                newEndDate: req.body.newEndDate ? new Date(req.body.newEndDate) : undefined,
                amountChange: req.body.amountChange,
                scopeChanges: req.body.scopeChanges,
                notes: req.body.notes,
            });
            res.status(201).json({ success: true, data: addendum });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * POST /api/contracts/addendums/:addendumId/approve
     * Approve addendum
     */
    router.post('/addendums/:addendumId/approve', authMiddleware.authenticate, authMiddleware.authorize('admin', 'legal', '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 addendum = await service.approveAddendum(getContext(req), req.params.addendumId);
            if (!addendum) {
                res.status(404).json({ error: 'Not Found', message: 'Addendum not found' });
                return;
            }
            res.status(200).json({ success: true, data: addendum });
        }
        catch (error) {
            next(error);
        }
    });
    /**
     * DELETE /api/contracts/:id
     * Soft delete contract
     */
    router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin'), 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 service.softDelete(getContext(req), req.params.id);
            if (!deleted) {
                res.status(404).json({ error: 'Not Found', message: 'Contract not found' });
                return;
            }
            res.status(204).send();
        }
        catch (error) {
            next(error);
        }
    });
    return router;
}
//# sourceMappingURL=contract.controller.js.map