import 'package:flutter/material.dart';
import 'package:tm_app/data/repositories/auth_repository.dart';
import 'package:tm_app/data/services/model/company_profile_data/company_profile_data.dart';
import 'package:tm_app/data/services/model/company_profile_response/company_profile_response.dart';
import 'package:tm_app/data/services/model/profile_data/profile_data.dart';

import '../core/utils/result.dart';
import '../data/repositories/profile_repository.dart';
import '../data/services/model/logout_response/logout_response.dart';
import '../data/services/model/profile_response/profile_response.dart';
import 'auth_view_model.dart';

class ProfileViewModel with ChangeNotifier {
  final ProfileRepository _profileRepository;
  final AuthRepository _authRepository;
  final AuthViewModel _authViewModel;

  ProfileViewModel({
    required ProfileRepository profileRepository,
    required AuthRepository authRepository,
    required AuthViewModel authViewModel,
  }) : _profileRepository = profileRepository,
       _authRepository = authRepository,
       _authViewModel = authViewModel {
    getCompanyProfile();
  }

  bool _isLoading = false;
  String? _errorMessage;
  ProfileData? _profileData;
  CompanyProfileData? _companyProfileData;
  bool _loggedOut = false;

  bool get isLoading => _isLoading;
  bool get loggedOut => _loggedOut;
  String? get errorMessage => _errorMessage;
  ProfileData? get profileData => _profileData;
  CompanyProfileData? get companyProfileData => _companyProfileData;

  Future<void> getProfile() async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    final result = await _profileRepository.getProfile();

    switch (result) {
      case Ok<ProfileResponse>():
        _profileData = result.value.data;
        break;
      case Error<ProfileResponse>():
        _errorMessage = result.error.toString();
        break;
    }
    _isLoading = false;
    notifyListeners();
    _errorMessage = null;
    notifyListeners();
  }

  Future<void> getCompanyProfile() async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    final result = await _profileRepository.getCompanyProfile();

    switch (result) {
      case Ok<CompanyProfileResponse>():
        _companyProfileData = result.value.data;
        break;
      case Error<CompanyProfileResponse>():
        _errorMessage = result.error.toString();
        break;
    }
    _isLoading = false;
    notifyListeners();
    _errorMessage = null;
    notifyListeners();
  }

  Future<void> localLogout() async {
    await _authRepository.localLogout();
    _loggedOut = true;
    notifyListeners();
  }

  Future<void> logout() async {
    _errorMessage = null;
    notifyListeners();

    final result = await _authRepository.logout();

    switch (result) {
      case Ok<LogoutResponse>():
        await _authViewModel.localLogout();
        _loggedOut = true;
        break;
      case Error<LogoutResponse>():
        _errorMessage = result.error.toString();
        break;
    }

    notifyListeners();
    _errorMessage = null;
    notifyListeners();
  }
}
