/// Sample Project Remote Data Source
///
/// Handles remote API calls for sample/model house projects.
library;

import 'package:worker/core/constants/api_constants.dart';
import 'package:worker/core/network/dio_client.dart';
import 'package:worker/features/showrooms/data/models/sample_project_model.dart';

/// Sample Project Remote Data Source Interface
abstract class SampleProjectRemoteDataSource {
  /// Fetch list of sample/model house projects from API
  Future<List<SampleProjectModel>> getSampleProjects({
    int limitStart = 0,
    int limitPageLength = 0,
  });

  /// Fetch detail of a sample/model house project by name
  Future<SampleProjectModel> getSampleProjectDetail(String name);
}

/// Sample Project Remote Data Source Implementation
class SampleProjectRemoteDataSourceImpl implements SampleProjectRemoteDataSource {
  const SampleProjectRemoteDataSourceImpl(this._dioClient);

  final DioClient _dioClient;

  /// Get list of sample projects
  ///
  /// Calls: POST /api/method/building_material.building_material.api.sample_project.get_list
  /// Body: { "limit_start": 0, "limit_page_length": 0 }
  /// Returns: List of sample projects with 360° view links
  @override
  Future<List<SampleProjectModel>> getSampleProjects({
    int limitStart = 0,
    int limitPageLength = 0,
  }) async {
    try {
      final response = await _dioClient.post<Map<String, dynamic>>(
        '${ApiConstants.frappeApiMethod}${ApiConstants.getSampleProjectList}',
        data: {
          'limit_start': limitStart,
          'limit_page_length': limitPageLength,
        },
      );

      final data = response.data;
      if (data == null) {
        throw Exception('No data received from getSampleProjectList API');
      }

      // API returns: { "message": [...] }
      final message = data['message'];
      if (message == null) {
        throw Exception('No message field in getSampleProjectList response');
      }

      final List<dynamic> projectsList = message as List<dynamic>;
      return projectsList
          .map((json) => SampleProjectModel.fromJson(json as Map<String, dynamic>))
          .toList();
    } catch (e) {
      throw Exception('Failed to get sample projects: $e');
    }
  }

  /// Get detail of a sample project by name
  ///
  /// Calls: POST /api/method/building_material.building_material.api.sample_project.get_detail
  /// Body: { "name": "PROJ-0001" }
  /// Returns: Full project detail with files_list
  @override
  Future<SampleProjectModel> getSampleProjectDetail(String name) async {
    try {
      final response = await _dioClient.post<Map<String, dynamic>>(
        '${ApiConstants.frappeApiMethod}${ApiConstants.getSampleProjectDetail}',
        data: {'name': name},
      );

      final data = response.data;
      if (data == null) {
        throw Exception('No data received from getSampleProjectDetail API');
      }

      // API returns: { "message": {...} }
      final message = data['message'];
      if (message == null) {
        throw Exception('No message field in getSampleProjectDetail response');
      }

      return SampleProjectModel.fromJson(message as Map<String, dynamic>);
    } catch (e) {
      throw Exception('Failed to get sample project detail: $e');
    }
  }
}
