<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
  severity?: 'primary' | 'secondary' | 'danger' | 'ghost' | 'text' | 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">…</span>

    <!-- 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 v-if="loading" class="ks-sr-only">…</span></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(--ks-space-2, 8px);
  border: none;
  border-radius: var(--ks-radius-md, 4px);
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: var(--ks-transition-fast, all 0.15s ease);
  user-select: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ks-button:hover:not(:disabled) {
  transform: translateY(-1px);
}

.ks-button:active:not(:disabled) {
  transform: translateY(1px);
}

/* Sizes */
.ks-button--xs {
  height: 28px;
  padding: 0 var(--ks-space-2, 8px);
  font-size: var(--ks-font-caption, 12px);
}

.ks-button--sm {
  height: 32px;
  padding: 0 var(--ks-space-3, 12px);
  font-size: var(--ks-font-grid, 13px);
}

.ks-button--md {
  height: var(--ks-control-height, 34px);
  padding: 0 var(--ks-space-4, 16px);
  font-size: var(--ks-font-body, 14px);
}

.ks-button--lg {
  height: 44px;
  padding: 0 var(--ks-space-6, 24px);
  font-size: var(--ks-font-section, 16px);
}

/* Variants - Primary */
.ks-button--primary {
  background-color: var(--ks-color-action, #2563eb);
  color: white;
  box-shadow: var(--ks-shadow-sm, 0 1px 2px rgba(0, 0, 0, 0.05));
}

.ks-button--primary:hover:not(:disabled) {
  background-color: var(--ks-color-action-hover, #1d4ed8);
  box-shadow: var(--ks-shadow-glow, 0 0 16px rgba(37, 99, 235, 0.25));
}

.ks-button--primary:active:not(:disabled) {
  background-color: #1e40af;
  box-shadow: none;
}

.ks-button--primary:focus-visible {
  outline: var(--ks-focus-ring-width, 2px) solid var(--ks-color-focus, #2563eb);
  outline-offset: var(--ks-focus-ring-offset, 1px);
}

/* Variants - Secondary */
.ks-button--secondary {
  background-color: var(--ks-color-surface, #ffffff);
  color: var(--ks-color-text, #0f172a);
  border: 1px solid var(--ks-color-border, #e2e8f0);
  box-shadow: var(--ks-shadow-sm, 0 1px 2px rgba(0, 0, 0, 0.05));
}

.ks-button--secondary:hover:not(:disabled) {
  background-color: var(--ks-color-canvas, #f8fafc);
  border-color: var(--ks-color-border-strong, #cbd5e1);
  box-shadow: var(--ks-shadow-md, 0 4px 6px rgba(0, 0, 0, 0.05));
}

.ks-button--secondary:active:not(:disabled) {
  background-color: var(--ks-color-neutral-100, #f1f5f9);
}

/* Variants - Danger */
.ks-button--danger {
  background-color: var(--ks-color-danger, #dc2626);
  color: white;
  box-shadow: var(--ks-shadow-sm, 0 1px 2px rgba(0, 0, 0, 0.05));
}

.ks-button--danger:hover:not(:disabled) {
  background-color: #b91c1c;
  box-shadow: var(--ks-shadow-danger-glow, 0 0 16px rgba(220, 38, 38, 0.25));
}

.ks-button--danger:active:not(:disabled) {
  background-color: #991b1b;
}

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

.ks-button--ghost:hover:not(:disabled) {
  background-color: var(--ks-color-neutral-100, #f1f5f9);
}

.ks-button--ghost:active:not(:disabled) {
  background-color: var(--ks-color-neutral-200, #e2e8f0);
}

/* Variants - Text */
.ks-button--text {
  background-color: transparent;
  color: var(--ks-color-action, #2563eb);
  padding: 0 var(--ks-space-2, 8px);
}

.ks-button--text:hover:not(:disabled) {
  color: var(--ks-color-action-hover, #1d4ed8);
  background-color: rgba(37, 99, 235, 0.08);
}

/* State - Disabled */
.ks-button--disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

/* 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>
