// lib/services/api_service.dart import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:flutter/material.dart'; import '../models/profile.dart'; import '../models/group.dart'; import 'session_manager.dart'; class ApiService extends ChangeNotifier { static const String baseUrl = 'https://paul-koop.org/api/'; final SessionManager? sessionManager; ApiService({this.sessionManager}); Future> login(String username, String password) async { final response = await http.post( Uri.parse('${baseUrl}api_login.php'), headers: {'Content-Type': 'application/json'}, body: jsonEncode({'username': username, 'password': password}), ); return jsonDecode(response.body); } Future> getProfiles() async { final response = await http.get( Uri.parse('${baseUrl}api_profiles.php'), headers: sessionManager?.authHeaders ?? {}, ); if (response.statusCode == 200) { final List data = jsonDecode(response.body); return data.map((json) => Profile.fromJson(json)).toList(); } throw Exception('Failed to load profiles'); } Future getProfile(String id) async { final response = await http.get( Uri.parse('${baseUrl}api_profiles.php?id=$id'), headers: sessionManager?.authHeaders ?? {}, ); if (response.statusCode == 200) { return Profile.fromJson(jsonDecode(response.body)); } throw Exception('Failed to load profile'); } Future createProfile(Profile profile) async { final response = await http.post( Uri.parse('${baseUrl}api_profiles.php'), headers: { 'Content-Type': 'application/json', ...?sessionManager?.authHeaders, }, body: jsonEncode(profile.toJson()), ); return response.statusCode == 200; } Future updateProfile(Profile profile) async { final response = await http.put( Uri.parse('${baseUrl}api_profiles.php'), headers: { 'Content-Type': 'application/json', 'X-Profile-ID': profile.profilID, ...?sessionManager?.authHeaders, }, body: jsonEncode(profile.toJson()), ); return response.statusCode == 200; } Future deleteProfile(String id) async { final response = await http.delete( Uri.parse('${baseUrl}api_profiles.php?id=$id'), headers: sessionManager?.authHeaders ?? {}, ); return response.statusCode == 200; } Future> getGroups() async { final response = await http.get( Uri.parse('${baseUrl}api_groups.php'), headers: sessionManager?.authHeaders ?? {}, ); if (response.statusCode == 200) { final List data = jsonDecode(response.body); return data.map((json) => Group.fromJson(json)).toList(); } throw Exception('Failed to load groups'); } Future createGroup(String name) async { final response = await http.post( Uri.parse('${baseUrl}api_groups.php'), headers: { 'Content-Type': 'application/json', ...?sessionManager?.authHeaders, }, body: jsonEncode({'name': name}), ); return response.statusCode == 200; } Future deleteGroup(int id) async { final response = await http.delete( Uri.parse('${baseUrl}api_groups.php?id=$id'), headers: sessionManager?.authHeaders ?? {}, ); return response.statusCode == 200; } }