import '../models/auth_models.dart';
import '../storage/token_storage.dart';
import 'api_client.dart';

class AuthService {
  final _client = ApiClient.instance;

  // Response after envelope unwrap: { token, employee: { id, firstName, ... } }
  Future<({String token, Employee employee})> loginEmployee({
    required String email,
    required String password,
  }) async {
    final res = await _client.post('/auth/employee/login', data: {
      'email': email.trim().toLowerCase(),
      'password': password,
    });
    final data = res.data as Map<String, dynamic>;
    final token = data['token'] as String;
    final employee =
        Employee.fromJson(data['employee'] as Map<String, dynamic>);
    return (token: token, employee: employee);
  }

  // Response after envelope unwrap: { employee: { id, firstName, ... } }
  Future<Employee> getMe() async {
    final res = await _client.get('/auth/employee/me');
    final data = res.data as Map<String, dynamic>;
    return Employee.fromJson(data['employee'] as Map<String, dynamic>);
  }

  Future<Renter> getRenterMe() async {
    final res = await _client.get('/auth/renter/me');
    final data = res.data as Map<String, dynamic>;
    return Renter.fromJson(data);
  }

  Future<void> logout() => TokenStorage.clear();
}
