import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:glowwheels/constants/api.dart';
import 'package:glowwheels/models/shop_profile_model.dart';
import 'package:http/http.dart' as http;


class ShopProfileProvider with ChangeNotifier {
  ShopProfileModel? _shopProfile;
  bool _isLoading = false;
  String? _errorMessage;

  ShopProfileModel? get shopProfile => _shopProfile;
  bool get isLoading => _isLoading;
  String? get errorMessage => _errorMessage;

  Future<void> fetchShopProfile(String shopId) async {
    _isLoading = true;
    _errorMessage = null;
    notifyListeners();

    final shopUri = Uri.parse(ApiConstants.shopDetails(shopId));

    try {
      final response = await http.get(shopUri);

      if (response.statusCode == 200) {
        final data = json.decode(response.body);
        _shopProfile = ShopProfileModel.fromJson(data);
      } else {
        _errorMessage = 'Failed to load shop profile. Status code: ${response.statusCode}';
      }
    } catch (e) {
      _errorMessage = 'An error occurred: $e';
    } finally {
      _isLoading = false;
      notifyListeners();
    }
  }
}
