// components.jsx - Shared UI primitives for Giraudo quoting app
// Industrial aesthetic: bold type, high contrast, orange + metallic gray

const Icon = {
  Eye: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
      <circle cx="12" cy="12" r="3" stroke={color} strokeWidth="2"/>
    </svg>
  ),
  ArrowRight: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M5 12h14M13 5l7 7-7 7" stroke={color} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  ArrowLeft: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M19 12H5M11 19l-7-7 7-7" stroke={color} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  X: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M18 6L6 18M6 6l12 12" stroke={color} strokeWidth="2.5" strokeLinecap="round"/>
    </svg>
  ),
  Share: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M4 12v8a2 2 0 002 2h12a2 2 0 002-2v-8M16 6l-4-4-4 4M12 2v13" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  WhatsApp: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill={color}>
      <path d="M17.5 14.4c-.3-.1-1.7-.8-2-.9-.3-.1-.5-.1-.7.1-.2.3-.8.9-1 1.1-.2.2-.4.2-.6.1-1.2-.6-2.1-1.1-2.9-2.3-.2-.4 0-.4.3-.7.1-.1.3-.3.4-.5.1-.2.2-.3.3-.5 0-.2 0-.4 0-.5-.1-.1-.7-1.6-.9-2.2-.2-.6-.5-.5-.7-.5h-.6c-.2 0-.5.1-.7.3-.3.3-1 1-1 2.4s1 2.8 1.2 3c.1.2 2 3.1 4.9 4.3.7.3 1.2.5 1.6.6.7.2 1.3.2 1.8.1.6-.1 1.7-.7 2-1.4.2-.7.2-1.2.2-1.4-.1-.1-.3-.2-.6-.3zM12 2C6.5 2 2 6.5 2 12c0 1.8.5 3.5 1.3 4.9L2 22l5.3-1.3c1.4.7 2.9 1.2 4.7 1.2 5.5 0 10-4.5 10-10S17.5 2 12 2z"/>
    </svg>
  ),
  Info: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="10" stroke={color} strokeWidth="2"/>
      <path d="M12 16v-4M12 8h0" stroke={color} strokeWidth="2.5" strokeLinecap="round"/>
    </svg>
  ),
  Warning: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M10.3 3.1L1.8 17a2 2 0 001.7 3h17a2 2 0 001.7-3L13.7 3.1a2 2 0 00-3.4 0zM12 9v4M12 17h0" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Check: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M20 6L9 17l-5-5" stroke={color} strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Cog: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M3 6h18M8 6V4a1 1 0 011-1h6a1 1 0 011 1v2M19 6l-1 14a2 2 0 01-2 2H8a2 2 0 01-2-2L5 6" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Save: ({ size = 16, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M19 21H5a2 2 0 01-2-2V5a2 2 0 012-2h11l5 5v11a2 2 0 01-2 2z" stroke={color} strokeWidth="2" strokeLinejoin="round"/>
      <path d="M17 21v-8H7v8M7 3v5h8" stroke={color} strokeWidth="2" strokeLinejoin="round"/>
    </svg>
  ),
  History: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M3 12a9 9 0 109-9 9 9 0 00-6.4 2.6L3 8M3 3v5h5" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M12 7v5l3 2" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Theme: ({ size = 18, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <circle cx="12" cy="12" r="9" stroke={color} strokeWidth="2"/>
      <path d="M12 3v18" stroke={color} strokeWidth="2"/>
      <path d="M12 3a9 9 0 000 18z" fill={color}/>
    </svg>
  ),
  Material: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="3" y="7" width="18" height="10" stroke={color} strokeWidth="2"/>
      <path d="M3 11h18M3 13h18" stroke={color} strokeWidth="1.2"/>
    </svg>
  ),
  Calibre: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M4 7h16M4 12h16M4 17h16" stroke={color} strokeWidth="2" strokeLinecap="round"/>
      <path d="M8 5v4M16 10v4M12 15v4" stroke={color} strokeWidth="2" strokeLinecap="round"/>
    </svg>
  ),
  Ruler: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="2" y="9" width="20" height="6" stroke={color} strokeWidth="2"/>
      <path d="M6 9v3M10 9v3M14 9v3M18 9v3" stroke={color} strokeWidth="1.5"/>
    </svg>
  ),
  Stack: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <rect x="4" y="4" width="16" height="3" stroke={color} strokeWidth="1.8"/>
      <rect x="4" y="10.5" width="16" height="3" stroke={color} strokeWidth="1.8"/>
      <rect x="4" y="17" width="16" height="3" stroke={color} strokeWidth="1.8"/>
    </svg>
  ),
  Truck: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M1 6h12v10H1zM13 10h5l3 3v3h-8z" stroke={color} strokeWidth="2" strokeLinejoin="round"/>
      <circle cx="6" cy="18" r="1.8" stroke={color} strokeWidth="1.8"/>
      <circle cx="17" cy="18" r="1.8" stroke={color} strokeWidth="1.8"/>
    </svg>
  ),
  Scissors: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <circle cx="6" cy="6" r="3" stroke={color} strokeWidth="1.8"/>
      <circle cx="6" cy="18" r="3" stroke={color} strokeWidth="1.8"/>
      <path d="M8.5 7.5L21 19M8.5 16.5L21 5" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
  Wrench: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M15 3a5 5 0 016 6l-3-1-2 2 1 3a5 5 0 01-6-6l3 1 2-2z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
      <path d="M14 10L4 20l-1-1L13 9" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
  Palette: ({ size = 14, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 3a9 9 0 100 18 2 2 0 002-2v-1a2 2 0 012-2h2a3 3 0 003-3 9 9 0 00-9-9z" stroke={color} strokeWidth="1.8"/>
      <circle cx="8" cy="10" r="1.2" fill={color}/>
      <circle cx="12" cy="7" r="1.2" fill={color}/>
      <circle cx="16" cy="10" r="1.2" fill={color}/>
    </svg>
  ),
  Snowflake: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 2v20M2 12h20M4.5 4.5l15 15M4.5 19.5l15-15" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
      <path d="M9 4l3 2 3-2M9 20l3-2 3 2M4 9l2 3-2 3M20 9l-2 3 2 3" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" fill="none"/>
    </svg>
  ),
  Flame: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 2s-1 3-3 5-4 4-4 8a7 7 0 0014 0c0-3-2-5-3-7-1.5-3-4-6-4-6z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
      <path d="M12 13s-1 1-2 2.5A3 3 0 0015 17c0-1.5-1-2.5-1.5-3-.5-.5-1.5-1-1.5-1z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
    </svg>
  ),
  Shield: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 2L4 5v7c0 5 3.5 8.5 8 10 4.5-1.5 8-5 8-10V5l-8-3z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
      <path d="M9 12l2 2 4-4" stroke={color} strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  Droplet: ({ size = 20, color = 'currentColor' }) => (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <path d="M12 2s-7 8-7 13a7 7 0 0014 0c0-5-7-13-7-13z" stroke={color} strokeWidth="1.8" strokeLinejoin="round"/>
      <path d="M9 14a3 3 0 003 3" stroke={color} strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  ),
};

const MATERIAL_ICONS = {
  'FRIO': Icon.Snowflake,
  'CALIENTE': Icon.Flame,
  'GALVANIZADA': Icon.Shield,
  'PREPINTADA': Icon.Droplet,
};

function FieldLabel({ children, detail, mono = false, icon }) {
  return (
    <div style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'center',
      marginBottom: 8,
    }}>
      <div style={{
        fontFamily: mono ? 'var(--mono)' : 'var(--sans)',
        fontSize: 11, fontWeight: 700, letterSpacing: '0.12em',
        textTransform: 'uppercase', color: 'var(--muted)',
        display: 'flex', alignItems: 'center', gap: 6,
      }}>{icon && <span style={{display:'inline-flex', color: 'var(--muted)'}}>{icon}</span>}{children}</div>
      {detail && (
        <div style={{
          fontFamily: 'var(--mono)', fontSize: 11, fontWeight: 600,
          color: 'var(--muted)', letterSpacing: '0.04em',
        }}>{detail}</div>
      )}
    </div>
  );
}

function NumInput({ value, onChange, suffix, placeholder }) {
  return (
    <div style={{
      background: 'var(--surface)',
      border: '1.5px solid var(--border)',
      borderRadius: 4, height: 52,
      display: 'flex', alignItems: 'center',
      padding: '0 16px', position: 'relative',
    }}>
      <input
        type="text" inputMode="numeric"
        value={value}
        onChange={e => onChange(e.target.value.replace(/[^0-9.]/g, ''))}
        placeholder={placeholder}
        style={{
          flex: 1, border: 'none', outline: 'none', minWidth: 0,
          background: 'transparent',
          fontFamily: 'var(--mono)', fontSize: 22, fontWeight: 600,
          color: 'var(--fg)',
          letterSpacing: '-0.01em',
        }}
      />
      {suffix && (
        <span style={{
          fontFamily: 'var(--mono)', fontSize: 13, fontWeight: 600,
          color: 'var(--muted)', letterSpacing: '0.08em',
          textTransform: 'uppercase',
        }}>{suffix}</span>
      )}
    </div>
  );
}

function Slider({ value, onChange, min = 0, max = 10, step = 0.5, suffix = '%', style = 'bar' }) {
  const pct = ((value - min) / (max - min)) * 100;

  if (style === 'numeric') {
    return (
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10, height: 52,
      }}>
        <button onClick={() => onChange(Math.max(min, value - step))} style={smallBtn}>−</button>
        <div style={{
          flex: 1, height: 52, border: '1.5px solid var(--border)',
          background: 'var(--surface)', borderRadius: 4,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          fontFamily: 'var(--mono)', fontSize: 22, fontWeight: 700,
          color: 'var(--fg)',
        }}>
          {value}{suffix}
        </div>
        <button onClick={() => onChange(Math.min(max, value + step))} style={smallBtn}>+</button>
      </div>
    );
  }

  if (style === 'stepper') {
    const steps = Math.round((max - min) / step);
    return (
      <div>
        <div style={{
          display: 'flex', gap: 2,
          height: 40, alignItems: 'stretch',
        }}>
          {Array.from({ length: steps + 1 }).map((_, i) => {
            const v = min + i * step;
            const active = v <= value;
            return (
              <div key={i}
                onClick={() => onChange(v)}
                style={{
                  flex: 1, cursor: 'pointer',
                  background: active ? 'var(--accent)' : 'var(--surface-2)',
                  border: '1px solid var(--border)',
                }}
              />
            );
          })}
        </div>
        <div style={{
          display: 'flex', justifyContent: 'space-between', marginTop: 6,
          fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--muted)',
          fontWeight: 600,
        }}>
          <span>{min}{suffix}</span>
          <span style={{ color: 'var(--fg)', fontWeight: 700 }}>{value}{suffix}</span>
          <span>{max}{suffix}</span>
        </div>
      </div>
    );
  }

  return (
    <div>
      <div style={{ position: 'relative', height: 40, display: 'flex', alignItems: 'center' }}>
        <div style={{
          height: 6, background: 'var(--surface-2)', borderRadius: 0,
          width: '100%', position: 'relative',
          border: '1px solid var(--border)',
        }}>
          <div style={{
            position: 'absolute', left: 0, top: -1, bottom: -1,
            width: `${pct}%`, background: 'var(--accent)',
          }} />
        </div>
        <input
          type="range" min={min} max={max} step={step}
          value={value}
          onChange={e => onChange(parseFloat(e.target.value))}
          style={{
            position: 'absolute', left: 0, right: 0, top: 0, bottom: 0,
            width: '100%', opacity: 0, cursor: 'pointer', margin: 0,
          }}
        />
        <div style={{
          position: 'absolute', left: `calc(${pct}% - 14px)`, top: 6,
          width: 28, height: 28, background: 'var(--fg)',
          border: '2px solid var(--accent)', pointerEvents: 'none',
        }} />
      </div>
      <div style={{
        display: 'flex', justifyContent: 'space-between', marginTop: 6,
        fontFamily: 'var(--mono)', fontSize: 11, color: 'var(--muted)',
        fontWeight: 600,
      }}>
        <span>{min}{suffix}</span>
        <span style={{ color: 'var(--accent)', fontWeight: 700, fontSize: 13 }}>{value}{suffix}</span>
        <span>{max}{suffix}</span>
      </div>
    </div>
  );
}

const smallBtn = {
  width: 52, height: 52,
  background: 'var(--surface)', border: '1.5px solid var(--border)',
  fontFamily: 'var(--mono)', fontSize: 24, fontWeight: 700,
  color: 'var(--fg)', cursor: 'pointer', borderRadius: 4,
};

function MaterialTabs({ value, onChange, options }) {
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: `repeat(${options.length}, 1fr)`,
      gap: 0, border: '1.5px solid var(--border)', borderRadius: 4,
      overflow: 'hidden', background: 'var(--surface)',
    }}>
      {options.map((opt, i) => {
        const active = opt === value;
        const IconComp = MATERIAL_ICONS[opt];
        return (
          <button key={opt}
            onClick={() => onChange(opt)}
            style={{
              display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center',
              gap: 6, padding: '14px 4px', minHeight: 76,
              fontFamily: 'var(--sans)', fontSize: 10.5, fontWeight: 800,
              letterSpacing: '0.06em', textTransform: 'uppercase',
              background: active ? 'var(--fg)' : 'transparent',
              color: active ? 'var(--bg)' : 'var(--fg)',
              border: 'none',
              borderLeft: i > 0 ? '1.5px solid var(--border)' : 'none',
              cursor: 'pointer',
            }}
          >
            {IconComp && <IconComp size={22} color={active ? 'var(--bg)' : 'var(--fg)'}/>}
            <span>{opt}</span>
          </button>
        );
      })}
    </div>
  );
}

function CalibreSelector({ value, onChange, options, style = 'cards' }) {
  if (style === 'dropdown') {
    return (
      <select value={value} onChange={e => onChange(e.target.value)}
        style={{
          width: '100%', height: 52, padding: '0 16px',
          border: '1.5px solid var(--border)', background: 'var(--surface)',
          fontFamily: 'var(--mono)', fontSize: 22, fontWeight: 700,
          color: 'var(--fg)', borderRadius: 4, appearance: 'none',
          backgroundImage: `url("data:image/svg+xml,%3Csvg width='12' height='8' viewBox='0 0 12 8' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23666' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E")`,
          backgroundRepeat: 'no-repeat', backgroundPosition: 'right 16px center',
        }}
      >
        {options.map(o => <option key={o} value={o}>Calibre {o}</option>)}
      </select>
    );
  }
  if (style === 'pills') {
    return (
      <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
        {options.map(o => {
          const active = o === value;
          return (
            <button key={o}
              onClick={() => onChange(o)}
              style={{
                flex: '1 1 60px', height: 44, minWidth: 60,
                borderRadius: 22, border: active ? 'none' : '1.5px solid var(--border)',
                background: active ? 'var(--accent)' : 'var(--surface)',
                color: active ? '#fff' : 'var(--fg)',
                fontFamily: 'var(--mono)', fontSize: 16, fontWeight: 700,
                cursor: 'pointer',
              }}
            >{o}</button>
          );
        })}
      </div>
    );
  }
  // Grid por cantidad: ≤5 todo en fila | 6 → 3×2 | 7–8 → 4 cols | 9 → 3×3 | >9 → 5 cols
  const n = options.length;
  const cols = n <= 5 ? n : n === 6 ? 3 : n <= 8 ? 4 : n === 9 ? 3 : 5;
  return (
    <div style={{
      display: 'grid',
      gridTemplateColumns: `repeat(${cols}, 1fr)`,
      gap: 8,
    }}>
      {options.map(o => {
        const active = o === value;
        return (
          <button key={o}
            onClick={() => onChange(o)}
            style={{
              height: 64, borderRadius: 4,
              border: active ? '2px solid var(--accent)' : '1.5px solid var(--border)',
              background: active ? 'var(--accent-soft)' : 'var(--surface)',
              color: active ? 'var(--accent)' : 'var(--fg)',
              fontFamily: 'var(--mono)', fontSize: 24, fontWeight: 700,
              cursor: 'pointer', position: 'relative',
            }}
          >
            {o}
            {active && (
              <div style={{
                position: 'absolute', top: 4, right: 6,
                fontSize: 8, fontWeight: 800, letterSpacing: '0.1em',
                color: 'var(--accent)',
              }}>●</div>
            )}
          </button>
        );
      })}
    </div>
  );
}

function Toggle({ value, onChange }) {
  return (
    <button onClick={() => onChange(!value)} style={{
      width: 52, height: 30, borderRadius: 15,
      background: value ? 'var(--accent)' : 'var(--surface-2)',
      border: '1.5px solid var(--border)',
      position: 'relative', cursor: 'pointer', padding: 0,
      transition: 'background 0.15s',
    }}>
      <div style={{
        position: 'absolute', top: 2, left: value ? 24 : 2,
        width: 22, height: 22, background: '#fff', borderRadius: '50%',
        transition: 'left 0.15s',
        boxShadow: '0 1px 3px rgba(0,0,0,0.25)',
      }} />
    </button>
  );
}

function CTAButton({ children, onClick, variant = 'primary', disabled = false }) {
  const styles = {
    primary: {
      background: 'var(--accent)', color: '#fff',
      border: 'none',
    },
    dark: {
      background: 'var(--fg)', color: 'var(--bg)',
      border: 'none',
    },
    ghost: {
      background: 'transparent', color: 'var(--fg)',
      border: '1.5px solid var(--border)',
    },
  }[variant];
  return (
    <button onClick={onClick} disabled={disabled}
      style={{
        width: '100%', height: 56, borderRadius: 4,
        fontFamily: 'var(--sans)', fontSize: 14, fontWeight: 800,
        letterSpacing: '0.1em', textTransform: 'uppercase',
        cursor: disabled ? 'not-allowed' : 'pointer',
        opacity: disabled ? 0.5 : 1,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        gap: 10,
        ...styles,
      }}
    >{children}</button>
  );
}

function TextInput({ value, onChange, placeholder, multiline = false }) {
  const Tag = multiline ? 'textarea' : 'input';
  return (
    <Tag
      value={value}
      onChange={e => onChange(e.target.value)}
      placeholder={placeholder}
      style={{
        width: '100%', boxSizing: 'border-box',
        minHeight: multiline ? 88 : 52,
        padding: multiline ? '14px 16px' : '0 16px',
        background: 'var(--surface)',
        border: '1.5px solid var(--border)', borderRadius: 4,
        fontFamily: 'var(--sans)', fontSize: 16, fontWeight: 500,
        color: 'var(--fg)', outline: 'none',
        resize: multiline ? 'vertical' : 'none',
        lineHeight: multiline ? 1.4 : undefined,
      }}
    />
  );
}

Object.assign(window, {
  Icon, FieldLabel, NumInput, Slider, MaterialTabs,
  CalibreSelector, Toggle, CTAButton, TextInput,
});
