import { useApi } from './useApi'
import type {
  ApiProject,
  CreateProjectPayload,
  ProjectListQuery,
  UpdateProjectPayload,
} from '@/types/project'
import type { Paginated } from '@/types/pagination'

// 프로젝트 도메인 API Composable — 인터셉터가 success/data 를 언래핑
export function useProject() {
  const api = useApi()

  // 목록(검색/정렬 + 페이지네이션)
  async function list(
    page = 1,
    size = 20,
    query: ProjectListQuery = {},
  ): Promise<Paginated<ApiProject>> {
    return (await api.get('/projects', {
      params: { ...query, page, size },
    })) as unknown as Paginated<ApiProject>
  }

  async function get(id: string): Promise<ApiProject> {
    return (await api.get(`/projects/${id}`)) as unknown as ApiProject
  }

  async function create(payload: CreateProjectPayload): Promise<ApiProject> {
    return (await api.post('/projects', payload)) as unknown as ApiProject
  }

  async function update(
    id: string,
    payload: UpdateProjectPayload,
  ): Promise<ApiProject> {
    return (await api.patch(`/projects/${id}`, payload)) as unknown as ApiProject
  }

  async function remove(id: string): Promise<void> {
    await api.delete(`/projects/${id}`)
  }

  return { list, get, create, update, remove }
}
