import { useQuery, useMutation } from '@apollo/client'
import {
  GET_COUNTRIES,
  GET_COUNTRY,
  GET_NETWORK_TOPOLOGIES,
  GET_NETWORK_TOPOLOGY,
  GET_COMPLIANCE_REQUIREMENTS,
  GET_COMPLIANCE_REQUIREMENT,
  GET_DEPLOYMENT_MILESTONES,
  GET_DEPLOYMENT_MILESTONE,
  GET_COST_ESTIMATES,
  GET_COST_ESTIMATE,
  GET_INFRASTRUCTURE_SUMMARY,
  UPDATE_NETWORK_TOPOLOGY,
  CREATE_DEPLOYMENT_MILESTONE,
  UPDATE_DEPLOYMENT_MILESTONE,
  DELETE_DEPLOYMENT_MILESTONE,
  UPDATE_COMPLIANCE_REQUIREMENT,
  UPDATE_COST_ESTIMATE,
} from '../queries/infrastructure'

export function useCountries(filter?: {
  region?: string
  priority?: string
  relationshipType?: string
}) {
  return useQuery(GET_COUNTRIES, {
    variables: { filter },
    fetchPolicy: 'cache-and-network',
  })
}

export function useCountry(name: string) {
  return useQuery(GET_COUNTRY, {
    variables: { name },
    skip: !name,
  })
}

export function useNetworkTopologies(filter?: { region?: string; entity?: string }) {
  return useQuery(GET_NETWORK_TOPOLOGIES, {
    variables: { filter },
    fetchPolicy: 'cache-and-network',
  })
}

export function useNetworkTopology(id: string) {
  return useQuery(GET_NETWORK_TOPOLOGY, {
    variables: { id },
    skip: !id,
  })
}

export function useComplianceRequirements(filter?: {
  country?: string
  region?: string
  status?: string
  framework?: string
}) {
  return useQuery(GET_COMPLIANCE_REQUIREMENTS, {
    variables: { filter },
    fetchPolicy: 'cache-and-network',
  })
}

export function useComplianceRequirement(country: string) {
  return useQuery(GET_COMPLIANCE_REQUIREMENT, {
    variables: { country },
    skip: !country,
  })
}

export function useDeploymentMilestones(filter?: {
  region?: string
  entity?: string
  priority?: string
  status?: string
}) {
  return useQuery(GET_DEPLOYMENT_MILESTONES, {
    variables: { filter },
    fetchPolicy: 'cache-and-network',
  })
}

export function useDeploymentMilestone(id: string) {
  return useQuery(GET_DEPLOYMENT_MILESTONE, {
    variables: { id },
    skip: !id,
  })
}

export function useCostEstimates(filter?: {
  region?: string
  entity?: string
  category?: string
}) {
  return useQuery(GET_COST_ESTIMATES, {
    variables: { filter },
    fetchPolicy: 'cache-and-network',
  })
}

export function useCostEstimate(region: string, entity: string, category: string) {
  return useQuery(GET_COST_ESTIMATE, {
    variables: { region, entity, category },
    skip: !region || !entity || !category,
  })
}

export function useInfrastructureSummary() {
  return useQuery(GET_INFRASTRUCTURE_SUMMARY, {
    fetchPolicy: 'cache-and-network',
  })
}

export function useUpdateNetworkTopology() {
  const [mutate, { loading, error }] = useMutation(UPDATE_NETWORK_TOPOLOGY, {
    refetchQueries: [{ query: GET_NETWORK_TOPOLOGIES }],
  })

  return {
    updateTopology: mutate,
    loading,
    error,
  }
}

export function useCreateDeploymentMilestone() {
  const [mutate, { loading, error }] = useMutation(CREATE_DEPLOYMENT_MILESTONE, {
    refetchQueries: [{ query: GET_DEPLOYMENT_MILESTONES }],
  })

  return {
    createMilestone: mutate,
    loading,
    error,
  }
}

export function useUpdateDeploymentMilestone() {
  const [mutate, { loading, error }] = useMutation(UPDATE_DEPLOYMENT_MILESTONE, {
    refetchQueries: [{ query: GET_DEPLOYMENT_MILESTONES }],
  })

  return {
    updateMilestone: mutate,
    loading,
    error,
  }
}

export function useDeleteDeploymentMilestone() {
  const [mutate, { loading, error }] = useMutation(DELETE_DEPLOYMENT_MILESTONE, {
    refetchQueries: [{ query: GET_DEPLOYMENT_MILESTONES }],
  })

  return {
    deleteMilestone: mutate,
    loading,
    error,
  }
}

export function useUpdateComplianceRequirement() {
  const [mutate, { loading, error }] = useMutation(UPDATE_COMPLIANCE_REQUIREMENT, {
    refetchQueries: [{ query: GET_COMPLIANCE_REQUIREMENTS }],
  })

  return {
    updateCompliance: mutate,
    loading,
    error,
  }
}

export function useUpdateCostEstimate() {
  const [mutate, { loading, error }] = useMutation(UPDATE_COST_ESTIMATE, {
    refetchQueries: [{ query: GET_COST_ESTIMATES }],
  })

  return {
    updateCostEstimate: mutate,
    loading,
    error,
  }
}

