import { Request, Response } from 'express';
import { z } from 'zod';
import serviceMonitorService from '../services/serviceMonitor.service';
import notificationService from '../services/notification.service';
import pool from '../config/database';
import logger from '../utils/logger';

const createServiceSchema = z.object({
  name: z.string().min(1).max(200),
  url: z.string().url().max(500),
});

const updateServiceSchema = z.object({
  name: z.string().min(1).max(200).optional(),
  url: z.string().url().max(500).optional(),
  is_active: z.boolean().optional(),
});

const broadcastSchema = z.object({
  titel: z.string().min(1).max(200),
  nachricht: z.string().min(1).max(2000),
  schwere: z.enum(['info', 'warnung', 'fehler']).default('info'),
  targetGroup: z.string().optional(),
  targetDienstgrad: z.array(z.string()).optional(),
});

const broadcastFilterSchema = z.object({
  targetGroup: z.string().optional(),
  targetDienstgrad: z.array(z.string()).optional(),
});

function buildFilteredUserQuery(filters: { targetGroup?: string; targetDienstgrad?: string[] }): { text: string; values: unknown[] } {
  const conditions: string[] = ['u.is_active = TRUE'];
  const values: unknown[] = [];
  let paramIndex = 1;

  if (filters.targetGroup) {
    conditions.push(`$${paramIndex} = ANY(u.authentik_groups)`);
    values.push(filters.targetGroup);
    paramIndex++;
  }

  if (filters.targetDienstgrad && filters.targetDienstgrad.length > 0) {
    conditions.push(`mp.dienstgrad = ANY($${paramIndex})`);
    values.push(filters.targetDienstgrad);
    paramIndex++;
  }

  const needsJoin = filters.targetDienstgrad && filters.targetDienstgrad.length > 0;
  const text = `SELECT DISTINCT u.id FROM users u${needsJoin ? ' LEFT JOIN member_profiles mp ON mp.user_id = u.id' : ''} WHERE ${conditions.join(' AND ')}`;

  return { text, values };
}

class ServiceMonitorController {
  async getAll(_req: Request, res: Response): Promise<void> {
    try {
      const services = await serviceMonitorService.getAllServices();
      res.json({ success: true, data: services });
    } catch (error) {
      logger.error('Failed to get services', { error });
      res.status(500).json({ success: false, message: 'Failed to get services' });
    }
  }

  async create(req: Request, res: Response): Promise<void> {
    try {
      const { name, url } = createServiceSchema.parse(req.body);
      const service = await serviceMonitorService.createService(name, url);
      res.status(201).json({ success: true, data: service });
    } catch (error) {
      if (error instanceof z.ZodError) {
        res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
        return;
      }
      logger.error('Failed to create service', { error });
      res.status(500).json({ success: false, message: 'Failed to create service' });
    }
  }

  async update(req: Request, res: Response): Promise<void> {
    try {
      const data = updateServiceSchema.parse(req.body);
      const service = await serviceMonitorService.updateService(req.params.id as string, data);
      res.json({ success: true, data: service });
    } catch (error) {
      if (error instanceof z.ZodError) {
        res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
        return;
      }
      logger.error('Failed to update service', { error });
      res.status(500).json({ success: false, message: 'Failed to update service' });
    }
  }

  async delete(req: Request, res: Response): Promise<void> {
    try {
      const deleted = await serviceMonitorService.deleteService(req.params.id as string);
      if (!deleted) {
        res.status(404).json({ success: false, message: 'Service not found or is internal' });
        return;
      }
      res.json({ success: true });
    } catch (error) {
      logger.error('Failed to delete service', { error });
      res.status(500).json({ success: false, message: 'Failed to delete service' });
    }
  }

  async pingAll(_req: Request, res: Response): Promise<void> {
    try {
      const results = await serviceMonitorService.pingAll();
      res.json({ success: true, data: results });
    } catch (error) {
      logger.error('Failed to ping services', { error });
      res.status(500).json({ success: false, message: 'Failed to ping services' });
    }
  }

  async getStatusSummary(_req: Request, res: Response): Promise<void> {
    try {
      const summary = await serviceMonitorService.getStatusSummary();
      res.json({ success: true, data: summary });
    } catch (error) {
      logger.error('Failed to get status summary', { error });
      res.status(500).json({ success: false, message: 'Failed to get status summary' });
    }
  }

  async getSystemHealth(_req: Request, res: Response): Promise<void> {
    try {
      let dbStatus = false;
      let dbSize = '0';

      try {
        await pool.query('SELECT 1');
        dbStatus = true;
        const sizeResult = await pool.query('SELECT pg_database_size(current_database()) as size');
        dbSize = sizeResult.rows[0].size;
      } catch {
        // DB is down
      }

      const mem = process.memoryUsage();
      res.json({
        success: true,
        data: {
          nodeVersion: process.version,
          uptime: process.uptime(),
          memoryUsage: {
            heapUsed: mem.heapUsed,
            heapTotal: mem.heapTotal,
            rss: mem.rss,
            external: mem.external,
          },
          dbStatus,
          dbSize,
        },
      });
    } catch (error) {
      logger.error('Failed to get system health', { error });
      res.status(500).json({ success: false, message: 'Failed to get system health' });
    }
  }

  async getUsers(_req: Request, res: Response): Promise<void> {
    try {
      const result = await pool.query(
        `SELECT id, email, name, authentik_groups as groups, is_active, last_login_at
         FROM users ORDER BY name`
      );
      res.json({ success: true, data: result.rows });
    } catch (error) {
      logger.error('Failed to get users', { error });
      res.status(500).json({ success: false, message: 'Failed to get users' });
    }
  }

  async getPingHistory(req: Request, res: Response): Promise<void> {
    try {
      const { serviceId } = req.params;
      const data = await serviceMonitorService.getPingHistory(serviceId as string);
      res.json({ success: true, data });
    } catch (error) {
      logger.error('Failed to get ping history', { error });
      res.status(500).json({ success: false, message: 'Failed to get ping history' });
    }
  }

  async broadcastNotification(req: Request, res: Response): Promise<void> {
    try {
      const { titel, nachricht, schwere, targetGroup, targetDienstgrad } = broadcastSchema.parse(req.body);

      const query = buildFilteredUserQuery({ targetGroup, targetDienstgrad });
      const result = await pool.query(query.text, query.values);
      const users = result.rows;

      let sent = 0;
      for (const user of users) {
        await notificationService.createNotification({
          user_id: user.id,
          typ: 'broadcast',
          titel,
          nachricht,
          schwere,
        });
        sent++;
      }

      res.json({ success: true, data: { sent } });
    } catch (error) {
      if (error instanceof z.ZodError) {
        res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
        return;
      }
      logger.error('Failed to broadcast notification', { error });
      res.status(500).json({ success: false, message: 'Failed to broadcast notification' });
    }
  }

  async broadcastPreview(req: Request, res: Response): Promise<void> {
    try {
      const { targetGroup, targetDienstgrad } = broadcastFilterSchema.parse(req.body);

      const query = buildFilteredUserQuery({ targetGroup, targetDienstgrad });
      const result = await pool.query(query.text, query.values);

      res.json({ success: true, data: { count: result.rows.length } });
    } catch (error) {
      if (error instanceof z.ZodError) {
        res.status(400).json({ success: false, message: 'Invalid input', errors: error.issues });
        return;
      }
      logger.error('Failed to preview broadcast', { error });
      res.status(500).json({ success: false, message: 'Failed to preview broadcast' });
    }
  }
}

export default new ServiceMonitorController();
