import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../domain/entities/tax_form_entity.dart';
import '../../../domain/usecases/submit_tax_form_usecase.dart';
import 'form_i9_state.dart';

class FormI9Cubit extends Cubit<FormI9State> {
  final SubmitTaxFormUseCase _submitTaxFormUseCase;

  FormI9Cubit(this._submitTaxFormUseCase) : super(const FormI9State());

  void nextStep(int totalSteps) {
    if (state.currentStep < totalSteps - 1) {
      emit(state.copyWith(currentStep: state.currentStep + 1));
    }
  }

  void previousStep() {
    if (state.currentStep > 0) {
      emit(state.copyWith(currentStep: state.currentStep - 1));
    }
  }

  // Personal Info
  void firstNameChanged(String value) => emit(state.copyWith(firstName: value));
  void lastNameChanged(String value) => emit(state.copyWith(lastName: value));
  void middleInitialChanged(String value) => emit(state.copyWith(middleInitial: value));
  void otherLastNamesChanged(String value) => emit(state.copyWith(otherLastNames: value));
  void dobChanged(String value) => emit(state.copyWith(dob: value));
  void ssnChanged(String value) => emit(state.copyWith(ssn: value));
  void emailChanged(String value) => emit(state.copyWith(email: value));
  void phoneChanged(String value) => emit(state.copyWith(phone: value));

  // Address
  void addressChanged(String value) => emit(state.copyWith(address: value));
  void aptNumberChanged(String value) => emit(state.copyWith(aptNumber: value));
  void cityChanged(String value) => emit(state.copyWith(city: value));
  void stateChanged(String value) => emit(state.copyWith(state: value));
  void zipCodeChanged(String value) => emit(state.copyWith(zipCode: value));

  // Citizenship
  void citizenshipStatusChanged(String value) => emit(state.copyWith(citizenshipStatus: value));
  void uscisNumberChanged(String value) => emit(state.copyWith(uscisNumber: value));
  void admissionNumberChanged(String value) => emit(state.copyWith(admissionNumber: value));
  void passportNumberChanged(String value) => emit(state.copyWith(passportNumber: value));
  void countryIssuanceChanged(String value) => emit(state.copyWith(countryIssuance: value));

  // Signature
  void preparerUsedChanged(bool value) => emit(state.copyWith(preparerUsed: value));
  void signatureChanged(String value) => emit(state.copyWith(signature: value));

  Future<void> submit() async {
    emit(state.copyWith(status: FormI9Status.submitting));
    try {
      await _submitTaxFormUseCase(
        TaxFormType.i9,
        <String, dynamic>{
          'firstName': state.firstName,
          'lastName': state.lastName,
          'middleInitial': state.middleInitial,
          'citizenshipStatus': state.citizenshipStatus,
          'ssn': state.ssn,
          'signature': state.signature,
          // ... add other fields as needed for backend
        },
      );
      emit(state.copyWith(status: FormI9Status.success));
    } catch (e) {
      emit(state.copyWith(
        status: FormI9Status.failure,
        errorMessage: e.toString(),
      ));
    }
  }
}
