/**
 * OpportunityController - Controller de Oportunidades
 *
 * Endpoints REST para gestion del pipeline de oportunidades.
 *
 * @module Bidding
 */

import { Router, Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import { OpportunityService, CreateOpportunityDto, UpdateOpportunityDto, OpportunityFilters, GoNoGoDecisionDto } from '../services/opportunity.service';
import { AuthMiddleware } from '../../auth/middleware/auth.middleware';
import { AuthService } from '../../auth/services/auth.service';
import { Opportunity, OpportunityStatus } from '../entities/opportunity.entity';
import { User } from '../../core/entities/user.entity';
import { Tenant } from '../../core/entities/tenant.entity';
import { RefreshToken } from '../../auth/entities/refresh-token.entity';
import { ServiceContext } from '../../../shared/services/base.service';

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

  // Repositorios
  const opportunityRepository = dataSource.getRepository(Opportunity);
  const userRepository = dataSource.getRepository(User);
  const tenantRepository = dataSource.getRepository(Tenant);
  const refreshTokenRepository = dataSource.getRepository(RefreshToken);

  // Servicios
  const opportunityService = new OpportunityService(opportunityRepository);
  const authService = new AuthService(userRepository, tenantRepository, refreshTokenRepository as any);
  const authMiddleware = new AuthMiddleware(authService, dataSource);

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

  /**
   * GET /opportunities
   */
  router.get('/', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

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

      if (req.query.status) {
        const statuses = (req.query.status as string).split(',') as OpportunityStatus[];
        filters.status = statuses.length === 1 ? statuses[0] : statuses;
      }
      if (req.query.source) filters.source = req.query.source as any;
      if (req.query.projectType) filters.projectType = req.query.projectType as string;
      if (req.query.priority) filters.priority = req.query.priority as any;
      if (req.query.clientName) filters.clientName = req.query.clientName as string;
      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);
      if (req.query.minValue) filters.minValue = parseFloat(req.query.minValue as string);
      if (req.query.maxValue) filters.maxValue = parseFloat(req.query.maxValue as string);
      if (req.query.search) filters.search = req.query.search as string;

      const result = await opportunityService.findAll(getContext(req), filters);

      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 /opportunities/pipeline
   */
  router.get('/pipeline', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const pipeline = await opportunityService.getPipeline(getContext(req));
      res.status(200).json({ success: true, data: pipeline });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /opportunities/upcoming-deadlines
   */
  router.get('/upcoming-deadlines', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const days = parseInt(req.query.days as string) || 7;
      const opportunities = await opportunityService.getUpcomingDeadlines(getContext(req), days);
      res.status(200).json({ success: true, data: opportunities });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /opportunities/stats
   * Get statistics
   */
  router.get('/stats', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const year = req.query.year ? parseInt(req.query.year as string) : undefined;
      const stats = await opportunityService.getStats(getContext(req), year);
      res.status(200).json({ success: true, data: stats });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /opportunities/code/:code
   * Get opportunity by code
   */
  router.get('/code/:code', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const opportunity = await opportunityService.findByCode(getContext(req), req.params.code);
      if (!opportunity) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

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

  /**
   * GET /opportunities/:id
   * Get opportunity by ID
   */
  router.get('/:id', authMiddleware.authenticate, async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const opportunity = await opportunityService.findById(getContext(req), req.params.id);
      if (!opportunity) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

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

  /**
   * POST /opportunities
   */
  router.post('/', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: CreateOpportunityDto = req.body;
      if (!dto.title || !dto.source || !dto.projectType || !dto.clientName || !dto.deadlineDate) {
        res.status(400).json({
          error: 'Bad Request',
          message: 'title, source, projectType, clientName, and deadlineDate are required',
        });
        return;
      }

      const opportunity = await opportunityService.create(getContext(req), dto);
      res.status(201).json({ success: true, data: opportunity });
    } catch (error) {
      next(error);
    }
  });

  /**
   * PUT /opportunities/:id
   * Full update opportunity
   */
  router.put('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: UpdateOpportunityDto = req.body;
      const opportunity = await opportunityService.update(getContext(req), req.params.id, dto);

      if (!opportunity) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

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

  /**
   * PATCH /opportunities/:id
   * Partial update opportunity
   */
  router.patch('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const dto: UpdateOpportunityDto = req.body;
      const opportunity = await opportunityService.update(getContext(req), req.params.id, dto);

      if (!opportunity) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

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

  /**
   * POST /opportunities/:id/evaluate
   * Evaluate Go/No-Go decision
   */
  router.post('/:id/evaluate', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const { decision, reason } = req.body;
      if (!decision || !['go', 'no_go'].includes(decision)) {
        res.status(400).json({ error: 'Bad Request', message: 'decision is required and must be go or no_go' });
        return;
      }

      if (!reason) {
        res.status(400).json({ error: 'Bad Request', message: 'reason is required' });
        return;
      }

      const dto: GoNoGoDecisionDto = { decision, reason };
      const opportunity = await opportunityService.evaluateGoNoGo(getContext(req), req.params.id, dto);

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

  /**
   * POST /opportunities/:id/status
   * Change opportunity status
   */
  router.post('/:id/status', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director', 'gerente', 'comercial'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const { status, reason } = req.body;
      if (!status) {
        res.status(400).json({ error: 'Bad Request', message: 'status is required' });
        return;
      }

      const opportunity = await opportunityService.changeStatus(getContext(req), req.params.id, status, reason);

      if (!opportunity) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

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

  /**
   * DELETE /opportunities/:id
   */
  router.delete('/:id', authMiddleware.authenticate, authMiddleware.authorize('admin', 'director'), async (req: Request, res: Response, next: NextFunction): Promise<void> => {
    try {
      if (!req.tenantId) {
        res.status(400).json({ error: 'Bad Request', message: 'Tenant ID required' });
        return;
      }

      const deleted = await opportunityService.softDelete(getContext(req), req.params.id);
      if (!deleted) {
        res.status(404).json({ error: 'Not Found', message: 'Opportunity not found' });
        return;
      }

      res.status(200).json({ success: true, message: 'Opportunity deleted' });
    } catch (error) {
      next(error);
    }
  });

  return router;
}

export default createOpportunityController;
