/**
 * ComparativoController - REST API for quotation comparisons
 *
 * Endpoints para gestión de cuadros comparativos de cotizaciones.
 *
 * @module Purchase
 * @routes /api/comparativos
 */

import { Router, Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import { ComparativoService, ComparativoFilters } from '../services/comparativo.service';
import { ComparativoCotizaciones } from '../entities/comparativo-cotizaciones.entity';
import { ComparativoProveedor } from '../entities/comparativo-proveedor.entity';
import { ComparativoProducto } from '../entities/comparativo-producto.entity';
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';

interface ServiceContext {
  tenantId: string;
  userId?: string;
}

export function createComparativoController(dataSource: DataSource): Router {
  const router = Router();

  // Repositories
  const comparativoRepo = dataSource.getRepository(ComparativoCotizaciones);
  const proveedorRepo = dataSource.getRepository(ComparativoProveedor);
  const productoRepo = dataSource.getRepository(ComparativoProducto);
  const userRepository = dataSource.getRepository(User);
  const tenantRepository = dataSource.getRepository(Tenant);
  const refreshTokenRepository = dataSource.getRepository(RefreshToken);

  // Services
  const service = new ComparativoService(comparativoRepo, proveedorRepo, productoRepo);
  const authService = new AuthService(userRepository, tenantRepository, refreshTokenRepository as any);
  const authMiddleware = new AuthMiddleware(authService, dataSource);

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

  /**
   * GET /api/comparativos
   * List comparisons with filters
   */
  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 filters: ComparativoFilters = {};
      if (req.query.requisicionId) {
        filters.requisicionId = req.query.requisicionId as string;
      }
      if (req.query.status) {
        filters.status = req.query.status as ComparativoFilters['status'];
      }
      if (req.query.dateFrom) {
        filters.dateFrom = new Date(req.query.dateFrom as string);
      }
      if (req.query.dateTo) {
        filters.dateTo = new Date(req.query.dateTo as string);
      }

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

      const result = await service.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 /api/comparativos/:id
   * Get comparison with full details
   */
  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 comparativo = await service.findWithDetails(getContext(req), req.params.id);
      if (!comparativo) {
        res.status(404).json({ error: 'Not Found', message: 'Comparison not found' });
        return;
      }

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

  /**
   * POST /api/comparativos
   * Create new comparison
   */
  router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 comparativo = await service.create(getContext(req), {
        requisicionId: req.body.requisicionId,
        name: req.body.name,
        comparisonDate: new Date(req.body.comparisonDate),
        notes: req.body.notes,
      });

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

  /**
   * POST /api/comparativos/:id/proveedores
   * Add supplier to comparison
   */
  router.post('/:id/proveedores', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 proveedor = await service.addProveedor(getContext(req), req.params.id, {
        supplierId: req.body.supplierId,
        quotationNumber: req.body.quotationNumber,
        quotationDate: req.body.quotationDate ? new Date(req.body.quotationDate) : undefined,
        deliveryDays: req.body.deliveryDays,
        paymentConditions: req.body.paymentConditions,
        evaluationNotes: req.body.evaluationNotes,
      });

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

  /**
   * POST /api/comparativos/proveedores/:proveedorId/productos
   * Add product to supplier entry
   */
  router.post('/proveedores/:proveedorId/productos', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 producto = await service.addProducto(getContext(req), req.params.proveedorId, {
        productId: req.body.productId,
        quantity: parseFloat(req.body.quantity),
        unitPrice: parseFloat(req.body.unitPrice),
        notes: req.body.notes,
      });

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

  /**
   * PUT /api/comparativos/proveedores/:proveedorId/total
   * Recalculate supplier total
   */
  router.put('/proveedores/:proveedorId/total', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 proveedor = await service.updateProveedorTotal(getContext(req), req.params.proveedorId);
      if (!proveedor) {
        res.status(404).json({ error: 'Not Found', message: 'Supplier entry not found' });
        return;
      }

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

  /**
   * POST /api/comparativos/:id/start-evaluation
   * Start evaluation process
   */
  router.post('/:id/start-evaluation', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 comparativo = await service.startEvaluation(getContext(req), req.params.id);
      if (!comparativo) {
        res.status(404).json({ error: 'Not Found', message: 'Comparison not found' });
        return;
      }

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

  /**
   * POST /api/comparativos/:id/select-winner
   * Select winning supplier
   */
  router.post('/:id/select-winner', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 comparativo = await service.selectWinner(getContext(req), req.params.id, req.body.supplierId);
      if (!comparativo) {
        res.status(404).json({ error: 'Not Found', message: 'Comparison not found' });
        return;
      }

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

  /**
   * POST /api/comparativos/:id/cancel
   * Cancel comparison
   */
  router.post('/:id/cancel', authMiddleware.authenticate, authMiddleware.authorize('admin', 'compras'), 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 comparativo = await service.cancel(getContext(req), req.params.id);
      if (!comparativo) {
        res.status(404).json({ error: 'Not Found', message: 'Comparison not found' });
        return;
      }

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

  /**
   * DELETE /api/comparativos/:id
   * Soft delete comparison
   */
  router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin'), 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 deleted = await service.softDelete(getContext(req), req.params.id);
      if (!deleted) {
        res.status(404).json({ error: 'Not Found', message: 'Comparison not found' });
        return;
      }

      res.status(204).send();
    } catch (error) {
      next(error);
    }
  });

  return router;
}
