import { api } from './api';
import type {
  Idea,
  CreateIdeaDto,
  UpdateIdeaDto,
  IdeaStatus,
  IdeaPriority,
} from '../types/idea';

export interface QueryIdeasParams {
  status?: IdeaStatus;
  priority?: IdeaPriority;
  module?: string;
  search?: string;
  sortBy?: string;
  sortOrder?: 'ASC' | 'DESC';
  page?: number;
  limit?: number;
}

export interface PaginatedResponse<T> {
  data: T[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
  };
}

export const ideasApi = {
  getAll: async (
    params?: QueryIdeasParams,
  ): Promise<PaginatedResponse<Idea>> => {
    const { data } = await api.get<PaginatedResponse<Idea>>('/ideas', {
      params,
    });
    return data;
  },

  getOne: async (id: string): Promise<Idea> => {
    const { data } = await api.get<Idea>(`/ideas/${id}`);
    return data;
  },

  create: async (dto: CreateIdeaDto): Promise<Idea> => {
    const { data } = await api.post<Idea>('/ideas', dto);
    return data;
  },

  update: async (id: string, dto: UpdateIdeaDto): Promise<Idea> => {
    const { data } = await api.patch<Idea>(`/ideas/${id}`, dto);
    return data;
  },

  delete: async (id: string): Promise<void> => {
    await api.delete(`/ideas/${id}`);
  },

  getModules: async (): Promise<string[]> => {
    const { data } = await api.get<string[]>('/ideas/modules');
    return data;
  },

  reorder: async (items: { id: string; order: number }[]): Promise<void> => {
    await api.patch('/ideas/reorder', { items });
  },
};
