// plugins/vee-validate.ts
import { defineNuxtPlugin } from '#app';
import { defineRule, configure, Form, Field, ErrorMessage } from 'vee-validate'; // Import components if registering globally
import {
  required,
  min,
  max,
  alpha_spaces as alphaSpaces,
  email,
  min_value as minValue,
  max_value as maxValue,
  confirmed,
  not_one_of as excluded,
} from '@vee-validate/rules';

export default defineNuxtPlugin((nuxtApp) => {
  // --- Global Component Registration (Optional in Nuxt 3) ---
  // Nuxt 3 often uses auto-imports or explicit imports in components.
  // You *can* register them globally like this if you prefer:
  // nuxtApp.vueApp.component('VeeForm', Form);
  // nuxtApp.vueApp.component('VeeField', Field);
  // nuxtApp.vueApp.component('ErrorMessage', ErrorMessage);
  // If you don't register globally, import them in your .vue files:
  // import { Form as VeeForm, Field as VeeField, ErrorMessage } from 'vee-validate';

  // --- Define Validation Rules ---
  // These are directly copied from your previous plugin
  defineRule('required', required);
  defineRule('tos', required); // Your custom rule using the 'required' function
  defineRule('min', min);
  defineRule('max', max);
  defineRule('alpha_spaces', alphaSpaces);
  defineRule('email', email);
  defineRule('min_value', minValue);
  defineRule('max_value', maxValue);
  defineRule('passwords_mismatch', confirmed); // Rule for password confirmation
  defineRule('excluded', excluded); // Rule for excluding specific values
  defineRule('membership_excluded', excluded); // Another custom rule using 'excluded'

  // --- Configure VeeValidate ---
  // Using your custom message generator and validation triggers
  configure({
    generateMessage: (ctx) => {
      // You might want to provide more user-friendly names for fields
      // This usually involves mapping field names or passing metadata
      const field = ctx.field; // The 'name' attribute of the VeeField

      // Your custom messages (make sure this object is complete)
      const messages: Record<string, string> = {
        // Examples from your previous code:
        required: `[${field}] 항목은 필수 입력 값입니다.`, // Using Korean based on context
        min: `[${field}] 항목은 최소 길이를 만족해야 합니다.`, // Example for min length
        max: `[${field}] 항목은 최대 길이를 초과할 수 없습니다.`, // Example for max length
        email: `[${field}] 항목은 유효한 이메일 주소여야 합니다.`, // Example for email
        passwords_mismatch: `비밀번호가 일치하지 않습니다.`, // Specific message, field name might not be needed
        // Add other messages for alpha_spaces, min_value, max_value, excluded, etc.
      };

      // Lookup the message, fallback to a generic one
      const message =
        messages[ctx.rule?.name || ''] ||
        `[${field}] 항목이 유효하지 않습니다.`; // Generic fallback
      return message;
    },
    // Your chosen validation triggers:
    validateOnBlur: true, // Validate when the field loses focus
    validateOnChange: true, // Validate when the value changes (and field is blurred/submitted)
    validateOnInput: false, // Do NOT validate on every keystroke
    validateOnModelUpdate: true, // Validate when v-model updates (often overlaps with other triggers)
  });
});
