import 'package:didvan/constants/assets.dart';
import 'package:flutter/material.dart';

enum MediaType { image, video, gif }

class StoryItem {
  final int id;
  final String url;
  final MediaType media;
  final Duration duration;
  final ValueNotifier<bool> isViewed;

  StoryItem({
    required this.id,
    required this.url,
    required this.media,
    required this.duration,
    bool viewed = false,
  }) : isViewed = ValueNotifier(viewed);

  factory StoryItem.fromJson(Map<String, dynamic> json) {
    MediaType mediaType;
    switch (json['mediaType']) {
      case 'VIDEO':
        mediaType = MediaType.video;
        break;
      case 'GIF':
        mediaType = MediaType.gif;
        break;
      case 'IMAGE':
      default:
        mediaType = MediaType.image;
        break;
    }

    return StoryItem(
      id: json['id'],
      url: json['mediaUrl'],
      media: mediaType,
      duration: const Duration(seconds: 10),
    );
  }
}

class User {
  final String name;
  final String profileImageUrl;
  final String createdAt;

  const User({
    required this.name,
    required this.profileImageUrl,
    required this.createdAt,
  });
}

class UserStories {
  final int id;
  final User user;
  final List<StoryItem> stories;

  UserStories({
    required this.id,
    required this.user,
    required this.stories,
  });

  factory UserStories.fromApiJson(Map<String, dynamic> json) {
    final List<dynamic> items = json['items'] ?? [];
    final List<dynamic> completedIds = json['completedStoryItemIds'] ?? [];

    return UserStories(
      id: json['id'],
      user: User(
        name: json['title'], 
        profileImageUrl:
            _mapCategoryToIcon(json['category']),
        createdAt: json['createdAt'],
      ),
      stories: items.map((item) {
        final storyItem = StoryItem.fromJson(item);
        if (completedIds.contains(storyItem.id)) {
          storyItem.isViewed.value = true;
        }
        return storyItem;
      }).toList(),
    );
  }
}


String _mapCategoryToIcon(String category) {
  switch (category) {
    case 'MACRO_TRENDS':
      return 'lib/assets/icons/MACRO_TRENDS_N.png';
    case 'INDUSTRY_ENVIRONMENT':
      return 'lib/assets/icons/INDUSTRY_ENVIRONMENT_N.png';
    case 'COMPETITION_ENVIRONMENT':
      return Assets.startup;
    case 'OPPORTUNITIES':
      return Assets.hugeideas;
    case 'THREATS':
      return Assets.risk;
    case 'TECHNOLOGY':
      return Assets.tech;
    case 'STARTUPS':
      return Assets.startup;
    case 'CONCEPTS':
      return 'lib/assets/icons/CONCEPTS_N.png';
    case 'MACRO_ENVIRONMENT':
      return 'lib/assets/icons/MACRO_ENVIRONMENT_N.png';
    case 'MODERN_INVESTMENTS':
      return 'lib/assets/icons/MODERN_INVESTMENTS_N.png';
    default:
      return Assets.stats;
  }
}