// ui.jsx — Shared UI primitives (Button, Input, Textarea, Select, Chip, Card)
// All consume the theme via CSS variables defined at <body> level in index.html.

function Button({ children, variant = 'primary', size = 'md', fullWidth, disabled, onClick, type = 'button', icon, iconAfter, ...rest }) {
  const sizes = {
    sm: { h: 36, px: 14, fs: 13 },
    md: { h: 44, px: 18, fs: 14 },
    lg: { h: 52, px: 22, fs: 15 }
  };
  const s = sizes[size];
  const variants = {
    primary: {
      background: 'var(--accent)',
      color: 'var(--accent-fg)',
      border: '1px solid var(--accent)'
    },
    secondary: {
      background: 'var(--surface)',
      color: 'var(--ink)',
      border: '1px solid var(--border)'
    },
    ghost: {
      background: 'transparent',
      color: 'var(--ink)',
      border: '1px solid transparent'
    },
    outline: {
      background: 'transparent',
      color: 'var(--ink)',
      border: '1.5px solid var(--ink)'
    }
  };
  const v = variants[variant];
  return (
    <button
      type={type}
      onClick={onClick}
      disabled={disabled}
      style={{
        height: s.h,
        padding: `0 ${s.px}px`,
        width: fullWidth ? '100%' : 'auto',
        fontSize: s.fs,
        fontWeight: 600,
        letterSpacing: '-0.005em',
        borderRadius: 999,
        cursor: disabled ? 'not-allowed' : 'pointer',
        opacity: disabled ? 0.5 : 1,
        display: 'inline-flex',
        alignItems: 'center',
        justifyContent: 'center',
        gap: 8,
        fontFamily: 'inherit',
        transition: 'transform .15s ease, background .15s ease, box-shadow .15s ease',
        ...v
      }}
      onMouseDown={(e) => !disabled && (e.currentTarget.style.transform = 'scale(0.98)')}
      onMouseUp={(e) => e.currentTarget.style.transform = 'scale(1)'}
      onMouseLeave={(e) => e.currentTarget.style.transform = 'scale(1)'}
      {...rest}>
      
      {icon}
      {children}
      {iconAfter}
    </button>);

}

function Field({ label, hint, error, required, children, ...rest }) {
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }} {...rest}>
      {label &&
      <label style={{
        fontSize: 13, fontWeight: 500,
        color: 'var(--ink)',
        letterSpacing: '-0.005em'
      }}>
          {label} {required && <span style={{ color: 'var(--accent-strong)' }}>*</span>}
        </label>
      }
      {children}
      {hint && !error &&
      <div style={{ fontSize: 12, color: 'var(--muted)' }}>{hint}</div>
      }
      {error &&
      <div style={{ fontSize: 12, color: '#c0392b' }}>{error}</div>
      }
    </div>);

}

function Input({ icon, suffix, ...props }) {
  return (
    <div style={{
      position: 'relative',
      display: 'flex',
      alignItems: 'center',
      background: 'var(--surface)',
      border: '1px solid var(--border)',
      borderRadius: 12,
      transition: 'border-color .15s ease, box-shadow .15s ease'
    }}
    onFocus={(e) => {
      e.currentTarget.style.borderColor = 'var(--accent)';
      e.currentTarget.style.boxShadow = '0 0 0 3px var(--accent-soft)';
    }}
    onBlur={(e) => {
      e.currentTarget.style.borderColor = 'var(--border)';
      e.currentTarget.style.boxShadow = 'none';
    }}>
      
      {icon &&
      <div style={{ paddingLeft: 14, color: 'var(--muted)', display: 'flex' }}>{icon}</div>
      }
      <input
        {...props}
        style={{
          flex: 1,
          height: 48,
          padding: icon ? '0 14px 0 10px' : '0 14px',
          border: 'none',
          background: 'transparent',
          outline: 'none',
          fontSize: 15,
          color: 'var(--ink)',
          fontFamily: 'inherit',
          letterSpacing: '-0.005em',
          width: '100%',
          ...props.style
        }} />
      
      {suffix &&
      <div style={{ paddingRight: 14, color: 'var(--muted)', fontSize: 13 }}>{suffix}</div>
      }
    </div>);

}

function Textarea(props) {
  return (
    <textarea
      {...props}
      style={{
        minHeight: 120,
        padding: '14px',
        border: '1px solid var(--border)',
        background: 'var(--surface)',
        borderRadius: 12,
        outline: 'none',
        fontSize: 15,
        color: 'var(--ink)',
        fontFamily: 'inherit',
        letterSpacing: '-0.005em',
        lineHeight: 1.5,
        resize: 'vertical',
        ...props.style
      }}
      onFocus={(e) => {
        e.currentTarget.style.borderColor = 'var(--accent)';
        e.currentTarget.style.boxShadow = '0 0 0 3px var(--accent-soft)';
        props.onFocus && props.onFocus(e);
      }}
      onBlur={(e) => {
        e.currentTarget.style.borderColor = 'var(--border)';
        e.currentTarget.style.boxShadow = 'none';
        props.onBlur && props.onBlur(e);
      }} />);


}

function Segmented({ options, value, onChange, fullWidth }) {
  return (
    <div style={{
      display: 'inline-flex',
      width: fullWidth ? '100%' : 'auto',
      background: 'var(--surface)',
      border: '1px solid var(--border)',
      borderRadius: 12,
      padding: 4
    }}>
      {options.map((opt) => {
        const v = typeof opt === 'object' ? opt.value : opt;
        const l = typeof opt === 'object' ? opt.label : opt;
        const active = v === value;
        return (
          <button
            key={v}
            type="button"
            onClick={() => onChange(v)}
            style={{
              flex: 1,
              height: 40,
              padding: '0 16px',
              border: 'none',
              background: active ? 'var(--accent)' : 'transparent',
              color: active ? 'var(--accent-fg)' : 'var(--ink)',
              borderRadius: 8,
              fontSize: 14,
              fontWeight: active ? 600 : 500,
              cursor: 'pointer',
              fontFamily: 'inherit',
              transition: 'background .15s ease, color .15s ease',
              letterSpacing: '-0.005em'
            }}>
            {l}</button>);

      })}
    </div>);

}

function Chip({ active, children, onClick, removable, onRemove }) {
  return (
    <button
      type="button"
      onClick={onClick}
      style={{
        height: 36,
        padding: '0 14px',
        borderRadius: 999,
        border: `1px solid ${active ? 'var(--accent)' : 'var(--border)'}`,
        background: active ? 'var(--accent-soft)' : 'var(--surface)',
        color: active ? 'var(--accent-strong)' : 'var(--ink)',
        fontSize: 13,
        fontWeight: 500,
        cursor: 'pointer',
        fontFamily: 'inherit',
        display: 'inline-flex',
        alignItems: 'center',
        gap: 6,
        letterSpacing: '-0.005em',
        transition: 'all .15s ease'
      }}>
      
      {children}
      {removable &&
      <span
        onClick={(e) => {e.stopPropagation();onRemove && onRemove();}}
        style={{
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
          width: 16, height: 16, borderRadius: '50%',
          background: active ? 'var(--accent)' : 'transparent',
          color: active ? 'var(--accent-fg)' : 'var(--muted)',
          fontSize: 10,
          marginLeft: 2,
          marginRight: -6
        }}>
        ✕</span>
      }
    </button>);

}

function Card({ children, padding = 20, hover, onClick, selected, style }) {
  return (
    <div
      onClick={onClick}
      style={{
        background: 'var(--surface)',
        border: selected ? '2px solid var(--accent)' : '1px solid var(--border)',
        borderRadius: 16,
        padding: selected ? padding - 1 : padding,
        cursor: onClick ? 'pointer' : 'default',
        transition: 'border-color .15s ease, transform .15s ease, box-shadow .15s ease',
        ...(hover ? {
          ':hover': { borderColor: 'var(--accent)' }
        } : {}),
        ...style, width: "100%"
      }}
      onMouseEnter={(e) => {
        if (onClick) {
          e.currentTarget.style.boxShadow = '0 4px 16px rgba(0,0,0,0.06)';
        }
      }}
      onMouseLeave={(e) => {
        e.currentTarget.style.boxShadow = 'none';
      }}>
      
      {children}
    </div>);

}

// Tennis ball logo mark
function VoleeMark({ size = 28, color }) {
  const c = color || 'var(--accent)';
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" fill="none">
      <circle cx="16" cy="16" r="14" fill={c} />
      <path d="M3 13c5 0 9 1 13 3 4 2 8 3 13 3"
      stroke="var(--accent-fg)" strokeWidth="1.5" strokeLinecap="round" fill="none" opacity="0.7" />
      <path d="M3 19c5 0 9-1 13-3 4-2 8-3 13-3"
      stroke="var(--accent-fg)" strokeWidth="1.5" strokeLinecap="round" fill="none" opacity="0.7" />
    </svg>);

}

function Logo({ size = 22 }) {
  return (
    <div style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
      <VoleeMark size={size} />
      <span style={{
        fontFamily: '"Instrument Serif", Georgia, serif',
        fontSize: size + 4,
        letterSpacing: '-0.01em',
        color: 'var(--ink)',
        fontStyle: 'italic',
        fontWeight: 400,
        lineHeight: 1
      }}>Trygo</span>
    </div>);

}

// Generic image placeholder used for coach avatars, racket images, etc.
function ImagePlaceholder({ width = '100%', height = 80, label, rounded = 12 }) {
  return (
    <div style={{
      width, height,
      borderRadius: rounded,
      background: 'repeating-linear-gradient(45deg, #ece9e0, #ece9e0 6px, #f3f1ea 6px, #f3f1ea 12px)',
      border: '1px solid var(--border)',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      color: '#9b988f',
      fontSize: 11,
      fontFamily: 'ui-monospace, "SF Mono", Menlo, monospace',
      textAlign: 'center',
      padding: 8,
      flexShrink: 0
    }}>{label}</div>);

}

// Letter avatar (used for coach cards as a stable visual without real photos)
function LetterAvatar({ name, size = 56, hue = 80, photoUrl }) {
  if (photoUrl) {
    return (
      <div style={{
        width: size, height: size,
        borderRadius: '50%',
        overflow: 'hidden',
        flexShrink: 0,
        border: '2px solid var(--border)',
      }}>
        <img
          src={photoUrl}
          alt={name}
          style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block' }}
        />
      </div>
    );
  }

  const initials = name.split(' ').map((n) => n[0]).slice(0, 2).join('');
  return (
    <div style={{
      width: size, height: size,
      borderRadius: '50%',
      background: `oklch(0.78 0.08 ${hue})`,
      color: '#fff',
      display: 'flex', alignItems: 'center', justifyContent: 'center',
      fontFamily: '"Instrument Serif", Georgia, serif',
      fontStyle: 'italic',
      fontSize: size * 0.42,
      fontWeight: 400,
      flexShrink: 0,
    }}>{initials}</div>
  );
}

Object.assign(window, {
  Button, Field, Input, Textarea, Segmented, Chip, Card,
  VoleeMark, Logo, ImagePlaceholder, LetterAvatar
});