import 'package:glowwheels/models/serviceboy_model.dart';

class Order {
  final String id;
  final User user;
  final Shop shop;
  final Service service;
  final String? address;
  final String date;
  final String timeSlot;
  final String status;
  final String? serviceBoyId;
  final DateTime createdAt;
  final DateTime updatedAt;
  final ServiceBoy? assignedServiceBoy;

  Order({
    required this.id,
    required this.user,
    required this.shop,
    required this.service,
    required this.address,
    required this.date,
    required this.timeSlot,
    required this.status,
    required this.serviceBoyId,
    required this.createdAt,
    required this.updatedAt,
    this.assignedServiceBoy,
  });

  factory Order.fromJson(Map<String, dynamic> json) {
    return Order(
      id: json['_id'],
      user: User.fromJson(json['userId']),
      shop: Shop.fromJson(json['shopId']),
      service: Service.fromJson(json['serviceId']),
      address : json['address'],
      date: json['date'],
      timeSlot: json['timeSlot'],
      status: json['status'],
      serviceBoyId: json['serviceBoyId'],
      createdAt: DateTime.parse(json['createdAt']),
      updatedAt: DateTime.parse(json['updatedAt']),
      assignedServiceBoy: json['assignedServiceBoy'] != null
          ? ServiceBoy.fromJson(json['assignedServiceBoy'])
          : null,
    );
  }

  Order copyWith({
    String? id,
    User? user,
    Shop? shop,
    Service? service,
    String? date,
    String? timeSlot,
    String? status,
    String? serviceBoyId,
    DateTime? createdAt,
    DateTime? updatedAt,
    ServiceBoy? assignedServiceBoy,
  }) {
    return Order(
      id: id ?? this.id,
      user: user ?? this.user,
      shop: shop ?? this.shop,
      service: service ?? this.service,
      date: date ?? this.date,
      timeSlot: timeSlot ?? this.timeSlot,
      status: status ?? this.status,
      serviceBoyId: serviceBoyId ?? this.serviceBoyId,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      assignedServiceBoy: assignedServiceBoy ?? this.assignedServiceBoy, address: '',
    );
  }
}

class User {
  final String id;
  final String name;
  final int phone;

  User({
    required this.id,
    required this.name,
    required this.phone,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['_id'],
      name: json['name'],
      phone: json['phone'],
    );
  }
}

class Shop {
  final String id;
  final String name;
  final String address;
  final double latitude;
  final double longitude;
  final String phone;
  final String email;
  final List<String> images;
  final int rating;

  Shop({
    required this.id,
    required this.name,
    required this.address,
    required this.latitude,
    required this.longitude,
    required this.phone,
    required this.email,
    required this.images,
    required this.rating,
  });

  factory Shop.fromJson(Map<String, dynamic> json) {
    return Shop(
      id: json['_id'],
      name: json['name'],
      address: json['address'],
      latitude: json['latitude'].toDouble(),
      longitude: json['longitude'].toDouble(),
      phone: json['phone'],
      email: json['email'],
      images: List<String>.from(json['images']),
      rating: json['rating'],
    );
  }
}

class Service {
  final String id;
  final Vehicle vehicle;
  final Manufacture manufacture;
  final Model model;
  final String serviceName;
  final int price;
  final String serviceType;

  Service({
    required this.id,
    required this.vehicle,
    required this.manufacture,
    required this.model,
    required this.serviceName,
    required this.price,
    required this.serviceType,
  });

  factory Service.fromJson(Map<String, dynamic> json) {
    return Service(
      id: json['_id'],
      vehicle: Vehicle.fromJson(json['vehicleId']),
      manufacture: Manufacture.fromJson(json['manufactureId']),
      model: Model.fromJson(json['modelId']),
      serviceName: json['serviceName'],
      price: json['price'],
      serviceType: json['serviceType'],
    );
  }
}

class Vehicle {
  final String id;
  final String vehicle;
  final String image;

  Vehicle({
    required this.id,
    required this.vehicle,
    required this.image,
  });

  factory Vehicle.fromJson(Map<String, dynamic> json) {
    return Vehicle(
      id: json['_id'],
      vehicle: json['vehicle'],
      image: json['image'],
    );
  }
}

class Manufacture {
  final String id;
  final String manufacture;
  final String image;

  Manufacture({
    required this.id,
    required this.manufacture,
    required this.image,
  });

  factory Manufacture.fromJson(Map<String, dynamic> json) {
    return Manufacture(
      id: json['_id'],
      manufacture: json['manufacture'],
      image: json['image'],
    );
  }
}

class Model {
  final String id;
  final String model;
  final String image;

  Model({
    required this.id,
    required this.model,
    required this.image,
  });

  factory Model.fromJson(Map<String, dynamic> json) {
    return Model(
      id: json['_id'],
      model: json['model'],
      image: json['image'],
    );
  }
}

