import 'package:uuid/uuid.dart';
import 'package:barber_app/core/database/database_service.dart';
import 'package:barber_app/features/services/data/models/service_model.dart';

class ServiceRepository {
  final _uuid = const Uuid();
  
  String? get _currentUserId => DatabaseService.getCurrentUserId();
  
  // Criar Serviço
  Future<ServiceModel?> createService({
    required String name,
    required double price,
    int durationMinutes = 30,
  }) async {
    if (_currentUserId == null) return null;
    
    final service = ServiceModel(
      id: _uuid.v4(),
      name: name,
      price: price,
      durationMinutes: durationMinutes,
      userId: _currentUserId!,
    );
    
    // ATENÇÃO: Precisamos adicionar servicesBoxInstance no DatabaseService
    await DatabaseService.servicesBoxInstance.put(service.id, service);
    return service;
  }
  
  // Editar
  Future<void> updateService(ServiceModel service) async {
    await DatabaseService.servicesBoxInstance.put(service.id, service);
  }
  
  // Deletar
  Future<void> deleteService(String id) async {
    await DatabaseService.servicesBoxInstance.delete(id);
  }
  
  // Listar Todos
  List<ServiceModel> getAllServices() {
    if (_currentUserId == null) return [];
    
    return DatabaseService.servicesBoxInstance.values
        .where((s) => s.userId == _currentUserId)
        .toList()
      ..sort((a, b) => a.name.compareTo(b.name));
  }
}
