import { apiClient, PaginatedResponse, PaginationParams } from './client';

export interface Asset {
  id: string;
  tenant_id: string;
  brand_id: string | null;
  brand?: {
    id: string;
    name: string;
  };
  name: string;
  original_name: string;
  type: 'image' | 'video' | 'document' | 'audio' | 'other';
  mime_type: string;
  size: number;
  url: string;
  thumbnail_url: string | null;
  width: number | null;
  height: number | null;
  duration: number | null;
  alt_text: string | null;
  description: string | null;
  tags: string[] | null;
  metadata: Record<string, any> | null;
  status: 'uploading' | 'processing' | 'ready' | 'error';
  storage_path: string | null;
  storage_provider: string | null;
  created_at: string;
  updated_at: string;
}

export interface AssetFolder {
  id: string;
  tenant_id: string;
  brand_id: string | null;
  brand?: {
    id: string;
    name: string;
  };
  parent_id: string | null;
  parent?: AssetFolder;
  children?: AssetFolder[];
  name: string;
  slug: string;
  description: string | null;
  path: string | null;
  level: number;
  sort_order: number;
  created_at: string;
  updated_at: string;
}

export const assetsApi = {
  // Assets
  getAll: (params?: PaginationParams & { brandId?: string; type?: string; search?: string }) =>
    apiClient.get<PaginatedResponse<Asset>>('/assets', { params }),

  getById: (id: string) => apiClient.get<Asset>(`/assets/${id}`),

  getByBrand: (brandId: string) => apiClient.get<Asset[]>(`/assets/brand/${brandId}`),

  create: (data: Partial<Asset>) => apiClient.post<Asset>('/assets', data),

  update: (id: string, data: Partial<Asset>) => apiClient.put<Asset>(`/assets/${id}`, data),

  updateStatus: (id: string, status: Asset['status']) =>
    apiClient.put<Asset>(`/assets/${id}/status`, { status }),

  delete: (id: string) => apiClient.delete(`/assets/${id}`),

  bulkDelete: (ids: string[]) => apiClient.delete('/assets/bulk', { data: { ids } }),
};

export const foldersApi = {
  // Folders
  getAll: (brandId?: string) =>
    apiClient.get<AssetFolder[]>('/assets/folders', { params: { brandId } }),

  getTree: (brandId?: string) =>
    apiClient.get<AssetFolder[]>('/assets/folders/tree', { params: { brandId } }),

  getRootFolders: (brandId?: string) =>
    apiClient.get<AssetFolder[]>('/assets/folders/root', { params: { brandId } }),

  getById: (id: string) => apiClient.get<AssetFolder>(`/assets/folders/${id}`),

  getBySlug: (slug: string) => apiClient.get<AssetFolder>(`/assets/folders/slug/${slug}`),

  create: (data: Partial<AssetFolder>) => apiClient.post<AssetFolder>('/assets/folders', data),

  update: (id: string, data: Partial<AssetFolder>) =>
    apiClient.put<AssetFolder>(`/assets/folders/${id}`, data),

  delete: (id: string) => apiClient.delete(`/assets/folders/${id}`),
};
