import 'dart:convert';

class VendorServiceModel {
  final int id;
  final int vendorId;
  final int serviceType;
  final String vendorName;
  final String serviceName;
  final int category;
  final int subcategory;
  final String workingHours;
  final String workingDuration;
  final String amount;
  final String location;
  final String description;
  final String details;
  final String createdDate;
  final String categoryName;
  final String subcategoryName;
  final List<String> video;
  final List<String> images1;

  VendorServiceModel({
    required this.id,
    required this.vendorId,
    required this.serviceType,
    required this.vendorName,
    required this.serviceName,
    required this.category,
    required this.subcategory,
    required this.workingHours,
    required this.workingDuration,
    required this.amount,
    required this.location,
    required this.description,
    required this.details,
    required this.createdDate,
    required this.categoryName,
    required this.subcategoryName,
    required this.video,
    required this.images1,
  });

  factory VendorServiceModel.fromJson(Map<String, dynamic> json) {
    return VendorServiceModel(
      id: json['id'] ?? 0,
      vendorId: json['vendor_id'] ?? 0,
      serviceType: json['service_type'] ?? 0,
      vendorName: json['vendorname'] ?? '',
      serviceName: json['servicename'] ?? '',
      category: json['category'] ?? 0,
      subcategory: json['subcategory'] ?? 0,
      workingHours: json['workinghours'] ?? '',
      workingDuration: json['workingduration'] ?? '',
      amount: json['amount']?.toString() ?? '',
      location: json['location'] ?? '',
      description: json['description'] ?? '',
      details: json['details'] ?? '',
      createdDate: json['created_date'] ?? '',
      categoryName: json['category_name'] ?? '',
      subcategoryName: json['subcategory_name'] ?? '',
      video: _parseStringList(json['videos']),
      images1: _parseStringList(json['images1']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'vendor_id': vendorId,
      'service_type': serviceType,
      'vendorname': vendorName,
      'servicename': serviceName,
      'category': category,
      'subcategory': subcategory,
      'workinghours': workingHours,
      'workingduration': workingDuration,
      'amount': amount,
      'location': location,
      'description': description,
      'details': details,
      'created_date': createdDate,
      'category_name': categoryName,
      'subcategory_name': subcategoryName,
      'videos': video,
      'images1': images1,
    };
  }

  static List<String> _parseStringList(dynamic input) {
    if (input == null) return [];

    if (input is List) {
      return input.map((e) => e.toString()).toList();
    }

    if (input is String) {
      if (input.trim().isEmpty) return [];

      if (input.trim().startsWith('[')) {
        try {
          final parsed = jsonDecode(input);
          if (parsed is List) {
            return parsed.map((e) => e.toString()).toList();
          }
        } catch (_) {}
      }

      if (input.contains(',')) {
        return input
            .split(',')
            .map((e) => e.trim())
            .where((e) => e.isNotEmpty)
            .toList();
      }

      return [input];
    }

    return [];
  }
}
