import { axiosInstance } from './axios';
import type { Asset, Organization } from '../types/assets';

export const getAssets = async (): Promise<Asset[]> => {
  const response = await axiosInstance.get<Asset[]>('/assets');
  return response.data;
};

export const getOrganizations = async (): Promise<Organization[]> => {
  const response = await axiosInstance.get<Organization[]>('/organizations');
  return response.data;
};

export const uploadAsset = async (formData: FormData): Promise<void> => {
  await axiosInstance.post('/assets/upload', formData, {
    headers: { 'Content-Type': 'multipart/form-data' }
  });
};

export const updateAsset = async (
  id: string,
  payload: {
    title?: string;
    description?: string;
    categoryId?: string;
    subcategory?: string;
    tags?: string[];
    githubUrl?: string;
    isDownloadable?: boolean;
    shares?: Array<{ organizationId: string; userId: string | null }>;
  }
): Promise<void> => {
  await axiosInstance.patch(`/assets/${id}`, payload);
};

export const deleteAsset = async (id: string): Promise<void> => {
  await axiosInstance.delete(`/assets/${id}`);
};

export const requestDownload = async (id: string): Promise<void> => {
  await axiosInstance.post(`/assets/${id}/request-download`);
};

export const approveDownloadRequest = async (assetId: string, requestId: string): Promise<void> => {
  await axiosInstance.post(`/assets/${assetId}/approve-download/${requestId}`);
};

export const rejectDownloadRequest = async (assetId: string, requestId: string): Promise<void> => {
  await axiosInstance.post(`/assets/${assetId}/reject-download/${requestId}`);
};

export const downloadAssetFile = async (id: string): Promise<void> => {
  await axiosInstance.post(`/assets/${id}/download`);
};

export const bulkShareAssets = async (
  assetIds: string[],
  shares: Array<{ organizationId: string; userId: string | null }>
): Promise<void> => {
  await axiosInstance.patch('/assets/bulk-share', { assetIds, shares });
};

export interface AssetGroup {
  id: string;
  name: string;
  description: string | null;
  assets: Asset[];
  createdAt: string;
  updatedAt: string;
}

export const getAssetGroups = async (): Promise<AssetGroup[]> => {
  const response = await axiosInstance.get<AssetGroup[]>('/assets/groups/list');
  return response.data;
};

export const createAssetGroup = async (payload: { name: string; description?: string; assetIds?: string[] }): Promise<AssetGroup> => {
  const response = await axiosInstance.post<AssetGroup>('/assets/groups/create', payload);
  return response.data;
};

export const deleteAssetGroup = async (id: string): Promise<void> => {
  await axiosInstance.delete(`/assets/groups/${id}`);
};

export const addAssetsToGroup = async (groupId: string, assetIds: string[]): Promise<void> => {
  await axiosInstance.patch(`/assets/groups/${groupId}/add-assets`, { assetIds });
};

export const removeAssetsFromGroup = async (groupId: string, assetIds: string[]): Promise<void> => {
  await axiosInstance.patch(`/assets/groups/${groupId}/remove-assets`, { assetIds });
};

export interface ScrapedCaseStudy {
  title: string;
  thumbnailUrl: string;
  problemStatement: string;
  solution: string;
}

export const scrapeCaseStudy = async (url: string): Promise<ScrapedCaseStudy> => {
  const response = await axiosInstance.get<ScrapedCaseStudy>('/assets/scrape-case-study', {
    params: { url }
  });
  return response.data;
};
