/**
 * 功能测试报告模板配置 API 接口
 *
 * @module api/reportTemplate
 * @author czj
 * @date 2026-08-13
 */

import request from '@/utils/request'

/** 报告章节开关键集合 */
export type ReportChapterKey = 'report_basic_info' | 'execution_summary' | 'test_scope' | 'case_details' | 'bug_list' | 'chart_analysis' | 'execution_records' | 'attachments' | 'risk_assessment' | 'appendix'

/** 报告章节开关 */
export type ReportChapters = Record<ReportChapterKey, boolean>

/** 模板配置结构（与后端 config.py 的 get_default_config() 对齐） */
export interface ReportTemplateConfig {
  /** 章节开关 */
  chapters: ReportChapters
  /** 报告默认值 */
  defaults: {
    test_lead: string
    tester: string
    test_env: string
    system_version: string
    server_address: string
    deployment_env: string
  }
  /** Word 样式 */
  word_style: {
    font_name: string
    font_size: number
    title_level_1_size: number
    title_level_2_size: number
    title_level_3_size: number
    title_level_4_size: number
    paragraph_first_line_indent: number
    title_color: string
  }
  /** 图表样式 */
  chart_style: {
    dpi: number
    figure_width: number
    figure_height: number
    chinese_font: string
    colors: {
      pass: string
      fail: string
      unverified: string
      undeveloped: string
      bug_1: string
      bug_2: string
      bug_3: string
      bug_4: string
    }
  }
}

/** 模板列表项（不含 config 详情） */
export interface ReportTemplateListItem {
  id: number
  name: string
  is_default: boolean
  updated_at?: string
}

/** 模板完整响应 */
export interface ReportTemplateResponse {
  id: number
  name: string
  is_default: boolean
  config: ReportTemplateConfig
  created_at?: string
  updated_at?: string
}

/** 创建模板请求 */
export interface ReportTemplateCreateRequest {
  name: string
  is_default?: boolean
  config: ReportTemplateConfig
}

/** 更新模板请求 */
export interface ReportTemplateUpdateRequest {
  name?: string
  is_default?: boolean
  config?: ReportTemplateConfig
}

/**
 * 报告模板 API 服务
 */
export const reportTemplateApi = {
  /**
   * 获取模板列表
   */
  async list(): Promise<ReportTemplateListItem[]> {
    const response = await request.get('/api/functional-report/templates')
    return response as any
  },

  /**
   * 获取单个模板详情
   * @param id 模板 ID
   */
  async get(id: number): Promise<ReportTemplateResponse> {
    const response = await request.get(`/api/functional-report/templates/${id}`)
    return response as any
  },

  /**
   * 创建模板
   * @param data 创建请求
   */
  async create(data: ReportTemplateCreateRequest): Promise<ReportTemplateResponse> {
    const response = await request.post('/api/functional-report/templates', data)
    return response as any
  },

  /**
   * 更新模板
   * @param id 模板 ID
   * @param data 更新请求
   */
  async update(id: number, data: ReportTemplateUpdateRequest): Promise<ReportTemplateResponse> {
    const response = await request.put(`/api/functional-report/templates/${id}`, data)
    return response as any
  },

  /**
   * 删除模板
   * @param id 模板 ID
   */
  async delete(id: number): Promise<void> {
    const response = await request.delete(`/api/functional-report/templates/${id}`)
    return response as any
  },

  /**
   * 设为默认模板
   * @param id 模板 ID
   */
  async setDefault(id: number): Promise<ReportTemplateResponse> {
    const response = await request.put(`/api/functional-report/templates/${id}/default`)
    return response as any
  }
}