import 'package:krow_data_connect/krow_data_connect.dart';
import 'package:krow_domain/krow_domain.dart';
import '../../domain/repositories/shifts_repository_interface.dart';

/// Implementation of [ShiftsRepositoryInterface] that delegates to [ShiftsRepositoryMock].
///
/// This class resides in the data layer and handles the communication with 
/// the external data sources (currently mocks).
class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
  final ShiftsRepositoryMock _mock;

  ShiftsRepositoryImpl({ShiftsRepositoryMock? mock}) : _mock = mock ?? ShiftsRepositoryMock();

  @override
  Future<List<Shift>> getMyShifts() async {
    return _mock.getMyShifts();
  }

  @override
  Future<List<Shift>> getAvailableShifts(String query, String type) async {
    // Delegates to mock. Logic kept here temporarily as per architecture constraints
    // on data_connect modifications, mimicking a query capable datasource.
    var shifts = await _mock.getAvailableShifts();
    
    // Simple in-memory filtering for mock adapter
    if (query.isNotEmpty) {
      shifts = shifts.where((s) => 
        s.title.toLowerCase().contains(query.toLowerCase()) || 
        s.clientName.toLowerCase().contains(query.toLowerCase())
      ).toList();
    }
    
    if (type != 'all') {
      if (type == 'one-day') {
        shifts = shifts.where((s) => !s.title.contains('Multi-Day') && !s.title.contains('Long Term')).toList();
      } else if (type == 'multi-day') {
        shifts = shifts.where((s) => s.title.contains('Multi-Day')).toList();
      } else if (type == 'long-term') {
        shifts = shifts.where((s) => s.title.contains('Long Term')).toList();
      }
    }
    
    return shifts;
  }

  @override
  Future<List<Shift>> getPendingAssignments() async {
    return _mock.getPendingAssignments();
  }

  @override
  Future<Shift?> getShiftDetails(String shiftId) async {
    return _mock.getShiftDetails(shiftId);
  }

  @override
  Future<void> applyForShift(String shiftId) async {
    await Future.delayed(const Duration(milliseconds: 500));
  }
  
  @override
  Future<void> acceptShift(String shiftId) async {
    await Future.delayed(const Duration(milliseconds: 500));
  }
  
  @override
  Future<void> declineShift(String shiftId) async {
    await Future.delayed(const Duration(milliseconds: 500));
  }
}
