<script setup lang="ts">
import { computed, ref } from 'vue'

export interface KsTextFieldProps {
  modelValue: string
  label?: string
  type?: 'text' | 'email' | 'password' | 'number' | 'tel' | 'url'
  placeholder?: string
  disabled?: boolean
  readonly?: boolean
  required?: boolean
  error?: string
  help?: string
  maxLength?: number
  minLength?: number
  pattern?: string
  autocomplete?: string
  inputId?: string
  size?: 'sm' | 'md' | 'lg'
}

const props = withDefaults(defineProps<KsTextFieldProps>(), {
  type: 'text',
  disabled: false,
  readonly: false,
  required: false,
  size: 'md',
})

const emit = defineEmits<{
  'update:modelValue': [value: string]
  blur: [event: FocusEvent]
  focus: [event: FocusEvent]
  input: [event: Event]
}>()

const isFocused = ref(false)
const id = computed(() => props.inputId || `input-${Math.random().toString(36).substr(2, 9)}`)

const containerClass = computed(() => [
  'ks-text-field',
  `ks-text-field--${props.size}`,
  {
    'ks-text-field--focused': isFocused.value,
    'ks-text-field--filled': props.modelValue,
    'ks-text-field--disabled': props.disabled,
    'ks-text-field--error': !!props.error,
    'ks-text-field--required': props.required,
  },
])

const handleFocus = (event: FocusEvent) => {
  isFocused.value = true
  emit('focus', event)
}

const handleBlur = (event: FocusEvent) => {
  isFocused.value = false
  emit('blur', event)
}

const handleInput = (event: Event) => {
  const target = event.target as HTMLInputElement
  emit('update:modelValue', target.value)
  emit('input', event)
}
</script>

<template>
  <div :class="containerClass">
    <!-- Label -->
    <label :for="id" class="ks-text-field__label">
      {{ label }}
      <span v-if="required" class="ks-text-field__required" aria-label="required">*</span>
    </label>

    <!-- Input container with focus ring -->
    <div class="ks-text-field__container">
      <input
        :id="id"
        :type="type"
        :value="modelValue"
        :placeholder="placeholder"
        :disabled="disabled"
        :readonly="readonly"
        :required="required"
        :maxlength="maxLength"
        :minlength="minLength"
        :pattern="pattern"
        :autocomplete="autocomplete"
        class="ks-text-field__input"
        :aria-invalid="!!error"
        :aria-describedby="error ? `${id}-message` : (help ? `${id}-hint` : undefined)"
        @input="handleInput"
        @focus="handleFocus"
        @blur="handleBlur"
      />
    </div>

    <!-- Character count (if maxLength set) -->
    <div v-if="maxLength" class="ks-text-field__count">
      {{ modelValue.length }} / {{ maxLength }}
    </div>

    <!-- Error or help text -->
    <div v-if="error" :id="`${id}-message`" class="ks-text-field__error">
      {{ error }}
    </div>
    <div v-else-if="help" :id="`${id}-hint`" class="ks-text-field__help">
      {{ help }}
    </div>
  </div>
</template>

<style scoped>
.ks-text-field {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-2);
}

.ks-text-field--sm {
  --input-height: 32px;
  --font-size: var(--font-size-sm);
}

.ks-text-field--md {
  --input-height: 36px;
  --font-size: var(--font-size-sm);
}

.ks-text-field--lg {
  --input-height: 44px;
  --font-size: var(--font-size-base);
}

/* Label */
.ks-text-field__label {
  display: flex;
  align-items: center;
  gap: var(--spacing-1);
  font-weight: var(--font-weight-medium);
  color: var(--color-text-primary);
  font-size: var(--font-size-sm);
  cursor: pointer;
}

.ks-text-field__required {
  color: var(--color-danger-500);
}

/* Input container */
.ks-text-field__container {
  position: relative;
  display: flex;
  align-items: center;
  border: 1px solid var(--color-border-primary);
  border-radius: var(--border-radius-sm);
  background-color: var(--color-background-primary);
  transition: all var(--transition-normal);
}

.ks-text-field__input {
  flex: 1;
  height: var(--input-height);
  padding: 0 var(--spacing-3);
  border: none;
  background: transparent;
  font-size: var(--font-size);
  color: var(--color-text-primary);
  outline: none;
  font-family: inherit;
}

.ks-text-field__input::placeholder {
  color: var(--color-text-tertiary);
}

/* Autofill styling handled in JavaScript */

/* Focus state */
.ks-text-field--focused .ks-text-field__container {
  border-color: var(--color-primary-500);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Filled state (label floating style) */
.ks-text-field--filled .ks-text-field__label {
  font-size: 0.75rem;
  color: var(--color-text-secondary);
}

/* Disabled state */
.ks-text-field--disabled .ks-text-field__container {
  background-color: var(--color-background-secondary);
  opacity: 0.6;
  cursor: not-allowed;
}

.ks-text-field--disabled .ks-text-field__input {
  cursor: not-allowed;
}

/* Error state */
.ks-text-field--error .ks-text-field__container {
  border-color: var(--color-danger-500);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

.ks-text-field--error .ks-text-field__label {
  color: var(--color-danger-500);
}

/* Character count */
.ks-text-field__count {
  font-size: var(--font-size-xs);
  color: var(--color-text-tertiary);
  text-align: right;
}

/* Error text */
.ks-text-field__error {
  font-size: var(--font-size-xs);
  color: var(--color-danger-500);
  line-height: var(--line-height-tight);
}

/* Help text */
.ks-text-field__help {
  font-size: var(--font-size-xs);
  color: var(--color-text-secondary);
  line-height: var(--line-height-tight);
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .ks-text-field__container {
    transition: none;
  }
}
</style>
