import 'package:dio/dio.dart';
import '../../../../core/network/api_endpoints.dart';

class GenerateResultModel {
  final String status;
  final String stockSymbol;
  final String? message;
  final int eventsAnalyzed;
  final int scenariosGenerated;
  final int scenariosSavedToDb;

  const GenerateResultModel({
    required this.status,
    this.stockSymbol = '',
    this.message,
    this.eventsAnalyzed = 0,
    this.scenariosGenerated = 0,
    this.scenariosSavedToDb = 0,
  });

  factory GenerateResultModel.fromJson(Map<String, dynamic> json) {
    return GenerateResultModel(
      status: (json['status'] as String?) ?? 'unknown',
      stockSymbol: (json['stock_symbol'] as String?) ?? 'Mixed Basket',
      message: json['message'] as String?,
      eventsAnalyzed: (json['events_analyzed'] as num?)?.toInt() ?? 0,
      scenariosGenerated: (json['scenarios_generated'] as num?)?.toInt() ?? 0,
      scenariosSavedToDb: (json['scenarios_saved_to_db'] as num?)?.toInt() ?? 0,
    );
  }

  bool get hasScenarios => scenariosGenerated > 0;
}

class GeneratorRemoteDatasource {
  final Dio _dio;

  GeneratorRemoteDatasource(this._dio);

  Future<GenerateResultModel> generate({
    required String stockSymbol,
    required int zscoreWindow,
    required double zscoreTriggerMin,
    required double zscoreTriggerMax,
    String? knowledgeBaseId,
  }) async {
    final response = await _dio.post(
      ApiEndpoints.generate,
      data: {
        'stock_symbol': stockSymbol,
        'zscore_window': zscoreWindow,
        'zscore_trigger_min': zscoreTriggerMin,
        'zscore_trigger_max': zscoreTriggerMax,
        if (knowledgeBaseId != null) 'knowledge_base_id': knowledgeBaseId,
      },
    );
    return GenerateResultModel.fromJson(
        response.data as Map<String, dynamic>);
  }
}