import { httpClient } from '../../../core/infrastructure/httpClient';
import type { StoryGroup } from '../../../core/domain/types';

export class StoryApi {
  static async getStories() {
    return httpClient.request<StoryGroup[]>('/stories');
  }

  static async getUserStories(userId: string) {
    return httpClient.request<StoryGroup>(`/stories/user/${userId}`);
  }

  static async createStory(data: { type: string; mediaUrl?: string; content?: string; bgColor?: string; privacy?: string; filter?: string; isMuted?: boolean }) {
    return httpClient.request<{ id: string }>('/stories', {
      method: 'POST',
      body: JSON.stringify(data),
    });
  }

  static async uploadVideoToStory(file: File) {
    const formData = new FormData();
    formData.append('file', file);
    return httpClient.request<{ url: string }>('/stories/video', {
      method: 'POST',
      body: formData,
      timeout: 120_000,
    });
  }

  static async viewStory(storyId: string) {
    return httpClient.request<{ message: string }>(`/stories/${storyId}/view`, { method: 'POST' });
  }

  static async deleteStory(storyId: string) {
    return httpClient.request<{ message: string }>(`/stories/${storyId}`, { method: 'DELETE' });
  }

  static async getStoryViewers(storyId: string) {
    return httpClient.request<Array<{ userId: string; username: string; displayName: string; avatar: string | null; viewedAt: string }>>(`/stories/${storyId}/viewers`);
  }

  static async addStoryReaction(storyId: string, emoji: string) {
    return httpClient.request<{ message: string }>(`/stories/${storyId}/reaction`, {
      method: 'POST',
      body: JSON.stringify({ emoji }),
    });
  }

  static async removeStoryReaction(storyId: string, emoji: string) {
    return httpClient.request<{ message: string }>(`/stories/${storyId}/reaction/delete?emoji=${encodeURIComponent(emoji)}`, {
      method: 'POST',
    });
  }

  static async addStoryReply(storyId: string, content: string) {
    return httpClient.request<{ message: string }>(`/stories/${storyId}/reply`, {
      method: 'POST',
      body: JSON.stringify({ content }),
    });
  }

  static async getStoryReplies(storyId: string) {
    return httpClient.request<Array<{ id: string; userId: string; username: string; displayName: string; avatar: string | null; content: string; createdAt: string }>>(`/stories/${storyId}/replies`);
  }
}
