
import 'package:app_petty_cash/repository/home_repository.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../model/list_transaksi_model.dart';
import '../../repository/transaksi_repository.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';

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

class ListTransaksiHomeStateInit extends ListTransaksiHomeState {
  ListTransaksiHomeStateInit() : super(DateTime.now());
}

class ListTransaksiHomeStateLoading extends ListTransaksiHomeState {
  ListTransaksiHomeStateLoading() : super(DateTime.now());
}

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

class ListTransaksiHomeStateDone extends ListTransaksiHomeState {
  final List<ListTransaksiHomeModel> model;
  // final String resp;
  ListTransaksiHomeStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class ListTransaksiHomeNotifier extends StateNotifier<ListTransaksiHomeState> {
  final Ref ref;
  ListTransaksiHomeNotifier({
    required this.ref,
  }) : super(ListTransaksiHomeStateInit());

  void listTransaksiHome(
    String companyid,
  ) async {
    try {
      state = ListTransaksiHomeStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await HomeRepository(dio: dio).getTransaksiListHome(
        companyid,
      );
      state = ListTransaksiHomeStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = ListTransaksiHomeStateError(message: e.message.toString());
      } else {
        state = ListTransaksiHomeStateError(message: e.toString());
      }
    }
  }
}

//provider
final listTransaksiHomeProvider =
    StateNotifierProvider<ListTransaksiHomeNotifier, ListTransaksiHomeState>(
  (ref) => ListTransaksiHomeNotifier(ref: ref),
);
