// Realistic t-shirt mockup SVGs for 4 zones (front, back, leftSleeve, rightSleeve).
// Each renders a t-shirt with shadows/folds and overlays a print zone where
// designs (logos) get rendered with drag/resize/rotate support handled by the editor.

// Color palettes for shirts
const SHIRT_FILLS = {
  white: { body: '#f3f3f1', shade: '#d8d8d4', deep: '#b8b8b3', neck: '#cfcfc9', stitch: '#a0a09a', fold: '#c8c8c2' },
  black: { body: '#1a1a1c', shade: '#0a0a0c', deep: '#000', neck: '#0c0c0e', stitch: '#3a3a3d', fold: '#2a2a2d' },
};

// The PrintZone is a positioned rect on the shirt where the design canvas lives.
// Coordinates are in the SVG viewBox space. Editor will use these to convert to mm.

// Zone print rectangles in the SVG (x,y,width,height) for each view
const ZONE_RECTS = {
  front:       { x: 130, y: 130, w: 240, h: 280 },
  back:        { x: 110, y: 100, w: 280, h: 340 },
  sleeve:      { x: 60,  y: 60,  w: 180, h: 220 },
};

function useSvgId(prefix) {
  const ref = React.useRef(null);
  if (!ref.current) ref.current = `${prefix}-${Math.random().toString(36).slice(2, 10)}`;
  return ref.current;
}

function ShirtFront({ shirtColor = 'white', children, showZoneOutline = true }) {
  const c = SHIRT_FILLS[shirtColor];
  const shadeId = useSvgId('frontShade');
  const centerId = useSvgId('frontCenter');
  return (
    <svg viewBox="0 0 500 600" preserveAspectRatio="xMidYMid meet" style={{ width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={shadeId} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor={c.body}/>
          <stop offset="50%" stopColor={c.body}/>
          <stop offset="100%" stopColor={c.shade}/>
        </linearGradient>
        <radialGradient id={centerId} cx="0.5" cy="0.45" r="0.6">
          <stop offset="0%" stopColor={c.body} stopOpacity="0"/>
          <stop offset="80%" stopColor={c.shade} stopOpacity="0.55"/>
          <stop offset="100%" stopColor={c.deep} stopOpacity="0.7"/>
        </radialGradient>
      </defs>
      {/* shirt body */}
      <path d="M 110 90
               L 180 60
               L 200 70
               Q 250 100 300 70
               L 320 60
               L 390 90
               L 440 200
               L 405 230
               L 395 260
               L 395 540
               Q 250 555 105 540
               L 105 260
               L 95 230
               L 60 200 Z"
            fill={`url(#${shadeId})`} stroke={c.deep} strokeWidth="1.5"/>
      <path d="M 110 90 L 180 60 L 200 70 Q 250 100 300 70 L 320 60 L 390 90 L 440 200 L 405 230 L 395 260 L 395 540 Q 250 555 105 540 L 105 260 L 95 230 L 60 200 Z"
            fill={`url(#${centerId})`} pointerEvents="none"/>
      {/* neckline */}
      <path d="M 200 70 Q 250 100 300 70 Q 285 95 250 95 Q 215 95 200 70 Z" fill={c.neck} stroke={c.deep} strokeWidth="1.5"/>
      <path d="M 205 73 Q 250 98 295 73" fill="none" stroke={c.stitch} strokeWidth="0.8" strokeDasharray="3 2"/>
      {/* sleeve seam */}
      <path d="M 110 90 L 95 230" fill="none" stroke={c.fold} strokeWidth="1" opacity="0.5"/>
      <path d="M 390 90 L 405 230" fill="none" stroke={c.fold} strokeWidth="1" opacity="0.5"/>
      {/* subtle folds */}
      <path d="M 250 110 Q 240 220 250 350 Q 258 450 250 540" stroke={c.fold} strokeWidth="0.8" fill="none" opacity="0.35"/>
      <path d="M 180 250 Q 200 350 195 480" stroke={c.fold} strokeWidth="0.6" fill="none" opacity="0.25"/>
      <path d="M 320 250 Q 300 350 305 480" stroke={c.fold} strokeWidth="0.6" fill="none" opacity="0.25"/>
      {/* hem stitch */}
      <path d="M 105 540 Q 250 555 395 540" fill="none" stroke={c.stitch} strokeWidth="0.6" strokeDasharray="3 3"/>

      {/* Print zone overlay */}
      {showZoneOutline && (
        <rect x={ZONE_RECTS.front.x} y={ZONE_RECTS.front.y} width={ZONE_RECTS.front.w} height={ZONE_RECTS.front.h}
              fill="none" stroke="var(--accent, #00ff9c)" strokeWidth="1" strokeDasharray="4 4" opacity="0.55"/>
      )}
      {children}
    </svg>
  );
}

function ShirtBack({ shirtColor = 'white', children, showZoneOutline = true }) {
  const c = SHIRT_FILLS[shirtColor];
  const shadeId = useSvgId('backShade');
  const centerId = useSvgId('backCenter');
  return (
    <svg viewBox="0 0 500 600" preserveAspectRatio="xMidYMid meet" style={{ width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={shadeId} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor={c.body}/>
          <stop offset="100%" stopColor={c.shade}/>
        </linearGradient>
        <radialGradient id={centerId} cx="0.5" cy="0.5" r="0.6">
          <stop offset="0%" stopColor={c.body} stopOpacity="0"/>
          <stop offset="85%" stopColor={c.shade} stopOpacity="0.45"/>
          <stop offset="100%" stopColor={c.deep} stopOpacity="0.65"/>
        </radialGradient>
      </defs>
      {/* shirt body (back has higher neckline) */}
      <path d="M 110 90
               L 180 60
               L 210 75
               Q 250 90 290 75
               L 320 60
               L 390 90
               L 440 200
               L 405 230
               L 395 260
               L 395 540
               Q 250 555 105 540
               L 105 260
               L 95 230
               L 60 200 Z"
            fill={`url(#${shadeId})`} stroke={c.deep} strokeWidth="1.5"/>
      <path d="M 110 90 L 180 60 L 210 75 Q 250 90 290 75 L 320 60 L 390 90 L 440 200 L 405 230 L 395 260 L 395 540 Q 250 555 105 540 L 105 260 L 95 230 L 60 200 Z"
            fill={`url(#${centerId})`} pointerEvents="none"/>
      {/* back neck label */}
      <path d="M 210 75 Q 250 88 290 75" fill="none" stroke={c.stitch} strokeWidth="0.8"/>
      <rect x="232" y="80" width="36" height="14" fill={c.neck} opacity="0.6"/>
      {/* center fold */}
      <path d="M 250 95 Q 248 250 252 540" stroke={c.fold} strokeWidth="0.6" fill="none" opacity="0.4"/>
      <path d="M 110 90 L 95 230" fill="none" stroke={c.fold} strokeWidth="1" opacity="0.5"/>
      <path d="M 390 90 L 405 230" fill="none" stroke={c.fold} strokeWidth="1" opacity="0.5"/>
      <path d="M 105 540 Q 250 555 395 540" fill="none" stroke={c.stitch} strokeWidth="0.6" strokeDasharray="3 3"/>

      {showZoneOutline && (
        <rect x={ZONE_RECTS.back.x} y={ZONE_RECTS.back.y} width={ZONE_RECTS.back.w} height={ZONE_RECTS.back.h}
              fill="none" stroke="var(--accent, #00ff9c)" strokeWidth="1" strokeDasharray="4 4" opacity="0.55"/>
      )}
      {children}
    </svg>
  );
}

function ShirtSleeve({ shirtColor = 'white', children, showZoneOutline = true }) {
  const c = SHIRT_FILLS[shirtColor];
  const shadeId = useSvgId('slvShade');
  return (
    <svg viewBox="0 0 300 340" preserveAspectRatio="xMidYMid meet" style={{ width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={shadeId} x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stopColor={c.body}/>
          <stop offset="100%" stopColor={c.shade}/>
        </linearGradient>
      </defs>
      <path d="M 30 30
               L 240 30
               Q 270 30 270 70
               L 250 320
               Q 150 332 50 320
               Z"
            fill={`url(#${shadeId})`} stroke={c.deep} strokeWidth="1.5"/>
      <path d="M 30 30 L 50 320" stroke={c.stitch} strokeWidth="0.8" fill="none" strokeDasharray="3 3"/>
      <path d="M 50 320 Q 150 332 250 320" stroke={c.stitch} strokeWidth="0.8" fill="none" strokeDasharray="3 3"/>
      <rect x="55" y="295" width="190" height="3" fill={c.fold} opacity="0.4"/>
      <path d="M 90 50 Q 110 180 100 310" stroke={c.fold} strokeWidth="0.6" fill="none" opacity="0.35"/>
      <path d="M 200 50 Q 195 180 205 310" stroke={c.fold} strokeWidth="0.6" fill="none" opacity="0.3"/>

      {showZoneOutline && (
        <rect x={ZONE_RECTS.sleeve.x + 30} y={ZONE_RECTS.sleeve.y + 20} width={ZONE_RECTS.sleeve.w} height={ZONE_RECTS.sleeve.h}
              fill="none" stroke="var(--accent, #00ff9c)" strokeWidth="1" strokeDasharray="4 4" opacity="0.55"/>
      )}
      {children}
    </svg>
  );
}

Object.assign(window, { ShirtFront, ShirtBack, ShirtSleeve, ZONE_RECTS, SHIRT_FILLS });
