import { workflowRequestClient } from '#/api/request';

// --- Enums ---

export enum FormStatus {
  Draft = 0,
  Published = 1,
  Disabled = 2,
}

// --- Types ---

export interface FormDefinitionDto {
  id: string;
  name: string;
  code: string;
  description: string | null;
  version: number;
  status: FormStatus;
}

export interface FormDefinitionDetailDto extends FormDefinitionDto {
  schemaJson: string | null;
}

export interface PagedFormResult {
  items: FormDefinitionDto[];
  total: number;
}

export interface FormDataDto {
  id: string;
  formDefinitionId: string;
  instanceId: string;
  dataJson: string;
}

// --- Form Definition CRUD ---

export function getFormDefinitionsApi(params?: {
  pageIndex?: number;
  pageSize?: number;
  status?: FormStatus;
}) {
  return workflowRequestClient.get<PagedFormResult>('/forms', { params });
}

export function getFormDefinitionByIdApi(id: string) {
  return workflowRequestClient.get<FormDefinitionDetailDto>(`/forms/${id}`);
}

export function createFormDefinitionApi(data: {
  name: string;
  code: string;
  description?: string;
  schemaJson: string;
}) {
  return workflowRequestClient.post<FormDefinitionDto>('/forms', data);
}

export function updateFormDefinitionApi(
  id: string,
  data: {
    name: string;
    description?: string;
    schemaJson: string;
  },
) {
  return workflowRequestClient.put<FormDefinitionDto>(`/forms/${id}`, data);
}

export function deleteFormDefinitionApi(id: string) {
  return workflowRequestClient.delete(`/forms/${id}`);
}

export function publishFormDefinitionApi(id: string) {
  return workflowRequestClient.post(`/forms/${id}/publish`);
}

export function disableFormDefinitionApi(id: string) {
  return workflowRequestClient.post(`/forms/${id}/disable`);
}

// --- Form Data ---

export function submitFormDataApi(data: {
  formDefinitionId: string;
  instanceId: string;
  dataJson: string;
}) {
  return workflowRequestClient.post<string>('/forms/data', data);
}

export function getFormDataByInstanceApi(instanceId: string) {
  return workflowRequestClient.get<FormDataDto | null>(
    `/forms/data/${instanceId}`,
  );
}

// --- Component Registry ---

export interface FormComponentDto {
  id: string;
  name: string;
  displayName: string;
  category: string;
  icon: string;
  defaultSchema: string;
  supportedProps: string;
  isActive: boolean;
  sortOrder: number;
}

export function getFormComponentsApi(params?: {
  category?: string;
  isActive?: boolean;
}) {
  return workflowRequestClient.get<FormComponentDto[]>('/form-components', {
    params,
  });
}

export function getFormComponentByIdApi(id: string) {
  return workflowRequestClient.get<FormComponentDto>(`/form-components/${id}`);
}

export function createFormComponentApi(data: {
  name: string;
  displayName: string;
  category: string;
  icon: string;
  defaultSchema: string;
  supportedProps?: string;
}) {
  return workflowRequestClient.post<FormComponentDto>('/form-components', data);
}

export function updateFormComponentApi(
  id: string,
  data: {
    displayName: string;
    category: string;
    icon: string;
    defaultSchema: string;
    supportedProps?: string;
    isActive?: boolean;
  },
) {
  return workflowRequestClient.put<FormComponentDto>(
    `/form-components/${id}`,
    data,
  );
}

export function deleteFormComponentApi(id: string) {
  return workflowRequestClient.delete(`/form-components/${id}`);
}
