import 'package:didvan/models/requests/infography.dart';
import 'package:didvan/models/requests/news.dart';
import 'package:didvan/models/requests/newstats_general.dart';
import 'package:didvan/models/requests/radar.dart';
import 'package:didvan/models/requests/studio.dart';

class RequestHelper {
  static const String baseUrl = 'https://api.didvan.app';
  static const String baseUrl2 = 'https://opportunity-threat.didvan.com';
  static const String storiesV1 = '$baseUrl2/api/v1/stories';
  static const String storyActivity = '$baseUrl2/api/v1/stories/activity';
  static const String _baseUserUrl = '$baseUrl/user';
  static const String _baseRadarUrl = '$baseUrl/radar';
  static const String _baseNewsUrl = '$baseUrl/news';
  static const String _baseInfographyUrl = '$baseUrl/home/infography';
  static const String _baseInfoTagUrl = '$baseUrl/home/infography/tags';
  static const String _baseStudioUrl = '$baseUrl/studio';
  static const String _baseStatisticUrl = '$baseUrl/statistic';
  static const String _baseDirectUrl = '$_baseUserUrl/direct';
  static const String _baseHomeUrl = '$baseUrl/home';
  static const String _baseNewStats = '$baseUrl/home/statistic';
  static const String _baseNewStatsSearch = '$baseUrl/home/statistic/search';
  static const String checkHasPassword = '$_baseUserUrl/checkHasPassword';

  static const String mainPageContent = _baseHomeUrl;
  static String searchAll({
    required int page,
    String? startDate,
    String? endDate,
    String? search,
    List<int>? types,
  }) =>
      '$_baseHomeUrl/search${_urlConcatGenerator([
            MapEntry('page', page),
            MapEntry('start', startDate),
            MapEntry('end', endDate),
            MapEntry('q', search),
            MapEntry('type', _urlListConcatGenerator(types)),
          ])}';
  static String searchMarks({
    required int page,
    String? search,
    List<int>? types,
    String? startDate,
    String? endDate,
  }) =>
      '$_baseHomeUrl/mark/v2${_urlConcatGenerator([
            MapEntry('page', page),
            MapEntry('q', search),
            MapEntry('type', _urlListConcatGenerator(types)),
            MapEntry('start', startDate),
            MapEntry('end', endDate),
          ])}';

  static const String confirmUsername = '$_baseUserUrl/confirmUsername';
  static const String changePassword = '$_baseUserUrl/changePassword';
  static const String login = '$_baseUserUrl/login';
  static const String directs = '$_baseUserUrl/direct';
  static const String userInfo = '$_baseUserUrl/info';
  static const String firebaseToken = '$_baseUserUrl/firebaseToken';
  static const String silenceInterval = '$_baseUserUrl/silenceInterval';
  static const String updateProfilePhoto = '$_baseUserUrl/profile/photo';
  static const String checkUsername = '$_baseUserUrl/CheckUsername';
  static const String updateProfile = '$_baseUserUrl/profile/edit';
  static const String otp = '$_baseUserUrl/otp';
  static String editItemBookmark(String type, int id) =>
      '$_baseHomeUrl/$type/$id/mark';
  static String editItemLike(String type, int id) => '$baseUrl/$type/$id/like';

  static const String directTypes = '$baseUrl/direct/types';
  static String direct(int id) => '$_baseDirectUrl/$id';
  static String sendDirectMessage(int id) => '$_baseDirectUrl/$id/sendMessage';
  static String deleteDirect(int id, int messageId) =>
      '$_baseDirectUrl/$id/message/$messageId';

  static String tagV2({
    required List<int> ids,
    int? page,
    int? limit,
  }) =>
      '$baseUrl/tag/v2${_urlConcatGenerator([
            MapEntry('page', page),
            MapEntry('limit', limit ?? '3'),
            MapEntry('tags', _urlListConcatGenerator(ids)),
          ])}';

  static String tag({
    required List<int> ids,
    String? type,
    int? itemId,
    int? page,
    int? limit,
  }) =>
      '$baseUrl/tag${_urlConcatGenerator([
            MapEntry('page', page),
            MapEntry('limit', limit ?? '3'),
            MapEntry('type', type),
            MapEntry('id', itemId ?? '1'),
            MapEntry('tags', _urlListConcatGenerator(ids)),
          ])}';

  static String radarDetails(int id, RadarRequestArgs args) =>
      '$_baseRadarUrl/$id${_urlConcatGenerator([
            MapEntry('page', args.page),
            MapEntry('start', args.startDate),
            MapEntry('end', args.endDate),
            MapEntry('search', args.search),
            MapEntry('categories', _urlListConcatGenerator(args.categories)),
          ])}';
  static String radarOverviews({required RadarRequestArgs args}) =>
      _baseRadarUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
        MapEntry('categories', _urlListConcatGenerator(args.categories)),
      ]);

  static String newsDetails(int id, NewsRequestArgs args) =>
      '$_baseNewsUrl/$id${_urlConcatGenerator([
            MapEntry('page', args.page),
            MapEntry('start', args.startDate),
            MapEntry('end', args.endDate),
            MapEntry('search', args.search),
          ])}';
  static String newsOverviews({required NewsRequestArgs args}) =>
      _baseNewsUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
        MapEntry('search', args.search),
      ]);

  static String infographyOverviews({required InfographyRequestArgs args}) =>
      _baseInfographyUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page),
        MapEntry('category', _urlListConcatGenerator(args.categories)),
        MapEntry('q', args.q),
        MapEntry('tag', _urlListConcatGenerator(args.tag)),
      ]);

  static String statisticGeneral(
          {required StatisticsGeneralRequestArgs args}) =>
      _baseNewStatsSearch +
      _urlConcatGenerator([
        MapEntry('page', args.page),
        MapEntry('category', args.categories),
        MapEntry('q', args.q),
        MapEntry('type', args.type),
      ]);

  static String newStatisticOverViw() => _baseNewStats;

  static String infographyTags() => _baseInfoTagUrl;

  static String studioSlider(String type) =>
      '$_baseStudioUrl/slider${_urlConcatGenerator([MapEntry('type', type)])}';
  static String studioDetails(int id, StudioRequestArgs args) =>
      '$_baseStudioUrl/$id${_urlConcatGenerator([
            MapEntry('page', args.page),
            MapEntry('type', args.type),
            MapEntry('order', args.order),
            MapEntry('search', args.search),
            MapEntry('asc', args.asc),
            MapEntry('start', args.startDate),
            MapEntry('end', args.endDate),
          ])}';
  static String studioOverviews({required StudioRequestArgs args}) =>
      _baseStudioUrl +
      _urlConcatGenerator([
        MapEntry('page', args.page),
        MapEntry('type', args.type),
        MapEntry('order', args.order),
        MapEntry('search', args.search),
        MapEntry('asc', args.asc),
        MapEntry('start', args.startDate),
        MapEntry('end', args.endDate),
      ]);

  static String statisticOverviews(int? category) =>
      _baseStatisticUrl +
      _urlConcatGenerator(
        [MapEntry('category', category)],
      );
  static String statisticDetails(
    String label,
    String period,
  ) =>
      '$_baseStatisticUrl/$label${_urlConcatGenerator([
            MapEntry('period', period),
          ])}';

  static String mark(int id, String type) => '$baseUrl/$type/$id/mark';
  static String tracking(int id, String type) => '$baseUrl/$type/$id/tracking';
  static String comments(int id, String type) => '$baseUrl/$type/$id/comments';
  static String mention(int id, String type) => '$baseUrl/$type/$id/mention';
  static String favourites() => '$baseUrl/user/favorites';
  static String notificationStatus() => '$baseUrl/user/notification/status';
  static String notificationTime() => '$baseUrl/user/notification/time';
  static String usersMentions(String search) =>
      '$baseUrl/comment/user?search=$search';
  static String feedback(int id, int commentId, String type) =>
      '$baseUrl/$type/$id/comments/$commentId/feedback';
  static String addComment(int id, String type) =>
      '$baseUrl/$type/$id/comments/add';
  static String deleteComment(int id) => '$baseUrl/comment/$id';
  static String reportComment(int id) => '$baseUrl/comment/$id/report';
  static String widgetNews() => '$baseUrl/user/widget';
  static String aiChats() => '$baseUrl/ai/chat/v2';
  static String aiArchived() => '$baseUrl/ai/chat/v2${_urlConcatGenerator([
            const MapEntry('archived', true),
          ])}';
  static String aiBots() => '$baseUrl/ai/bot/v2';
  static String aiSearchBots(String q) =>
      '$baseUrl/ai/bot${_urlConcatGenerator([
            MapEntry('q', q),
          ])}';
  static String aiSearchChats(String q) =>
      '$baseUrl/ai/chat${_urlConcatGenerator([
            MapEntry('q', q),
          ])}';
  static String aiSearchArchived(String q) =>
      '$baseUrl/ai/chat${_urlConcatGenerator([
            MapEntry('q', q),
            const MapEntry('archived', true),
          ])}';
  static String aiAChat(int id) => '$baseUrl/ai/chat/$id/v2';
  static String aiChatId() => '$baseUrl/ai/chat/id';
  static String aiDeleteChats() => '$baseUrl/ai/chat';
  static String aiChangeChats(int id) => '$baseUrl/ai/chat/$id/title';  
  static String deleteChat(int id) => '$baseUrl/ai/chat/$id';
  static String deleteMessage(int chatId, int messageId) =>
      '$baseUrl/ai/chat/$chatId/message/$messageId';
  static String deleteAllChats() => '$baseUrl/ai/chat/all';
  static String archivedChat(int id) => '$baseUrl/ai/chat/$id/archive';
  static String placeholder(int id) => '$baseUrl/ai/chat/$id/placeholder';
  static String aiSummery() => '$baseUrl/ai/aisummery';
  static String aiAudio() => '$baseUrl/ai/101/aiaudio';
  static String chartAnalysis() => '$baseUrl/ai/27/chart-analysis';
  static String aiVideo() => '$baseUrl/ai/102/aivideo';
  static String tools() => '$baseUrl/ai/tool';
  static String info() => '$baseUrl/ai/video';
  static String usersAssistants({final bool personal = false}) =>
      '$baseUrl/ai/user/bot${_urlConcatGenerator([
            MapEntry('personal', personal),
          ])}';
  static String createAssistants() => '$baseUrl/ai/user/bot';
  static String updateAssistants(int id) => '$baseUrl/ai/user/bot/$id';
  static String getAssistant(int id) => '$baseUrl/ai/user/bot/$id';
  static String deleteAssistant(int id) => '$baseUrl/ai/user/bot/$id';
  static String nameOfAssistant() => '$baseUrl/ai/user/bot/name';
  static String aiWelcome() => '$baseUrl/ai/aiwellcom';

  static String _urlConcatGenerator(List<MapEntry<String, dynamic>> additions) {
    String result = '';
    additions.removeWhere(
      (element) => element.value == null || element.value.toString().isEmpty,
    );
    if (additions.isNotEmpty) {
      result += '?';
      for (var i = 0; i < additions.length; i++) {
        result += ('${additions[i].key}=${additions[i].value!}');
        if (i != additions.length - 1) {
          result += '&';
        }
      }
    }
    return result;
  }

  static String? _urlListConcatGenerator(List? input) {
    String? result;
    if (input == null) return null;
    if (input.isNotEmpty) {
      result = '';
      for (var i = 0; i < input.length; i++) {
        result = result! + input[i].toString();
        if (i != input.length - 1) {
          result += ',';
        }
      }
      return result;
    }
    return null;
  }

  static String aiSuggestedQuestions() {
    return '$baseUrl/ai/aiquestion';
  }

}
