import 'dart:convert'; import 'package:http/http.dart' as http; import '../config/api_config.dart'; import '../models/lesson.dart'; /// Service per le lezioni della scuola YogaSoul. /// Chiama `public/api/my_lessons.php` (phpApiBase), stesso pattern di SettingsApi. class LessonsApi { static Map _headers(String token) => { 'Accept': 'application/json', 'Authorization': 'Bearer $token', }; /// Carica le lezioni del mese indicato. /// [month] deve essere nel formato "YYYY-MM". Se null, il server usa il mese corrente. static Future fetchMyLessons({ required String token, String? month, }) async { final query = (month != null && month.isNotEmpty) ? '?month=$month' : ''; final uri = Uri.parse('${ApiConfig.phpApiBase}/my_lessons.php$query'); final res = await http.get(uri, headers: _headers(token)); Map data; try { data = jsonDecode(res.body) as Map; } catch (_) { throw Exception('Risposta non valida dal server (${res.statusCode})'); } if (res.statusCode != 200 || data['success'] != true) { throw Exception(data['error'] ?? data['message'] ?? 'Errore lezioni'); } return LessonsResponse.fromJson(data); } }