// 自动化规则和场景 API

import { request, type ApiResponse } from './client';

// ========== 类型定义 ==========

/** 自动化规则 */
export interface AutomationRule {
  id: string;
  user_id: string;
  name: string;
  description: string;
  trigger_type: string;
  trigger_config: unknown;
  conditions: unknown;
  actions: unknown;
  enabled: boolean;
  last_triggered_at?: string | null;
  created_at: string;
  updated_at: string;
}

/** 自动化场景 */
export interface AutomationScene {
  id: string;
  user_id: string;
  name: string;
  icon: string;
  rule_ids: unknown;
  created_at: string;
  updated_at: string;
}

/** 创建规则请求 */
export interface CreateRuleRequest {
  name: string;
  description?: string;
  trigger_type: string;
  trigger_config?: unknown;
  conditions?: unknown;
  actions: unknown;
  enabled?: boolean;
}

/** 更新规则请求 */
export interface UpdateRuleRequest {
  name?: string;
  description?: string;
  trigger_type?: string;
  trigger_config?: unknown;
  conditions?: unknown;
  actions?: unknown;
  enabled?: boolean;
}

/** 创建场景请求 */
export interface CreateSceneRequest {
  name: string;
  icon?: string;
  rule_ids?: string[];
}

/** 更新场景请求 */
export interface UpdateSceneRequest {
  name?: string;
  icon?: string;
  rule_ids?: string[];
}

/** 规则列表响应 */
export interface RuleListResponse {
  rules: AutomationRule[];
  count: number;
}

/** 场景列表响应 */
export interface SceneListResponse {
  scenes: AutomationScene[];
  count: number;
}

// ========== 规则 API ==========

/**
 * 获取用户的所有规则
 */
export async function listRules(): Promise<ApiResponse<RuleListResponse>> {
  return request<RuleListResponse>('/automation/rules');
}

/**
 * 创建新规则
 */
export async function createRule(data: CreateRuleRequest): Promise<ApiResponse<{ success: boolean; rule: AutomationRule }>> {
  return request<{ success: boolean; rule: AutomationRule }>('/automation/rules', {
    method: 'POST',
    body: data,
  });
}

/**
 * 获取单条规则
 */
export async function getRule(id: string): Promise<ApiResponse<{ rule: AutomationRule }>> {
  return request<{ rule: AutomationRule }>(`/automation/rules/${id}`);
}

/**
 * 更新规则
 */
export async function updateRule(id: string, data: UpdateRuleRequest): Promise<ApiResponse<{ success: boolean; rule: AutomationRule }>> {
  return request<{ success: boolean; rule: AutomationRule }>(`/automation/rules/${id}`, {
    method: 'PUT',
    body: data,
  });
}

/**
 * 删除规则
 */
export async function deleteRule(id: string): Promise<ApiResponse<{ success: boolean }>> {
  return request<{ success: boolean }>(`/automation/rules/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 手动触发规则
 */
export async function triggerRule(id: string): Promise<ApiResponse<{ success: boolean; message: string }>> {
  return request<{ success: boolean; message: string }>(`/automation/rules/${id}/trigger`, {
    method: 'POST',
  });
}

/**
 * 切换规则启用状态
 */
export async function toggleRule(id: string, enabled: boolean): Promise<ApiResponse<{ success: boolean; rule: AutomationRule }>> {
  return updateRule(id, { enabled });
}

// ========== 场景 API ==========

/**
 * 获取用户的所有场景
 */
export async function listScenes(): Promise<ApiResponse<SceneListResponse>> {
  return request<SceneListResponse>('/automation/scenes');
}

/**
 * 创建新场景
 */
export async function createScene(data: CreateSceneRequest): Promise<ApiResponse<{ success: boolean; scene: AutomationScene }>> {
  return request<{ success: boolean; scene: AutomationScene }>('/automation/scenes', {
    method: 'POST',
    body: data,
  });
}

/**
 * 获取单个场景
 */
export async function getScene(id: string): Promise<ApiResponse<{ scene: AutomationScene }>> {
  return request<{ scene: AutomationScene }>(`/automation/scenes/${id}`);
}

/**
 * 更新场景
 */
export async function updateScene(id: string, data: UpdateSceneRequest): Promise<ApiResponse<{ success: boolean; scene: AutomationScene }>> {
  return request<{ success: boolean; scene: AutomationScene }>(`/automation/scenes/${id}`, {
    method: 'PUT',
    body: data,
  });
}

/**
 * 删除场景
 */
export async function deleteScene(id: string): Promise<ApiResponse<{ success: boolean }>> {
  return request<{ success: boolean }>(`/automation/scenes/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 手动执行场景
 */
export async function executeScene(id: string): Promise<ApiResponse<{ success: boolean; message: string }>> {
  return request<{ success: boolean; message: string }>(`/automation/scenes/${id}/execute`, {
    method: 'POST',
  });
}
