import { Controller } from "@hotwired/stimulus"

export default class WafPolicyFormController extends Controller {
  static targets = ["policyTypeSelect", "policyActionSelect", "countryTargets", "asnTargets",
                   "companyTargets", "networkTypeTargets", "redirectConfig", "challengeConfig"]

  connect() {
    this.updateTargetsVisibility()
    this.updateActionConfig()
  }

  updateTargetsVisibility() {
    const selectedType = this.policyTypeSelectTarget.value

    // Hide all target sections
    this.countryTargetsTarget.classList.add('hidden')
    this.asnTargetsTarget.classList.add('hidden')
    this.companyTargetsTarget.classList.add('hidden')
    this.networkTypeTargetsTarget.classList.add('hidden')

    // Show relevant target section
    switch(selectedType) {
      case 'country':
        this.countryTargetsTarget.classList.remove('hidden')
        break
      case 'asn':
        this.asnTargetsTarget.classList.remove('hidden')
        break
      case 'company':
        this.companyTargetsTarget.classList.remove('hidden')
        break
      case 'network_type':
        this.networkTypeTargetsTarget.classList.remove('hidden')
        break
    }
  }

  updateActionConfig() {
    const selectedAction = this.policyActionSelectTarget.value

    // Hide all config sections
    this.redirectConfigTarget.classList.add('hidden')
    this.challengeConfigTarget.classList.add('hidden')

    // Show relevant config section
    switch(selectedAction) {
      case 'redirect':
        this.redirectConfigTarget.classList.remove('hidden')
        break
      case 'challenge':
        this.challengeConfigTarget.classList.remove('hidden')
        break
    }
  }
}