'use client'

import { useMemo, useCallback } from 'react'
import { useCompliance } from './hooks-core'
import type { Control, Evidence, Risk } from '@breakpilot/compliance-sdk-types'

// =============================================================================
// COMPLIANCE HOOKS
// =============================================================================

export function useComplianceModule() {
  const { compliance, state } = useCompliance()

  return useMemo(
    () => ({
      // Controls
      controls: state.controls,
      getControlById: compliance.getControlById.bind(compliance),
      getControlsByDomain: compliance.getControlsByDomain.bind(compliance),
      getControlsByStatus: compliance.getControlsByStatus.bind(compliance),
      controlComplianceRate: compliance.getControlComplianceRate(),

      // Evidence
      evidence: state.evidence,
      getEvidenceByControlId: compliance.getEvidenceByControlId.bind(compliance),
      expiringEvidence: compliance.getExpiringEvidence(),

      // Requirements
      requirements: state.requirements,
      getRequirementsByRegulation: compliance.getRequirementsByRegulation.bind(compliance),
      requirementComplianceRate: compliance.getRequirementComplianceRate(),

      // Obligations
      obligations: state.obligations,
      upcomingObligations: compliance.getUpcomingObligations(),
      overdueObligations: compliance.getOverdueObligations(),

      // AI Act
      aiActClassification: state.aiActClassification,
      aiActRiskCategory: compliance.getAIActRiskCategory(),
      isHighRiskAI: compliance.isHighRiskAI(),

      // Score
      complianceScore: compliance.calculateComplianceScore(),

      // Risks
      risks: state.risks,
      criticalRisks: compliance.getCriticalRisks(),
      averageRiskScore: compliance.getAverageRiskScore(),
    }),
    [compliance, state]
  )
}

export function useControls() {
  const { compliance, state, dispatch } = useCompliance()

  const addControl = useCallback(
    (control: Control) => {
      dispatch({ type: 'ADD_CONTROL', payload: control })
    },
    [dispatch]
  )

  const updateControl = useCallback(
    (id: string, data: Partial<Control>) => {
      dispatch({ type: 'UPDATE_CONTROL', payload: { id, data } })
    },
    [dispatch]
  )

  return useMemo(
    () => ({
      controls: state.controls,
      addControl,
      updateControl,
      getById: compliance.getControlById.bind(compliance),
      getByDomain: compliance.getControlsByDomain.bind(compliance),
      getByStatus: compliance.getControlsByStatus.bind(compliance),
      implementedCount: state.controls.filter(c => c.implementationStatus === 'IMPLEMENTED').length,
      totalCount: state.controls.length,
      complianceRate: compliance.getControlComplianceRate(),
    }),
    [state.controls, compliance, addControl, updateControl]
  )
}

export function useEvidence() {
  const { compliance, state, dispatch } = useCompliance()

  const addEvidence = useCallback(
    (evidence: Evidence) => {
      dispatch({ type: 'ADD_EVIDENCE', payload: evidence })
    },
    [dispatch]
  )

  const updateEvidence = useCallback(
    (id: string, data: Partial<Evidence>) => {
      dispatch({ type: 'UPDATE_EVIDENCE', payload: { id, data } })
    },
    [dispatch]
  )

  const deleteEvidence = useCallback(
    (id: string) => {
      dispatch({ type: 'DELETE_EVIDENCE', payload: id })
    },
    [dispatch]
  )

  return useMemo(
    () => ({
      evidence: state.evidence,
      addEvidence,
      updateEvidence,
      deleteEvidence,
      getByControlId: compliance.getEvidenceByControlId.bind(compliance),
      expiringEvidence: compliance.getExpiringEvidence(),
      activeCount: state.evidence.filter(e => e.status === 'ACTIVE').length,
      totalCount: state.evidence.length,
    }),
    [state.evidence, compliance, addEvidence, updateEvidence, deleteEvidence]
  )
}

export function useRisks() {
  const { compliance, state, dispatch } = useCompliance()

  const addRisk = useCallback(
    (risk: Risk) => {
      dispatch({ type: 'ADD_RISK', payload: risk })
    },
    [dispatch]
  )

  const updateRisk = useCallback(
    (id: string, data: Partial<Risk>) => {
      dispatch({ type: 'UPDATE_RISK', payload: { id, data } })
    },
    [dispatch]
  )

  const deleteRisk = useCallback(
    (id: string) => {
      dispatch({ type: 'DELETE_RISK', payload: id })
    },
    [dispatch]
  )

  return useMemo(
    () => ({
      risks: state.risks,
      addRisk,
      updateRisk,
      deleteRisk,
      criticalRisks: compliance.getCriticalRisks(),
      getByStatus: compliance.getRisksByStatus.bind(compliance),
      getBySeverity: compliance.getRisksBySeverity.bind(compliance),
      averageScore: compliance.getAverageRiskScore(),
    }),
    [state.risks, compliance, addRisk, updateRisk, deleteRisk]
  )
}
