import '../models/reservation.dart';
import 'api_client.dart';

class ReservationInspection {
  final String id;
  final String type; // CHECKIN | CHECKOUT
  final int? mileage;
  final String? fuelLevel;
  final String? generalCondition;
  final String? employeeNotes;
  final DateTime createdAt;

  const ReservationInspection({
    required this.id,
    required this.type,
    this.mileage,
    this.fuelLevel,
    this.generalCondition,
    this.employeeNotes,
    required this.createdAt,
  });

  factory ReservationInspection.fromJson(Map<String, dynamic> json) =>
      ReservationInspection(
        id: json['id'] as String,
        type: json['type'] as String,
        mileage: json['mileage'] as int?,
        fuelLevel: json['fuelLevel'] as String?,
        generalCondition: json['generalCondition'] as String?,
        employeeNotes: json['employeeNotes'] as String?,
        createdAt: DateTime.parse(
            json['createdAt'] as String? ?? DateTime.now().toIso8601String()),
      );
}

class ReservationService {
  final _client = ApiClient.instance;

  Future<ReservationListResponse> getReservations({
    int page = 1,
    int pageSize = 20,
    String? status,
    String? source,
    String? search,
    String? customerId,
  }) async {
    final res = await _client.get('/reservations', params: {
      'page': page,
      'pageSize': pageSize,
      'status': ?status,
      'source': ?source,
      if (search != null && search.isNotEmpty) 'search': search,
      'customerId': ?customerId,
    });
    return ReservationListResponse.fromJson(res.data as Map<String, dynamic>);
  }

  Future<Reservation> getReservation(String id) async {
    final res = await _client.get('/reservations/$id');
    return Reservation.fromJson(res.data as Map<String, dynamic>);
  }

  Future<Reservation> createReservation(Map<String, dynamic> data) async {
    final res = await _client.post('/reservations', data: data);
    return Reservation.fromJson(res.data as Map<String, dynamic>);
  }

  Future<void> confirmReservation(String id) async {
    await _client.post('/reservations/$id/confirm');
  }

  Future<void> cancelReservation(String id, String reason) async {
    await _client.post('/reservations/$id/cancel', data: {'reason': reason});
  }

  Future<void> checkin(String id, int mileage) async {
    await _client.post('/reservations/$id/checkin', data: {'mileage': mileage});
  }

  Future<void> checkout(String id, int mileage) async {
    await _client.post('/reservations/$id/checkout', data: {'mileage': mileage});
  }

  Future<Reservation> closeReservation(String id) async {
    final res = await _client.post('/reservations/$id/close');
    return Reservation.fromJson(res.data as Map<String, dynamic>);
  }

  Future<List<ReservationInspection>> getInspections(String id) async {
    final res = await _client.get('/reservations/$id/inspections');
    final list = res.data as List<dynamic>;
    return list
        .map((e) =>
            ReservationInspection.fromJson(e as Map<String, dynamic>))
        .toList();
  }

  Future<ReservationInspection> upsertInspection(
      String id, String type, Map<String, dynamic> data) async {
    final res = await _client.put(
        '/reservations/$id/inspections/$type',
        data: data);
    return ReservationInspection.fromJson(res.data as Map<String, dynamic>);
  }
}
