import 'package:dio/dio.dart';
import 'package:real_estate_mobile/core/constants/api_constants.dart';
import 'package:real_estate_mobile/core/network/api_client.dart';
import 'package:real_estate_mobile/core/network/api_exceptions.dart';
import 'package:real_estate_mobile/features/home/data/models/landing_page_content.dart';

class HomeRepository {
  final Dio _dio = ApiClient.instance.dio;

  /// Fetch landing page CMS content: GET /cms/page/landing
  /// Response: { success: true, data: [ { sectionKey, content, ... }, ... ] }
  Future<LandingPageContent> getLandingPageContent() async {
    try {
      final response = await _dio.get(ApiConstants.cmsPageLanding);

      final data = response.data['data'] as List<dynamic>;
      final sections = data
          .map((e) => CmsSection.fromJson(e as Map<String, dynamic>))
          .toList();

      return LandingPageContent.fromSections(sections);
    } on DioException catch (e) {
      if (e.error is ApiException) {
        throw e.error as ApiException;
      }
      throw ApiException(
        message: e.message ?? 'Failed to fetch landing page content',
        statusCode: e.response?.statusCode,
      );
    }
  }
}
