import { computed } from 'vue'

import { isFinanceUser, isManagerUser, isPlatformAdminUser } from '../../utils/accessControl.js'
import { normalizeText } from './auditViewModel.js'

export function useAuditViewPermissions({
  currentUser,
  selectedSkill,
  actionState
}) {
  const detailBusy = computed(() => Boolean(actionState.value))
  const isAdmin = computed(() => isPlatformAdminUser(currentUser.value))
  const isRuleManager = computed(() => isManagerUser(currentUser.value))
  const isFinance = computed(() => isFinanceUser(currentUser.value))
  const selectedSkillIsRule = computed(() => selectedSkill.value?.type === 'rules')
  const selectedSkillUsesSpreadsheet = computed(
    () => selectedSkillIsRule.value && Boolean(selectedSkill.value?.usesSpreadsheetRule)
  )
  const selectedSkillUsesJsonRisk = computed(
    () => selectedSkillIsRule.value && Boolean(selectedSkill.value?.usesJsonRiskRule)
  )
  const canManageSelected = computed(
    () => isRuleManager.value && Boolean(selectedSkill.value) && !selectedSkill.value?.isPreviewMock
  )
  const canAdminOperateSelected = computed(
    () => isAdmin.value && Boolean(selectedSkill.value) && !selectedSkill.value?.isPreviewMock
  )
  const canEditSelected = computed(
    () =>
      Boolean(selectedSkill.value) &&
      !selectedSkill.value?.isPreviewMock &&
      (isAdmin.value || isFinance.value)
  )
  const latestRiskRuleTestSummary = computed(() => selectedSkill.value?.latestTestSummary || null)
  const riskRuleTestPassed = computed(() => Boolean(latestRiskRuleTestSummary.value?.test_passed))
  const riskRuleInReview = computed(
    () => selectedSkillUsesJsonRisk.value && selectedSkill.value?.statusValue === 'review'
  )
  const riskRuleGenerationBusy = computed(
    () => selectedSkillUsesJsonRisk.value && selectedSkill.value?.statusValue === 'generating'
  )
  const riskRuleGenerationFailed = computed(
    () => selectedSkillUsesJsonRisk.value && selectedSkill.value?.statusValue === 'failed'
  )
  const canOpenRiskRuleTest = computed(
    () =>
      selectedSkillUsesJsonRisk.value &&
      canAdminOperateSelected.value &&
      Boolean(selectedSkill.value?.id) &&
      !riskRuleGenerationBusy.value &&
      !riskRuleGenerationFailed.value
  )
  const canDeleteRiskRule = computed(
    () =>
      selectedSkillUsesJsonRisk.value &&
      canAdminOperateSelected.value &&
      Boolean(selectedSkill.value?.id) &&
      !normalizeText(selectedSkill.value?.publishedVersion).replace('-', '')
  )
  const canOpenRiskRuleReviewSubmit = computed(() => false)
  const canSubmitRiskRuleReview = computed(
    () =>
      canOpenRiskRuleReviewSubmit.value &&
      riskRuleTestPassed.value
  )
  const canReturnRiskRule = computed(() => false)
  const riskRuleHasPublishableRevision = computed(() => {
    const revision = selectedSkill.value?.configJson?.revision_draft
    return selectedSkillUsesJsonRisk.value && revision &&
      revision.generation_status === 'completed' &&
      normalizeText(selectedSkill.value?.workingVersion).replace('-', '') &&
      selectedSkill.value?.workingVersion !== selectedSkill.value?.publishedVersion
  })
  const canPublishRiskRule = computed(
    () =>
      Boolean(riskRuleHasPublishableRevision.value) &&
      canManageSelected.value &&
      riskRuleTestPassed.value &&
      !detailBusy.value
  )
  const canToggleRiskRuleEnabled = computed(
    () => selectedSkillUsesJsonRisk.value && canManageSelected.value
  )
  const canEditRiskRuleDraft = computed(
    () =>
      selectedSkillUsesJsonRisk.value &&
      (canEditSelected.value || canManageSelected.value) &&
      !detailBusy.value &&
      !riskRuleGenerationBusy.value &&
      !normalizeText(selectedSkill.value?.publishedVersion).replace('-', '')
  )
  const canCreateRiskRuleRevision = computed(
    () =>
      selectedSkillUsesJsonRisk.value &&
      (canEditSelected.value || canManageSelected.value) &&
      !detailBusy.value &&
      !riskRuleGenerationBusy.value &&
      !riskRuleGenerationFailed.value &&
      Boolean(normalizeText(selectedSkill.value?.publishedVersion).replace('-', ''))
  )
  const canEditMarkdown = computed(() => selectedSkillIsRule.value && canEditSelected.value)
  const isDisplayingWorkingVersion = computed(
    () => selectedSkill.value?.displayVersion === selectedSkill.value?.workingVersion
  )
  const canUploadSpreadsheet = computed(
    () =>
      canEditSelected.value &&
      selectedSkillUsesSpreadsheet.value &&
      !detailBusy.value
  )
  const canDownloadSpreadsheet = computed(
    () =>
      selectedSkillUsesSpreadsheet.value &&
      Boolean(selectedSkill.value?.id) &&
      !detailBusy.value
  )
  const canEditSpreadsheetInline = computed(
    () =>
      selectedSkillUsesSpreadsheet.value &&
      (selectedSkill.value?.isPreviewMock || canEditSelected.value)
  )
  const selectedSpreadsheetFileName = computed(
    () =>
      normalizeText(selectedSkill.value?.ruleDocument?.file_name) || '未上传规则表'
  )

  return {
    detailBusy,
    isAdmin,
    selectedSkillIsRule,
    selectedSkillUsesSpreadsheet,
    selectedSkillUsesJsonRisk,
    canManageSelected,
    canAdminOperateSelected,
    canEditSelected,
    latestRiskRuleTestSummary,
    riskRuleTestPassed,
    riskRuleInReview,
    riskRuleGenerationBusy,
    riskRuleGenerationFailed,
    canOpenRiskRuleTest,
    canDeleteRiskRule,
    canOpenRiskRuleReviewSubmit,
    canSubmitRiskRuleReview,
    canReturnRiskRule,
    riskRuleHasPublishableRevision,
    canPublishRiskRule,
    canToggleRiskRuleEnabled,
    canEditRiskRuleDraft,
    canCreateRiskRuleRevision,
    canEditMarkdown,
    isDisplayingWorkingVersion,
    canUploadSpreadsheet,
    canDownloadSpreadsheet,
    canEditSpreadsheetInline,
    selectedSpreadsheetFileName
  }
}
