import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';

import 'package:staff_home/src/domain/entities/shift.dart';
import 'package:staff_home/src/domain/usecases/get_home_shifts.dart';
import 'package:staff_home/src/domain/repositories/home_repository.dart';

part 'home_state.dart';

/// Simple Cubit to manage home page state (shifts + loading/error).
class HomeCubit extends Cubit<HomeState> {
  final GetHomeShifts _getHomeShifts;

  HomeCubit(HomeRepository repository)
    : _getHomeShifts = GetHomeShifts(repository),
      super(const HomeState.initial());

  Future<void> loadShifts() async {
    emit(state.copyWith(status: HomeStatus.loading));
    try {
      final result = await _getHomeShifts.call();
      emit(
        state.copyWith(
          status: HomeStatus.loaded,
          todayShifts: result.today,
          tomorrowShifts: result.tomorrow,
          recommendedShifts: result.recommended,
          // Mock profile status for now, ideally fetched from a user repository
          isProfileComplete: false,
        ),
      );
    } catch (e) {
      emit(
        state.copyWith(status: HomeStatus.error, errorMessage: e.toString()),
      );
    }
  }

  void toggleAutoMatch(bool enabled) {
    emit(state.copyWith(autoMatchEnabled: enabled));
  }
}
