import { apiRequest, buildUrl, type PaginatedResponse } from './base';
import type { Rule, RuleGroup } from '~/models/rule';

/**
 * 评查规则API服务
 */

interface RuleFilterParams {
  ruleType?: string;
  groupId?: string;
  isActive?: boolean;
  keyword?: string;
  page?: number;
  pageSize?: number;
}

// 获取评查点列表
export async function getRules(params?: RuleFilterParams): Promise<PaginatedResponse<Rule>> {
  const url = buildUrl('/api/rules', params);
  return apiRequest<PaginatedResponse<Rule>>(url);
}

// 获取单个评查点
export async function getRule(id: string): Promise<Rule> {
  const url = buildUrl(`/api/rules/${id}`);
  return apiRequest<Rule>(url);
}

// 创建评查点
export async function createRule(rule: Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>): Promise<Rule> {
  const url = buildUrl('/api/rules');
  return apiRequest<Rule>(url, 'POST', rule);
}

// 更新评查点
export async function updateRule(id: string, rule: Partial<Omit<Rule, 'id' | 'createdAt' | 'updatedAt'>>): Promise<Rule> {
  const url = buildUrl(`/api/rules/${id}`);
  return apiRequest<Rule>(url, 'PUT', rule);
}

// 删除评查点
export async function deleteRule(id: string): Promise<void> {
  const url = buildUrl(`/api/rules/${id}`);
  return apiRequest<void>(url, 'DELETE');
}

// 获取评查点分组列表
export async function getRuleGroups(): Promise<RuleGroup[]> {
  const url = buildUrl('/api/rule-groups');
  return apiRequest<RuleGroup[]>(url);
}

// 获取单个评查点分组
export async function getRuleGroup(id: string): Promise<RuleGroup> {
  const url = buildUrl(`/api/rule-groups/${id}`);
  return apiRequest<RuleGroup>(url);
}

// 创建评查点分组
export async function createRuleGroup(group: Omit<RuleGroup, 'id' | 'createdAt' | 'updatedAt'>): Promise<RuleGroup> {
  const url = buildUrl('/api/rule-groups');
  return apiRequest<RuleGroup>(url, 'POST', group);
}

// 更新评查点分组
export async function updateRuleGroup(id: string, group: Partial<Omit<RuleGroup, 'id' | 'createdAt' | 'updatedAt'>>): Promise<RuleGroup> {
  const url = buildUrl(`/api/rule-groups/${id}`);
  return apiRequest<RuleGroup>(url, 'PUT', group);
}

// 删除评查点分组
export async function deleteRuleGroup(id: string): Promise<void> {
  const url = buildUrl(`/api/rule-groups/${id}`);
  return apiRequest<void>(url, 'DELETE');
} 