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

class ScenarioSummaryModel {
  final String id;
  final String title;
  final String? shortDescription;
  final String? eventType;
  final String? difficulty;
  final String? category;
  final String? riskLevel;
  final String? createdAt;

  const ScenarioSummaryModel({
    required this.id,
    required this.title,
    this.shortDescription,
    this.eventType,
    this.difficulty,
    this.category,
    this.riskLevel,
    this.createdAt,
  });

  factory ScenarioSummaryModel.fromJson(Map<String, dynamic> json) {
    return ScenarioSummaryModel(
      id: json['id'] as String,
      title: json['title'] as String,
      shortDescription: json['short_description'] as String?,
      eventType: json['event_type'] as String?,
      difficulty: json['difficulty'] as String?,
      category: json['category'] as String?,
      riskLevel: json['risk_level'] as String?,
      createdAt: json['created_at'] as String?,
    );
  }
}

class ScenarioPageModel {
  final List<ScenarioSummaryModel> scenarios;
  final int total;
  final int page;
  final int pages;

  const ScenarioPageModel({
    required this.scenarios,
    required this.total,
    required this.page,
    required this.pages,
  });

  factory ScenarioPageModel.fromJson(Map<String, dynamic> json) {
    return ScenarioPageModel(
      scenarios: (json['scenarios'] as List<dynamic>)
          .map((e) =>
              ScenarioSummaryModel.fromJson(e as Map<String, dynamic>))
          .toList(),
      total: (json['total'] as num).toInt(),
      page: (json['page'] as num).toInt(),
      pages: (json['pages'] as num).toInt(),
    );
  }
}

class ScenarioDetailModel {
  final String id;
  final String title;
  final String? shortDescription;
  final Map<String, dynamic>? givensTable;
  final String? scenarioParagraph;
  final String? bestAnswer;
  final String? bestAnswerRationale;
  final String? otherOption1;
  final String? otherOption1Exp;
  final String? otherOption2;
  final String? otherOption2Exp;
  final String? otherOption3;
  final String? otherOption3Exp;
  final String? eventType;
  final String? difficulty;
  final String? category;
  final String? riskLevel;
  final String? createdAt;

  const ScenarioDetailModel({
    required this.id,
    required this.title,
    this.shortDescription,
    this.givensTable,
    this.scenarioParagraph,
    this.bestAnswer,
    this.bestAnswerRationale,
    this.otherOption1,
    this.otherOption1Exp,
    this.otherOption2,
    this.otherOption2Exp,
    this.otherOption3,
    this.otherOption3Exp,
    this.eventType,
    this.difficulty,
    this.category,
    this.riskLevel,
    this.createdAt,
  });

  factory ScenarioDetailModel.fromJson(Map<String, dynamic> json) {
    var givens = json['givens_table'];
    if (givens is String) {
      try {
        givens = null; // API should return parsed, but safety fallback
      } catch (_) {}
    }

    return ScenarioDetailModel(
      id: json['id'] as String,
      title: json['title'] as String,
      shortDescription: json['short_description'] as String?,
      givensTable: givens is Map<String, dynamic> ? givens : null,
      scenarioParagraph: json['scenario_paragraph'] as String?,
      bestAnswer: json['best_answer'] as String?,
      bestAnswerRationale: json['best_answer_rationale'] as String?,
      otherOption1: json['other_option1'] as String?,
      otherOption1Exp: json['other_option1_exp'] as String?,
      otherOption2: json['other_option2'] as String?,
      otherOption2Exp: json['other_option2_exp'] as String?,
      otherOption3: json['other_option3'] as String?,
      otherOption3Exp: json['other_option3_exp'] as String?,
      eventType: json['event_type'] as String?,
      difficulty: json['difficulty'] as String?,
      category: json['category'] as String?,
      riskLevel: json['risk_level'] as String?,
      createdAt: json['created_at'] as String?,
    );
  }
}

class ScenarioRemoteDatasource {
  final Dio _dio;

  ScenarioRemoteDatasource(this._dio);

  Future<ScenarioPageModel> getScenarios({
    int page = 1,
    int limit = 12,
    String? difficulty,
    String? category,
    String? risk,
  }) async {
    final response = await _dio.get(
      ApiEndpoints.scenarios,
      queryParameters: {
        'page': page,
        'limit': limit,
        if (difficulty != null && difficulty.isNotEmpty) 'difficulty': difficulty,
        if (category != null && category.isNotEmpty) 'category': category,
        if (risk != null && risk.isNotEmpty) 'risk': risk,
      },
    );
    return ScenarioPageModel.fromJson(response.data as Map<String, dynamic>);
  }

  Future<ScenarioDetailModel> getDetail(String id) async {
    final response = await _dio.get(ApiEndpoints.scenarioDetail(id));
    return ScenarioDetailModel.fromJson(
        response.data as Map<String, dynamic>);
  }
}