import prisma from '../lib/prisma'

/**
 * 规章制度民主程序服务
 */

const DEMOCRACY_STEPS = [
  { step: 1, name: '起草制度', description: 'HR 起草规章制度文本' },
  { step: 2, name: '职工讨论', description: '提交职工代表大会或全体职工讨论，提出方案和意见' },
  { step: 3, name: '平等协商', description: '与工会或职工代表平等协商确定' },
  { step: 4, name: '公示告知', description: '向全体员工公示告知（公告栏/邮件/培训签收等）' },
]

/**
 * 初始化民主程序进度
 */
export function initDemocracyProgress() {
  return {
    currentStep: 1,
    steps: DEMOCRACY_STEPS.map(s => ({
      ...s,
      status: s.step === 1 ? 'IN_PROGRESS' : 'PENDING',
      date: s.step === 1 ? new Date().toISOString().slice(0, 10) : null,
      note: null,
    })),
  }
}

/**
 * 更新民主程序步骤
 */
export function updateDemocracyStep(progress: any, targetStep: number, note?: string) {
  const steps = progress.steps.map((s: any) => {
    if (s.step < targetStep) {
      return { ...s, status: 'COMPLETED' }
    } else if (s.step === targetStep) {
      return { ...s, status: 'COMPLETED', date: new Date().toISOString().slice(0, 10), note: note || s.note }
    }
    return s
  })

  // 设置下一步为进行中
  if (targetStep < DEMOCRACY_STEPS.length) {
    const nextIdx = steps.findIndex((s: any) => s.step === targetStep + 1)
    if (nextIdx >= 0) {
      steps[nextIdx] = { ...steps[nextIdx], status: 'IN_PROGRESS', date: new Date().toISOString().slice(0, 10) }
    }
  }

  return {
    currentStep: targetStep >= DEMOCRACY_STEPS.length ? DEMOCRACY_STEPS.length : targetStep + 1,
    steps,
  }
}

/**
 * 创建制度文档
 */
export async function createPolicy(orgId: string, userId: string, data: { title: string; content: string; type?: string }) {
  return prisma.policyDocument.create({
    data: {
      orgId,
      title: data.title,
      content: data.content,
      type: data.type || 'RULES',
      status: 'DRAFT',
      democracyProgress: initDemocracyProgress(),
      createdBy: userId,
    },
  })
}

/**
 * 获取制度列表（含阅读签收统计）
 */
export async function getPolicies(orgId: string, status?: string, page?: number, pageSize?: number) {
  const where: any = { orgId }
  if (status) where.status = status
  const hasPagination = page && pageSize
  const [policies, totalEmployees, total] = await Promise.all([
    prisma.policyDocument.findMany({
      where,
      orderBy: { updatedAt: 'desc' },
      include: {
        _count: { select: { readRecords: true } },
      },
      ...(hasPagination ? { skip: (page! - 1) * pageSize!, take: pageSize! } : {}),
    }),
    prisma.employee.count({ where: { orgId, status: 'ACTIVE' } }),
    hasPagination ? prisma.policyDocument.count({ where }) : Promise.resolve(0),
  ])
  const items = policies.map(p => ({
    ...p,
    readCount: p._count?.readRecords || 0,
    totalEmployees,
  }))
  if (hasPagination) {
    return { items, total, page, pageSize, totalPages: Math.ceil(total / pageSize!) }
  }
  return items
}

/**
 * 获取制度详情
 */
export async function getPolicyDetail(orgId: string, id: string) {
  const policy = await prisma.policyDocument.findFirst({ where: { id, orgId } })
  if (!policy) {
    throw { code: 'NOT_FOUND', message: '制度文档不存在' }
  }
  return policy
}

/**
 * 更新制度
 */
export async function updatePolicy(orgId: string, id: string, data: { title?: string; content?: string; type?: string }) {
  const policy = await prisma.policyDocument.findFirst({ where: { id, orgId } })
  if (!policy) {
    throw { code: 'NOT_FOUND', message: '制度文档不存在' }
  }
  if (policy.status === 'PUBLISHED') {
    throw { code: 'CONFLICT', message: '已公示的制度不可编辑' }
  }

  const updateData: any = {}
  if (data.title !== undefined) updateData.title = data.title
  if (data.content !== undefined) updateData.content = data.content
  if (data.type !== undefined) updateData.type = data.type

  return prisma.policyDocument.update({ where: { id }, data: updateData })
}

/**
 * 推进民主程序步骤
 */
export async function advanceDemocracyStep(orgId: string, id: string, step: number, note?: string) {
  const policy = await prisma.policyDocument.findFirst({ where: { id, orgId } })
  if (!policy) {
    throw { code: 'NOT_FOUND', message: '制度文档不存在' }
  }
  if (policy.status === 'PUBLISHED') {
    throw { code: 'CONFLICT', message: '已公示的制度不可修改' }
  }

  const progress = updateDemocracyStep(policy.democracyProgress, step, note)
  const status = step >= 4 ? 'PUBLISHED' : step >= 3 ? 'CONSULTING' : step >= 2 ? 'DISCUSSING' : 'DRAFT'

  return prisma.policyDocument.update({
    where: { id },
    data: {
      democracyProgress: progress,
      status,
      publishedAt: step >= 4 ? new Date() : null,
    },
  })
}

/**
 * 删除制度
 */
export async function deletePolicy(orgId: string, id: string) {
  const policy = await prisma.policyDocument.findFirst({ where: { id, orgId } })
  if (!policy) {
    throw { code: 'NOT_FOUND', message: '制度文档不存在' }
  }
  if (policy.status === 'PUBLISHED') {
    throw { code: 'CONFLICT', message: '已公示的制度不可删除' }
  }
  return prisma.policyDocument.delete({ where: { id } })
}
