import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

class TermineController {
    async getTermine(req, res) {
        try {
            const csvPath = path.join(__dirname, '../data/termine.csv');
            const csvContent = fs.readFileSync(csvPath, 'utf-8');
            
            const lines = csvContent.trim().split('\n');
            const headers = lines[0].split(',');
            
            const termine = lines.slice(1).map(line => {
                const values = line.split(',');
                const termin = {};
                headers.forEach((header, index) => {
                    termin[header] = values[index] || '';
                });
                return termin;
            });
            
            // Sortiere nach Datum
            termine.sort((a, b) => new Date(a.datum) - new Date(b.datum));
            
            // Filtere nur zukünftige Termine
            const heute = new Date();
            heute.setHours(0, 0, 0, 0);
            const zukuenftigeTermine = termine.filter(t => new Date(t.datum) >= heute);
            
            res.status(200).json(zukuenftigeTermine);
        } catch (error) {
            console.error('Error reading termine.csv:', error);
            res.status(500).json({ error: 'Could not load termine' });
        }
    }
}

export default new TermineController();

