'use client';

import React from 'react';

type Cn<T extends keyof JSX.IntrinsicElements> = React.ComponentPropsWithoutRef<T>;

export function mdComponents(isMobile: boolean): Record<string, React.ComponentType<never>> {
  return {
    h1: ({ children, id }: Cn<'h1'>) => (
      <h1 id={id} style={{ fontSize: isMobile ? '22px' : '28px', fontWeight: 800, color: '#1a1a2e', margin: '28px 0 10px', letterSpacing: '-0.5px', scrollMarginTop: '72px' }}>{children}</h1>
    ),
    h2: ({ children, id }: Cn<'h2'>) => (
      <h2 id={id} style={{ fontSize: isMobile ? '18px' : '20px', fontWeight: 800, color: '#1a1a2e', margin: '32px 0 10px', letterSpacing: '-0.3px', scrollMarginTop: '72px' }}>
        {children}
      </h2>
    ),
    h3: ({ children, id }: Cn<'h3'>) => (
      <h3 id={id} style={{ fontSize: '16px', fontWeight: 700, color: '#1a1a2e', margin: '20px 0 8px', scrollMarginTop: '72px' }}>{children}</h3>
    ),
    p: ({ children }: Cn<'p'>) => (
      <p style={{ fontSize: '14px', color: '#444', lineHeight: 1.75, margin: '10px 0' }}>{children}</p>
    ),
    blockquote: ({ children }: Cn<'blockquote'>) => (
      <blockquote style={{ fontSize: '14px', color: '#555', lineHeight: 1.7, margin: '14px 0', padding: '12px 16px', background: '#f9f9fd', borderRadius: '8px', borderLeft: '3px solid #5c5fbc' }}>
        {children}
      </blockquote>
    ),
    ul: ({ children }: Cn<'ul'>) => (
      <ul style={{ margin: '10px 0', paddingLeft: 0, listStyle: 'none' }}>{children}</ul>
    ),
    ol: ({ children }: Cn<'ol'>) => (
      <ol style={{ margin: '10px 0', paddingLeft: '20px' }}>{children}</ol>
    ),
    li: ({ children }: Cn<'li'>) => (
      <li style={{ display: 'flex', alignItems: 'flex-start', gap: '10px', margin: '6px 0', fontSize: '14px', color: '#444', lineHeight: 1.7 }}>
        <span style={{ flexShrink: 0, marginTop: '6px', width: '16px', height: '16px', borderRadius: '50%', background: '#ededf7', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#5c5fbc', fontSize: '10px', fontWeight: 700 }}>✓</span>
        <span>{children}</span>
      </li>
    ),
    table: ({ children }: Cn<'table'>) => (
      <div style={{ overflowX: 'auto', margin: '14px 0' }}>
        <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: '13px' }}>{children}</table>
      </div>
    ),
    thead: ({ children }: Cn<'thead'>) => <thead style={{ background: '#f8f8fb' }}>{children}</thead>,
    th: ({ children }: Cn<'th'>) => (
      <th style={{ padding: '9px 12px', textAlign: 'left', color: '#888', fontWeight: 600, borderBottom: '1px solid #eee', whiteSpace: 'nowrap' as const }}>{children}</th>
    ),
    td: ({ children }: Cn<'td'>) => (
      <td style={{ padding: '8px 12px', color: '#555', borderBottom: '1px solid #f5f5f5', verticalAlign: 'top' }}>{children}</td>
    ),
    code: ({ children, className }: Cn<'code'>) => {
      if (className?.startsWith('language-')) {
        return (
          <pre style={{ background: '#f5f5fb', borderRadius: '8px', padding: '14px 16px', overflowX: 'auto', margin: '14px 0' }}>
            <code style={{ fontSize: '12.5px', color: '#333', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace', lineHeight: 1.65 }}>{children}</code>
          </pre>
        );
      }
      return <code style={{ fontSize: '12.5px', color: '#5c5fbc', background: '#ededf7', padding: '2px 6px', borderRadius: '4px', fontFamily: 'ui-monospace, SFMono-Regular, Menlo, monospace' }}>{children}</code>;
    },
    pre: ({ children }: Cn<'pre'>) => <>{children}</>,
    a: ({ children, href }: Cn<'a'>) => {
      const isExternal = href?.startsWith('http');
      return (
        <a
          href={href}
          target={isExternal ? '_blank' : undefined}
          rel={isExternal ? 'noopener noreferrer' : undefined}
          style={{ color: '#5c5fbc', textDecoration: 'none', fontWeight: 500 }}
          onMouseEnter={e => (e.currentTarget.style.textDecoration = 'underline')}
          onMouseLeave={e => (e.currentTarget.style.textDecoration = 'none')}
        >
          {children}
        </a>
      );
    },
    hr: () => <hr style={{ border: 'none', borderTop: '1px solid #f0f0f0', margin: '24px 0' }} />,
    strong: ({ children }: Cn<'strong'>) => <strong style={{ fontWeight: 700, color: '#1a1a2e' }}>{children}</strong>,
    img: ({ src, alt }: Cn<'img'>) => (
      // MDX images come from arbitrary paths in user-authored content; next/image
      // would force us to enumerate every host/path. <img> is the right tradeoff
      // for a docs site whose assets are all in public/.
      // eslint-disable-next-line @next/next/no-img-element
      <img src={src} alt={alt} style={{ maxWidth: '100%', borderRadius: '8px', margin: '14px 0', border: '1px solid #eee' }} />
    ),
  } as unknown as Record<string, React.ComponentType<never>>;
}

/**
 * Release-notes flavor: tighter spacing and smaller body text for changelog-style cards.
 */
export function mdComponentsCompact(isMobile: boolean): Record<string, React.ComponentType<never>> {
  return {
    h1: ({ children }: Cn<'h1'>) => (
      <h1 style={{ fontSize: isMobile ? '18px' : '20px', fontWeight: 800, color: '#1a1a2e', margin: '28px 0 6px', letterSpacing: '-0.4px' }}>{children}</h1>
    ),
    h2: ({ children }: Cn<'h2'>) => (
      <h2 style={{ fontSize: '13px', fontWeight: 700, color: '#5c5fbc', textTransform: 'uppercase' as const, letterSpacing: '0.5px', margin: '24px 0 10px', paddingTop: '4px' }}>
        {children}
      </h2>
    ),
    h3: ({ children }: Cn<'h3'>) => (
      <h3 style={{ fontSize: '15px', fontWeight: 700, color: '#1a1a2e', margin: '18px 0 8px' }}>{children}</h3>
    ),
    p: ({ children }: Cn<'p'>) => (
      <p style={{ fontSize: '13px', color: '#555', lineHeight: 1.7, margin: '6px 0' }}>{children}</p>
    ),
    blockquote: ({ children }: Cn<'blockquote'>) => (
      <blockquote style={{ fontSize: '14px', color: '#555', lineHeight: 1.7, margin: '10px 0 16px', padding: '12px 16px', background: '#f9f9fd', borderRadius: '8px', borderLeft: '3px solid #5c5fbc' }}>
        {children}
      </blockquote>
    ),
    ul: ({ children }: Cn<'ul'>) => (
      <ul style={{ margin: '6px 0', paddingLeft: 0, listStyle: 'none' }}>{children}</ul>
    ),
    ol: ({ children }: Cn<'ol'>) => (
      <ol style={{ margin: '6px 0', paddingLeft: '20px' }}>{children}</ol>
    ),
    li: ({ children }: Cn<'li'>) => (
      <li style={{ display: 'flex', alignItems: 'flex-start', gap: '10px', margin: '5px 0', fontSize: '13px', color: '#444', lineHeight: 1.6 }}>
        <span style={{ flexShrink: 0, marginTop: '4px', width: '16px', height: '16px', borderRadius: '50%', background: '#ededf7', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#5c5fbc', fontSize: '10px', fontWeight: 700 }}>✓</span>
        <span>{children}</span>
      </li>
    ),
    table: ({ children }: Cn<'table'>) => (
      <div style={{ overflowX: 'auto', margin: '10px 0 16px' }}>
        <table style={{ width: '100%', borderCollapse: 'collapse', fontSize: '13px' }}>{children}</table>
      </div>
    ),
    thead: ({ children }: Cn<'thead'>) => <thead style={{ background: '#f8f8fb' }}>{children}</thead>,
    th: ({ children }: Cn<'th'>) => (
      <th style={{ padding: '9px 12px', textAlign: 'left', color: '#888', fontWeight: 600, borderBottom: '1px solid #eee', whiteSpace: 'nowrap' as const }}>{children}</th>
    ),
    td: ({ children }: Cn<'td'>) => (
      <td style={{ padding: '8px 12px', color: '#555', borderBottom: '1px solid #f5f5f5', verticalAlign: 'top' }}>{children}</td>
    ),
    code: ({ children, className }: Cn<'code'>) => {
      if (className?.startsWith('language-')) {
        return (
          <pre style={{ background: '#f5f5fb', borderRadius: '8px', padding: '14px 16px', overflowX: 'auto', margin: '10px 0' }}>
            <code style={{ fontSize: '12px', color: '#333', fontFamily: 'monospace' }}>{children}</code>
          </pre>
        );
      }
      return <code style={{ fontSize: '12px', color: '#5c5fbc', background: '#ededf7', padding: '2px 6px', borderRadius: '4px', fontFamily: 'monospace' }}>{children}</code>;
    },
    pre: ({ children }: Cn<'pre'>) => <>{children}</>,
    a: ({ children, href }: Cn<'a'>) => (
      <a href={href} target="_blank" rel="noopener noreferrer"
        style={{ color: '#5c5fbc', textDecoration: 'none', fontWeight: 500 }}
        onMouseEnter={e => (e.currentTarget.style.textDecoration = 'underline')}
        onMouseLeave={e => (e.currentTarget.style.textDecoration = 'none')}
      >{children}</a>
    ),
    hr: () => <hr style={{ border: 'none', borderTop: '1px solid #f0f0f0', margin: '20px 0' }} />,
    strong: ({ children }: Cn<'strong'>) => <strong style={{ fontWeight: 700, color: '#1a1a2e' }}>{children}</strong>,
  } as unknown as Record<string, React.ComponentType<never>>;
}
