import 'package:flutter/material.dart';
import 'package:tm_app/core/utils/result.dart';
import 'package:tm_app/data/repositories/tender_detail_repository.dart';
import 'package:tm_app/data/repositories/tenders_repository.dart';
import 'package:tm_app/data/services/model/tender_data/tender_data.dart';
import 'package:tm_app/data/services/model/tender_detail_response/tender_detail_response_model.dart';

import '../core/constants/tender_approval_status.dart';
import '../data/services/model/tender_approvals_by_id_response/tender_approvals_by_id_response.dart';
import '../data/services/model/tender_approvals_data/tender_approvals_data.dart';
import '../data/services/model/tender_approvals_toggle_response/tender_approvals_toggle_response.dart';
import 'home_view_model.dart';
import 'tenders_view_model.dart';

class TenderDetailViewModel with ChangeNotifier {
  final TenderDetailRepository _tenderDetailRepository;
  final TendersRepository _tendersRepository;
  final HomeViewModel _homeViewModel;

  TenderDetailViewModel({
    required TenderDetailRepository tenderDetailRepository,
    required TendersRepository tendersRepository,
    required TendersViewModel tendersViewModel,
    required HomeViewModel homeViewModel,
  }) : _tenderDetailRepository = tenderDetailRepository,
       _tendersRepository = tendersRepository,

       _homeViewModel = homeViewModel;

  bool _isLoading = false;
  bool _isSubmitApprovalLoading = false;
  bool _isRejectApprovalLoading = false;
  String? _errorMessage;
  TenderData? _tenderDetail;
  TenderApprovalsData? _tenderApprovalData;
  bool approvalStatus = false;
  String _status = '';

  bool get isLoading => _isLoading;
  bool get isSubmitApprovalLoading => _isSubmitApprovalLoading;
  bool get isRejectApprovalLoading => _isRejectApprovalLoading;
  String? get errorMessage => _errorMessage;
  TenderData? get tenderDetail => _tenderDetail;
  TenderApprovalsData? get tenderApprovalData => _tenderApprovalData;
  String get status => _status;

  Future<void> getTenderDetail({required String id}) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    final result = await _tenderDetailRepository.getTenderDetail(tenderId: id);
    switch (result) {
      case Ok<TenderDetailResponseModel>():
        _tenderDetail = result.value.data;
        await getTenderApprovals(id);
        break;
      case Error<TenderDetailResponseModel>():
        _errorMessage = result.error.toString();
        break;
    }

    _isLoading = false;
    notifyListeners();
  }

  Future<void> submitTenderApproval({
    required String tenderId,
    required String submissionMode,
  }) async {
    _isSubmitApprovalLoading = true;
    _errorMessage = null;
    notifyListeners();
    final result = await _tendersRepository.toggleApprovals(
      tenderId: tenderId,
      status: TenderApprovalStatus.submitted.value,
      submissionMode: submissionMode,
    );
    switch (result) {
      case Ok<TenderApprovalsToggleResponse>():
        approvalStatus = true;
        _status = result.value.data?.status ?? '';
        break;
      case Error<TenderApprovalsToggleResponse>():
        _errorMessage = result.error.toString();
        approvalStatus = false;
        break;
    }
    _isSubmitApprovalLoading = false;
    notifyListeners();
    _errorMessage = null;
    approvalStatus = false;
    notifyListeners();
  }

  Future<void> rejectTenderApproval({
    required String tenderId,
    required String submissionMode,
  }) async {
    _isRejectApprovalLoading = true;
    _errorMessage = null;
    notifyListeners();
    final result = await _tendersRepository.toggleApprovals(
      tenderId: tenderId,
      status: TenderApprovalStatus.rejected.value,
      submissionMode: submissionMode,
    );
    switch (result) {
      case Ok<TenderApprovalsToggleResponse>():
        approvalStatus = true;
        _status = result.value.data?.status ?? '';
        break;
      case Error<TenderApprovalsToggleResponse>():
        _errorMessage = result.error.toString();
        approvalStatus = false;
        break;
    }
    _isRejectApprovalLoading = false;
    notifyListeners();
    _errorMessage = null;
    approvalStatus = false;
    notifyListeners();
  }

  void updateHome() async {
    _homeViewModel.init();
  }

  Future<void> getTenderApprovals(String tenderId) async {
    _errorMessage = null;
    notifyListeners();

    final result = await _tendersRepository.getTenderApprovals(
      tenderId: tenderId,
    );
    approvalStatus = false;
    switch (result) {
      case Ok<TenderApprovalsByIdResponse>():
        _tenderApprovalData = result.value.data;
        _status = result.value.data?.status ?? '';
        break;
      case Error<TenderApprovalsByIdResponse>():
        _errorMessage = result.error.toString();
        break;
    }
    notifyListeners();
    _errorMessage = null;
    notifyListeners();
  }
}
