import 'package:equatable/equatable.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:uto_manager/domain/entities/personnel_record.dart'; import 'package:uto_manager/domain/entities/position_slot.dart'; import 'package:uto_manager/domain/entities/unit_node.dart'; import 'package:uto_manager/domain/services/mtoe_validation_engine.dart'; // --------------------------------------------------------------------------- // State // --------------------------------------------------------------------------- /// State for the validation cubit. sealed class ValidationState extends Equatable { const ValidationState(); } /// No validation has been run yet. class ValidationInitial extends ValidationState { const ValidationInitial(); @override List get props => []; } /// Validation is in progress. class ValidationRunning extends ValidationState { const ValidationRunning(); @override List get props => []; } /// Validation completed with a (possibly empty) list of findings. class ValidationComplete extends ValidationState { const ValidationComplete(this.findings); final List findings; /// All critical findings. List get critical => findings.where((f) => f.severity == FindingSeverity.critical).toList(); /// All warning findings. List get warnings => findings.where((f) => f.severity == FindingSeverity.warning).toList(); /// All advisory findings. List get advisories => findings.where((f) => f.severity == FindingSeverity.advisory).toList(); /// Returns findings for a specific [unitId]. List findingsForUnit(String unitId) => findings.where((f) => f.unitId == unitId).toList(); /// True if there are no findings. bool get isClean => findings.isEmpty; @override List get props => [findings]; } // --------------------------------------------------------------------------- // Cubit // --------------------------------------------------------------------------- /// Runs the [MtoeValidationEngine] against the current unit tree snapshot /// and exposes the resulting [ValidationFinding]s to the UI. /// /// Call [validate] whenever the unit tree, slots, or personnel change. class ValidationCubit extends Cubit { ValidationCubit() : super(const ValidationInitial()); final _engine = const MtoeValidationEngine(); /// Runs all MTOE validation rules against the provided snapshot. /// /// Emits [ValidationRunning] while computing, then [ValidationComplete] /// with the results. void validate({ required List units, required List slots, required List personnel, }) { emit(const ValidationRunning()); final findings = _engine.validate( units: units, slots: slots, personnel: personnel, ); emit(ValidationComplete(findings)); } /// Clears all findings and resets to the initial state. void clear() => emit(const ValidationInitial()); }