/**
 * TicketController - REST API for post-sale tickets
 *
 * Endpoints para gestión de tickets de garantía.
 *
 * @module Quality
 * @routes /api/tickets
 */

import { Router, Request, Response, NextFunction } from 'express';
import { DataSource } from 'typeorm';
import { TicketService, TicketFilters } from '../services/ticket.service';
import { PostSaleTicket } from '../entities/post-sale-ticket.entity';
import { TicketAssignment } from '../entities/ticket-assignment.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';
import { ServiceContext } from '../../../shared/services/base.service';

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

  // Repositories
  const ticketRepo = dataSource.getRepository(PostSaleTicket);
  const assignmentRepo = dataSource.getRepository(TicketAssignment);
  const userRepository = dataSource.getRepository(User);
  const tenantRepository = dataSource.getRepository(Tenant);
  const refreshTokenRepository = dataSource.getRepository(RefreshToken);

  // Services
  const service = new TicketService(ticketRepo, assignmentRepo);
  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/tickets
   * List tickets 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: TicketFilters = {};
      if (req.query.loteId) filters.loteId = req.query.loteId as string;
      if (req.query.derechohabienteId) filters.derechohabienteId = req.query.derechohabienteId as string;
      if (req.query.category) filters.category = req.query.category as TicketFilters['category'];
      if (req.query.priority) filters.priority = req.query.priority as TicketFilters['priority'];
      if (req.query.status) filters.status = req.query.status as TicketFilters['status'];
      if (req.query.slaBreached) filters.slaBreached = req.query.slaBreached === 'true';
      if (req.query.assignedTo) filters.assignedTo = req.query.assignedTo 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);

      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: result.meta });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /api/tickets/sla-stats
   * Get SLA statistics
   */
  router.get('/sla-stats', 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 stats = await service.getSlaStats(getContext(req));
      res.status(200).json({ success: true, data: stats });
    } catch (error) {
      next(error);
    }
  });

  /**
   * GET /api/tickets/:id
   * Get ticket with 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 ticket = await service.findWithDetails(getContext(req), req.params.id);
      if (!ticket) {
        res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
        return;
      }

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

  /**
   * POST /api/tickets
   * Create new ticket
   */
  router.post('/', 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 ticket = await service.create(getContext(req), {
        loteId: req.body.loteId,
        derechohabienteId: req.body.derechohabienteId,
        category: req.body.category,
        title: req.body.title,
        description: req.body.description,
        photoUrl: req.body.photoUrl,
        contactName: req.body.contactName,
        contactPhone: req.body.contactPhone,
      });

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

  /**
   * POST /api/tickets/:id/assign
   * Assign ticket to technician
   */
  router.post('/:id/assign', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa'), 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 ticket = await service.assign(getContext(req), req.params.id, {
        technicianId: req.body.technicianId,
        scheduledDate: req.body.scheduledDate ? new Date(req.body.scheduledDate) : undefined,
        scheduledTime: req.body.scheduledTime,
      });

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

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

  /**
   * POST /api/tickets/:id/start
   * Start work on ticket
   */
  router.post('/:id/start', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa', 'technician'), 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 ticket = await service.startWork(getContext(req), req.params.id);
      if (!ticket) {
        res.status(404).json({ error: 'Not Found', message: 'Ticket not found' });
        return;
      }

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

  /**
   * POST /api/tickets/:id/resolve
   * Resolve ticket
   */
  router.post('/:id/resolve', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality', 'postventa', 'technician'), 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 ticket = await service.resolve(getContext(req), req.params.id, {
        resolutionNotes: req.body.resolutionNotes,
        resolutionPhotoUrl: req.body.resolutionPhotoUrl,
      });

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

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

  /**
   * POST /api/tickets/:id/close
   * Close ticket with satisfaction rating
   */
  router.post('/:id/close', 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 ticket = await service.close(
        getContext(req),
        req.params.id,
        req.body.rating,
        req.body.comment
      );

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

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

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

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

  /**
   * POST /api/tickets/check-sla
   * Check and update SLA breaches
   */
  router.post('/check-sla', authMiddleware.authenticate, authMiddleware.authorize('admin', 'quality'), 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 breached = await service.checkSlaBreaches(getContext(req));
      res.status(200).json({ success: true, data: { breachedTickets: breached } });
    } catch (error) {
      next(error);
    }
  });

  return router;
}
