import { defineStore } from 'pinia'
import { ref, reactive } from 'vue'
import { useValidationStore } from '@/stores/validationStore'

export const useIzinBedelOraniValidationStore = defineStore(
  'izinBedelOraniValidationStore',
  () => {
    const validationStore = useValidationStore()

    const formChanged = ref<boolean>(false)
    const isFormValid = ref<boolean>(true)
    const invalidTexts = reactive<Record<string, any>>({})

    const FormCheck = (panelData: Record<string, any>): boolean => {
      Object.assign(invalidTexts, {})

      validationStore.IsFieldEmpty(
        panelData,
        invalidTexts,
        'ad',
        'Ad alanı zorunludur'
      )

      validationStore.IsFieldEmpty(
        panelData,
        invalidTexts,
        'oran',
        'Oran alanı zorunludur'
      )

      validationStore.IsFieldEmpty(
        panelData,
        invalidTexts,
        'aciklama',
        'Açıklama alanı zorunludur'
      )

      // Oran validation (0-100 arası)
      if (panelData.oran !== undefined && panelData.oran !== '') {
        const oran = parseFloat(panelData.oran)
        if (isNaN(oran) || oran < 0 || oran > 100) {
          invalidTexts.oran = 'Oran 0-100 arasında olmalıdır'
        }
      }

      isFormValid.value = Object.keys(invalidTexts).length === 0
      return isFormValid.value
    }

    const ResetValidation = () => {
      Object.assign(invalidTexts, {})
      formChanged.value = false
      isFormValid.value = true
    }

    return {
      formChanged,
      isFormValid,
      invalidTexts,
      FormCheck,
      ResetValidation
    }
  }
)
