import 'package:equatable/equatable.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '/models/company_model.dart';
import '/models/work_list_model.dart';
import '../../repository/company_repository.dart';
import '../../repository/work_list_repository.dart';

import '../../models/pending_work_model.dart';
import '../../provider/dio_provider.dart';
import '../../repository/base_repository.dart';
import '../../repository/home_screen_repository.dart';

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

class SearchCompanyStateInit extends SearchCompanyState {
  SearchCompanyStateInit() : super(DateTime.now());
}

class SearchCompanyStateLoading extends SearchCompanyState {
  SearchCompanyStateLoading() : super(DateTime.now());
}

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

class SearchCompanyStateDone extends SearchCompanyState {
  final List<CompanyModel> model;
  SearchCompanyStateDone({
    required this.model,
  }) : super(DateTime.now());
}

//notifier
class SearchCompanyNotifier extends StateNotifier<SearchCompanyState> {
  final Ref ref;
  SearchCompanyNotifier({
    required this.ref,
  }) : super(SearchCompanyStateInit());

  void getData({required String keyword}) async {
    try {
      state = SearchCompanyStateLoading();
      final dio = ref.read(dioProvider);
      final resp = await CompanyRepository(dio: dio).search(keyword: keyword);
      state = SearchCompanyStateDone(model: resp);
    } catch (e) {
      if (e is BaseRepositoryException) {
        state = SearchCompanyStateError(message: e.message.toString());
      } else {
        state = SearchCompanyStateError(message: e.toString());
      }
    }
  }
}

//provider
final SearchCompanyProvider =
    StateNotifierProvider<SearchCompanyNotifier, SearchCompanyState>(
        (ref) => SearchCompanyNotifier(ref: ref));
