import 'package:app_petty_cash/model/auth_model.dart';
import 'package:app_petty_cash/model/company_model.dart';
import 'package:app_petty_cash/repository/auth_repository.dart';
import 'package:app_petty_cash/repository/company_repository.dart';

import '../../model/list_category_model.dart';
import '../../repository/transaksi_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

abstract class ChangePasswordState extends Equatable {
  final DateTime date;
  const ChangePasswordState(this.date);
  @override
  List<Object?> get props => [date];
}

class ChangePasswordStateInit extends ChangePasswordState {
  ChangePasswordStateInit() : super(DateTime.now());
}

class ChangePasswordStateLoading extends ChangePasswordState {
  ChangePasswordStateLoading() : super(DateTime.now());
}

class ChangePasswordStateError extends ChangePasswordState {
  final String message;
  ChangePasswordStateError({
    required this.message,
  }) : super(DateTime.now());
}

class ChangePasswordStateDone extends ChangePasswordState {
  final String model;
  ChangePasswordStateDone({
    required this.model,
  }) : super(DateTime.now());
}


//notifier
class ChangePasswordNotifier extends StateNotifier<ChangePasswordState> {
  final Ref ref;
  ChangePasswordNotifier({
    required this.ref,
  }) : super(ChangePasswordStateInit());

  void postChangePassword({
    required String userID,
    required String userToken,
    required String oldPassword,
    required String newPassword,
    required String confirmPassword,

    }) async {
    try {
      state = ChangePasswordStateLoading();
      final dio = ref.read(dioProvider);
      final resp =
          await AuthRepository(dio: dio).changePassword(userID: userID,token: userToken, oldPassword: oldPassword, newPassword: newPassword, confirmPassword: confirmPassword);
      state = ChangePasswordStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ChangePasswordStateError(message: e.message.toString());
      } else {
        state = ChangePasswordStateError(message: e.toString());
      }
    }
  }
}

//provider
final ChangePasswordProvider =
    StateNotifierProvider<ChangePasswordNotifier, ChangePasswordState>(
  (ref) => ChangePasswordNotifier(ref: ref),
);