import client from '../client';
import type { PaginatedResponse } from '../types';

export type FollowUpType = 'phone' | 'outpatient' | 'home_visit' | 'online' | 'wechat';
export type TemplateStatus = 'active' | 'draft' | 'archived';

export interface TemplateField {
  id: string;
  template_id: string;
  label: string;
  field_key: string;
  field_type: string;
  required: boolean;
  options?: string;
  placeholder?: string;
  validation?: string;
  sort_order: number;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface TemplateFieldReq {
  label: string;
  field_key: string;
  field_type: string;
  required?: boolean;
  options?: string;
  placeholder?: string;
  validation?: string;
  sort_order?: number;
}

export interface FollowUpTemplate {
  id: string;
  name: string;
  description?: string;
  follow_up_type: string;
  applicable_scope?: string;
  status: string;
  fields: TemplateField[];
  created_at: string;
  updated_at: string;
  version: number;
}

export interface FollowUpTemplateListItem {
  id: string;
  name: string;
  description?: string;
  follow_up_type: string;
  status: string;
  field_count: number;
  created_at: string;
  updated_at: string;
  version: number;
}

export interface CreateTemplateReq {
  name: string;
  description?: string;
  follow_up_type: string;
  applicable_scope?: string;
  fields: TemplateFieldReq[];
}

export interface UpdateTemplateReq {
  name?: string;
  description?: string;
  follow_up_type?: string;
  applicable_scope?: string;
  status?: string;
  fields?: TemplateFieldReq[];
}

export const followUpTemplateApi = {
  list: async (params?: {
    page?: number;
    page_size?: number;
    follow_up_type?: string;
    status?: string;
  }) => {
    const { data } = await client.get<{
      success: boolean;
      data: PaginatedResponse<FollowUpTemplateListItem>;
    }>('/health/follow-up-templates', { params });
    return data.data;
  },

  get: async (id: string) => {
    const { data } = await client.get<{
      success: boolean;
      data: FollowUpTemplate;
    }>(`/health/follow-up-templates/${id}`);
    return data.data;
  },

  create: async (req: CreateTemplateReq) => {
    const { data } = await client.post<{
      success: boolean;
      data: FollowUpTemplate;
    }>('/health/follow-up-templates', req);
    return data.data;
  },

  update: async (id: string, req: UpdateTemplateReq & { version: number }) => {
    const { data } = await client.put<{
      success: boolean;
      data: FollowUpTemplate;
    }>(`/health/follow-up-templates/${id}`, req);
    return data.data;
  },

  delete: async (id: string, version: number) => {
    await client.delete(`/health/follow-up-templates/${id}`, {
      data: { version },
    });
  },
};
