<script setup lang="ts">
import { computed, onMounted } from 'vue'
import { componentLogger } from '../../utils/componentLogger'

export interface KsButtonProps {
  label?: string
  variant?: 'primary' | 'secondary' | 'danger' | 'ghost' | 'text'
  size?: 'xs' | 'sm' | 'md' | 'lg'
  disabled?: boolean
  loading?: boolean
  type?: 'button' | 'submit' | 'reset'
  fullWidth?: boolean
  icon?: string
  iconPosition?: 'left' | 'right'
  ariaLabel?: string
}

const props = withDefaults(defineProps<KsButtonProps>(), {
  variant: 'primary',
  size: 'md',
  disabled: false,
  loading: false,
  type: 'button',
  fullWidth: false,
  iconPosition: 'left',
})

const emit = defineEmits<{
  click: [event: MouseEvent]
}>()

const buttonClass = computed(() => [
  'ks-button',
  `ks-button--${props.variant}`,
  `ks-button--${props.size}`,
  {
    'ks-button--disabled': props.disabled || props.loading,
    'ks-button--loading': props.loading,
    'ks-button--full-width': props.fullWidth,
  },
])

const handleClick = (event: MouseEvent) => {
  if (!props.disabled && !props.loading) {
    componentLogger.log('KsButton', 'click', {
      label: props.label,
      variant: props.variant,
      size: props.size,
    })
    emit('click', event)
  }
}

onMounted(() => {
  componentLogger.log('KsButton', 'mounted', {
    label: props.label,
    variant: props.variant,
    size: props.size,
    disabled: props.disabled,
  })
})
</script>

<template>
  <button
    :type="type"
    :class="buttonClass"
    :disabled="disabled || loading"
    :aria-label="ariaLabel || label"
    @click="handleClick"
  >
    <!-- Loading spinner -->
    <span v-if="loading" class="ks-button__spinner" aria-hidden="true" />

    <!-- Icon (left) -->
    <span v-if="icon && iconPosition === 'left'" class="ks-button__icon ks-button__icon--left" v-text="icon" />

    <!-- Label -->
    <span v-if="label" class="ks-button__label">{{ label }}</span>
    <slot v-else />

    <!-- Icon (right) -->
    <span v-if="icon && iconPosition === 'right'" class="ks-button__icon ks-button__icon--right" v-text="icon" />
  </button>
</template>

<style scoped>
/* Base button */
.ks-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--spacing-1);
  border: none;
  border-radius: var(--border-radius-sm);
  font-family: inherit;
  font-weight: var(--font-weight-medium);
  cursor: pointer;
  transition: all var(--transition-normal);
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Sizes */
.ks-button--xs {
  height: 28px;
  padding: 0 var(--spacing-2);
  font-size: var(--font-size-xs);
}

.ks-button--sm {
  height: 32px;
  padding: 0 var(--spacing-3);
  font-size: var(--font-size-sm);
}

.ks-button--md {
  height: 36px;
  padding: 0 var(--spacing-4);
  font-size: var(--font-size-sm);
}

.ks-button--lg {
  height: 44px;
  padding: 0 var(--spacing-5);
  font-size: var(--font-size-base);
}

/* Variants - Primary */
.ks-button--primary {
  background-color: var(--color-primary-500);
  color: white;
}

.ks-button--primary:hover:not(:disabled) {
  background-color: var(--color-primary-600);
  box-shadow: 0 2px 8px rgba(59, 130, 246, 0.3);
}

.ks-button--primary:active:not(:disabled) {
  background-color: var(--color-primary-700);
}

.ks-button--primary:focus-visible {
  outline: 3px solid var(--color-primary-500);
  outline-offset: 2px;
}

/* Variants - Secondary */
.ks-button--secondary {
  background-color: var(--color-background-secondary);
  color: var(--color-text-primary);
  border: 1px solid var(--color-border-primary);
}

.ks-button--secondary:hover:not(:disabled) {
  background-color: var(--color-background-hover);
  border-color: var(--color-border-secondary);
}

.ks-button--secondary:active:not(:disabled) {
  background-color: var(--color-background-active);
}

/* Variants - Danger */
.ks-button--danger {
  background-color: var(--color-danger-500);
  color: white;
}

.ks-button--danger:hover:not(:disabled) {
  background-color: var(--color-danger-600);
  box-shadow: 0 2px 8px rgba(239, 68, 68, 0.3);
}

.ks-button--danger:active:not(:disabled) {
  background-color: var(--color-danger-700);
}

/* Variants - Ghost */
.ks-button--ghost {
  background-color: transparent;
  color: var(--color-text-primary);
}

.ks-button--ghost:hover:not(:disabled) {
  background-color: var(--color-background-secondary);
}

.ks-button--ghost:active:not(:disabled) {
  background-color: var(--color-background-active);
}

/* Variants - Text */
.ks-button--text {
  background-color: transparent;
  color: var(--color-primary-500);
  padding: 0 var(--spacing-2);
}

.ks-button--text:hover:not(:disabled) {
  color: var(--color-primary-600);
}

/* State - Disabled */
.ks-button--disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* State - Loading */
.ks-button--loading {
  color: transparent;
}

.ks-button__spinner {
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* Full width */
.ks-button--full-width {
  width: 100%;
}

/* Icon and label */
.ks-button__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2em;
}

.ks-button__label {
  flex: 1;
}
</style>
