'use client';

import React from 'react';

interface CrateVisualProps {
  tierId: string;
  size?: number;
  glow?: boolean;
}

export default function CrateVisual({ tierId, size = 180, glow = true }: CrateVisualProps) {
  // Theme styling based on tier
  const themes: Record<
    string,
    {
      primary: string;
      secondary: string;
      accent: string;
      metal: string;
      latches: string;
      glowColor: string;
      badgeText: string;
    }
  > = {
    'crate-bronze': {
      primary: '#bc6666',
      secondary: '#bf9393',
      accent: '#d98383',
      metal: '#b49494',
      latches: '#e69292',
      glowColor: 'rgba(217, 131, 131, 0.45)',
      badgeText: 'BRONZE',
    },
    'crate-silver': {
      primary: '#551212',
      secondary: '#3d1616',
      accent: '#7e0b0b',
      metal: '#512f2f',
      latches: '#f8e3e3',
      glowColor: 'rgba(248, 227, 227, 0.45)',
      badgeText: 'SILVER',
    },
    'crate-gold': {
      primary: '#f7ebeb',
      secondary: '#e0c4c4',
      accent: '#fbf1f1',
      metal: '#ede7e7',
      latches: '#fef7f7',
      glowColor: 'rgba(251, 241, 241, 0.55)',
      badgeText: 'GOLD',
    },
    'crate-obsidian': {
      primary: '#cc8a8a',
      secondary: '#e5d7d7',
      accent: '#fceaea',
      metal: '#1e0533',
      latches: '#f4d3d3',
      glowColor: 'rgba(252, 234, 234, 0.6)',
      badgeText: 'OBSIDIAN',
    },
  };

  const theme = themes[tierId] || themes['crate-bronze'];
  const width = size;
  const height = size * 0.72;

  return (
    <div
      style={{
        position: 'relative',
        width,
        height,
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
      }}
    >
      {/* Ambient background bloom */}
      {glow && (
        <div
          style={{
            position: 'absolute',
            width: width * 0.75,
            height: height * 0.75,
            background: theme.glowColor,
            filter: 'blur(32px)',
            borderRadius: '50%',
            opacity: 0.7,
            pointerEvents: 'none',
          }}
        />
      )}

      {/* Isometric CS2 Armory Weapon Case Vector SVG */}
      <svg
        width={width}
        height={height}
        viewBox="0 0 240 170"
        fill="none"
        xmlns="http://www.w3.org/2000/svg"
        style={{
          filter: `drop-shadow(0 14px 22px rgba(0,0,0,0.65)) drop-shadow(0 0 16px ${theme.glowColor})`,
          transition: 'transform 0.3s ease',
        }}
      >
        <defs>
          <linearGradient id={`topLid-${tierId}`} x1="30" y1="20" x2="210" y2="70" gradientUnits="userSpaceOnUse">
            <stop offset="0%" stopColor={theme.accent} />
            <stop offset="50%" stopColor={theme.primary} />
            <stop offset="100%" stopColor={theme.secondary} />
          </linearGradient>

          <linearGradient id={`frontFace-${tierId}`} x1="30" y1="70" x2="210" y2="155" gradientUnits="userSpaceOnUse">
            <stop offset="0%" stopColor={theme.primary} />
            <stop offset="70%" stopColor={theme.secondary} />
            <stop offset="100%" stopColor={theme.metal} />
          </linearGradient>

          <linearGradient id={`metalTrim-${tierId}`} x1="0" y1="0" x2="100" y2="100" gradientUnits="userSpaceOnUse">
            <stop offset="0%" stopColor="#2e1f1f" />
            <stop offset="100%" stopColor="#0d0f14" />
          </linearGradient>

          <filter id={`neonGlow-${tierId}`} x="-20%" y="-20%" width="140%" height="140%">
            <feGaussianBlur stdDeviation="3" result="blur" />
            <feComposite in="SourceGraphic" in2="blur" operator="over" />
          </filter>
        </defs>

        {/* Crate Base Shadow */}
        <ellipse cx="120" cy="158" rx="85" ry="10" fill="rgba(0,0,0,0.5)" filter="blur(4px)" />

        {/* Front Body */}
        <path
          d="M30 68 L210 68 L198 150 L42 150 Z"
          fill={`url(#frontFace-${tierId})`}
          stroke="#000"
          strokeWidth="2.5"
        />

        {/* Top Lid (Isometric Bevel) */}
        <path
          d="M55 24 L185 24 L210 68 L30 68 Z"
          fill={`url(#topLid-${tierId})`}
          stroke="#000"
          strokeWidth="2.5"
        />

        {/* Top Handle */}
        <rect
          x="95"
          y="15"
          width="50"
          height="12"
          rx="5"
          fill="url(#metalTrim-crate-bronze)"
          stroke="#111"
          strokeWidth="2"
        />
        <rect x="105" y="18" width="30" height="6" rx="3" fill="#3b2c2c" />

        {/* Lid Ridges & Stiffening ribs */}
        <line x1="80" y1="32" x2="68" y2="60" stroke="rgba(0,0,0,0.4)" strokeWidth="2.5" />
        <line x1="82" y1="32" x2="70" y2="60" stroke="rgba(0, 0, 0, 0.2)" strokeWidth="1" />
        <line x1="160" y1="32" x2="172" y2="60" stroke="rgba(0,0,0,0.4)" strokeWidth="2.5" />
        <line x1="162" y1="32" x2="174" y2="60" stroke="rgba(0, 0, 0, 0.2)" strokeWidth="1" />

        {/* Heavy Duty Corner Bumpers */}
        {/* Top Left */}
        <polygon points="30,68 45,68 48,50 38,42" fill="#181a20" stroke="#000" strokeWidth="1.5" />
        {/* Top Right */}
        <polygon points="210,68 195,68 192,50 202,42" fill="#181a20" stroke="#000" strokeWidth="1.5" />
        {/* Bottom Left */}
        <polygon points="30,68 42,150 56,150 48,85" fill="#181a20" stroke="#000" strokeWidth="1.5" />
        {/* Bottom Right */}
        <polygon points="210,68 198,150 184,150 192,85" fill="#181a20" stroke="#000" strokeWidth="1.5" />

        {/* Rivets on Bumpers */}
        <circle cx="48" cy="85" r="2" fill="#531414" />
        <circle cx="45" cy="115" r="2" fill="#531414" />
        <circle cx="43" cy="142" r="2" fill="#531414" />
        <circle cx="192" cy="85" r="2" fill="#531414" />
        <circle cx="195" cy="115" r="2" fill="#531414" />
        <circle cx="197" cy="142" r="2" fill="#531414" />

        {/* Center Case Crest / Diamond Emblem */}
        <g transform="translate(120, 108)">
          {/* Outer Diamond */}
          <polygon
            points="0,-24 24,0 0,24 -24,0"
            fill="#111318"
            stroke={theme.latches}
            strokeWidth="2"
            filter={`url(#neonGlow-${tierId})`}
          />
          {/* Inner Inset */}
          <polygon points="0,-18 18,0 0,18 -18,0" fill="#3a2c2c" />
          {/* Center Vault Logo Mark */}
          <path
            d="M-8 -6 L0 -14 L8 -6 L4 10 L-4 10 Z"
            fill={theme.accent}
            opacity="0.95"
          />
          <circle cx="0" cy="1" r="3" fill="#000" />
        </g>

        {/* Front Latches / Padlock Mechanisms */}
        {/* Left Latch */}
        <g transform="translate(68, 62)">
          <rect x="0" y="0" width="16" height="24" rx="2" fill="#1a1c23" stroke="#000" strokeWidth="1.5" />
          <rect x="3" y="4" width="10" height="8" rx="1" fill={theme.latches} />
          <circle cx="8" cy="18" r="1.5" fill="#000" />
        </g>

        {/* Right Latch */}
        <g transform="translate(156, 62)">
          <rect x="0" y="0" width="16" height="24" rx="2" fill="#1a1c23" stroke="#000" strokeWidth="1.5" />
          <rect x="3" y="4" width="10" height="8" rx="1" fill={theme.latches} />
          <circle cx="8" cy="18" r="1.5" fill="#000" />
        </g>

        {/* Horizontal Seam (Lid Joint) */}
        <line x1="30" y1="68" x2="210" y2="68" stroke="#000" strokeWidth="3" />
        <line x1="31" y1="69" x2="209" y2="69" stroke={theme.latches} strokeWidth="1" opacity="0.6" />

        {/* Bottom Stiffening Bars */}
        <rect x="65" y="140" width="110" height="5" rx="1" fill="#12141a" opacity="0.8" />

        {/* Stenciled Tier Label */}
        <text
          x="120"
          y="145"
          textAnchor="middle"
          fill="rgba(0, 0, 0, 0.4)"
          fontSize="8"
          fontWeight="900"
          fontFamily="'Space Grotesk', sans-serif"
          letterSpacing="2.5"
        >
          {theme.badgeText}
        </text>
      </svg>
    </div>
  );
}
