import React, { useEffect, useState } from 'react';
import styles from './Logo.module.scss';

export interface LogoProps {
  /** Logo color variant - if not provided, automatically switches based on theme */
  variant?: 'black' | 'white' | 'auto';
  /** Logo width */
  width?: number;
  /** Logo height */
  height?: number;
  /** Additional CSS classes */
  className?: string;
}

export const Logo: React.FC<LogoProps> = ({
  variant = 'auto',
  width = 131,
  height = 26,
  className = '',
}) => {
  const [isDarkMode, setIsDarkMode] = useState(false);

  useEffect(() => {
    const checkTheme = () => {
      // Check #sideconvo-app for data-theme (WordPress plugin scoping)
      const appEl = document.getElementById('sideconvo-app');
      if (appEl) {
        setIsDarkMode(appEl.getAttribute('data-theme') === 'dark');
        return;
      }
      // Fallback to documentElement
      const theme = document.documentElement.getAttribute('data-theme');
      setIsDarkMode(theme === 'dark');
    };

    checkTheme();

    // Watch for theme changes
    const target = document.getElementById('sideconvo-app') || document.documentElement;
    const observer = new MutationObserver((mutations) => {
      mutations.forEach((mutation) => {
        if (mutation.attributeName === 'data-theme') {
          checkTheme();
        }
      });
    });

    observer.observe(target, { attributes: true });

    return () => observer.disconnect();
  }, []);

  const getFill = () => {
    if (variant === 'auto') {
      return isDarkMode ? 'white' : '#1A1A1A';
    }
    return variant === 'black' ? '#1A1A1A' : 'white';
  };

  const fill = getFill();

  return (
    <svg
      width={width}
      height={height}
      viewBox="0 0 131 26"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      className={`${styles.logo} ${className}`}
      aria-label="Sideconvo logo"
    >
      <path
        d="M12.9706 11.4363C12.4731 11.4363 12.0458 11.28 11.6917 10.9674C11.3377 10.6548 11.16 10.2781 11.16 9.8389L16.035 2.33039L14.7827 6.96928H24.4611C22.2934 2.82741 17.9626 0 12.9706 0C5.80767 0 0 5.8205 0 12.9992C0 13.5291 0.035869 14.0495 0.0966905 14.5622H12.9706C13.468 14.5622 13.8954 14.7185 14.2494 15.0311C14.6034 15.3437 14.7812 15.7204 14.7812 16.1595L9.90609 23.6681L11.1584 19.0292H1.47999C3.64929 23.171 7.97852 25.9984 12.9706 25.9984C20.135 25.9984 25.9411 20.1779 25.9411 12.9992C25.9411 12.4694 25.9052 11.9489 25.8444 11.4363H12.9706Z"
        fill={fill}
      />
      <path
        d="M31.8392 17.7897L32.9574 15.9689C33.4003 16.2799 33.8713 16.53 34.3688 16.7175C34.8678 16.9067 35.4074 17.0004 35.9922 17.0004C36.627 17.0004 37.1198 16.8832 37.4707 16.6503C37.8216 16.4174 37.9962 16.0673 37.9962 15.6016C37.9962 15.2311 37.8496 14.9232 37.558 14.6747C37.2664 14.4262 36.7704 14.2215 36.0687 14.0589C34.6511 13.726 33.6561 13.2853 33.0837 12.7382C32.5114 12.1912 32.226 11.4691 32.226 10.5735C32.226 9.54507 32.6252 8.70263 33.4221 8.04774C34.2191 7.39286 35.2639 7.06464 36.5568 7.06464C37.1993 7.06464 37.8122 7.14747 38.3955 7.31471C38.9787 7.48195 39.548 7.74609 40.1016 8.1087L39.0598 9.88423C38.5733 9.5732 38.1257 9.35126 37.7202 9.21841C37.3147 9.08556 36.8968 9.01835 36.4679 9.01835C35.8628 9.01835 35.3918 9.13088 35.055 9.35751C34.7197 9.58258 34.5512 9.89517 34.5512 10.2953C34.5512 10.7173 34.7228 11.0299 35.0659 11.2331C35.409 11.4363 35.9876 11.6348 36.8001 11.827C38.0259 12.1224 38.9226 12.5507 39.4918 13.1086C40.061 13.6682 40.3449 14.4122 40.3449 15.3452C40.3449 16.4487 39.9441 17.3271 39.1425 17.982C38.3409 18.6369 37.257 18.9651 35.8924 18.9651C35.1532 18.9651 34.4468 18.8666 33.7715 18.6712C33.0962 18.4759 32.4521 18.1805 31.8392 17.7882V17.7897Z"
        fill={fill}
      />
      <path
        d="M44.0222 5.13593C43.5497 5.13593 43.1583 4.98432 42.8479 4.68111C42.5376 4.37789 42.3832 4.00434 42.3832 3.56046C42.3832 3.11658 42.5376 2.72427 42.8479 2.42887C43.1583 2.13347 43.5497 1.98499 44.0222 1.98499C44.4948 1.98499 44.8971 2.13347 45.2075 2.42887C45.5178 2.72427 45.6722 3.10251 45.6722 3.56046C45.6722 4.01841 45.5178 4.38727 45.2075 4.68736C44.8971 4.98745 44.5026 5.13749 44.0222 5.13749V5.13593ZM45.2964 18.7009H42.7154V7.33347H45.2964V18.7009Z"
        fill={fill}
      />
      <path
        d="M56.3378 16.9348C55.9323 17.6162 55.4177 18.1242 54.7986 18.4602C54.1779 18.7963 53.4511 18.9651 52.6168 18.9651C51.1992 18.9651 50.0607 18.4602 49.1999 17.4505C48.339 16.4409 47.9102 15.0514 47.9102 13.2821C47.9102 11.4019 48.3843 9.89672 49.334 8.76357C50.2822 7.63198 51.5361 7.06462 53.094 7.06462C53.8551 7.06462 54.5038 7.20685 55.0434 7.49131C55.583 7.77577 56.015 8.20715 56.3394 8.78389H56.383V1.87244H58.9641V18.7009H56.383V16.9363H56.3394L56.3378 16.9348ZM50.5348 13.1836C50.5348 14.3527 50.7968 15.2671 51.3208 15.9251C51.8448 16.5831 52.5575 16.9129 53.4589 16.9129C54.3603 16.9129 55.0497 16.5831 55.597 15.9251C56.1429 15.2671 56.4174 14.42 56.4174 13.3837V12.0849C56.4174 11.2409 56.1554 10.536 55.6314 9.97018C55.1074 9.40438 54.4352 9.12149 53.6149 9.12149C52.6542 9.12149 51.9025 9.48253 51.3551 10.2031C50.8093 10.9252 50.5364 11.9176 50.5364 13.1836H50.5348Z"
        fill={fill}
      />
      <path
        d="M71.9112 13.6947H64.1697C64.2134 14.7685 64.5238 15.5844 65.1008 16.1424C65.6762 16.7019 66.481 16.9801 67.5149 16.9801C67.936 16.9801 68.4163 16.8957 68.9544 16.7253C69.494 16.555 70.0242 16.2815 70.5498 15.9032L71.7132 17.5131C71.0114 18.0242 70.2987 18.3946 69.5751 18.6228C68.8514 18.8525 68.0545 18.9666 67.1827 18.9666C65.4174 18.9666 64.045 18.4587 63.0625 17.4412C62.08 16.4237 61.5856 15.0014 61.5778 13.1727C61.5778 11.3518 62.0847 9.87953 63.0999 8.75419C64.1152 7.62885 65.4158 7.06775 67.005 7.06775C68.5941 7.06775 69.7248 7.54758 70.5997 8.50568C71.4746 9.46378 71.9128 10.8204 71.9128 12.5741V13.6947H71.9112ZM69.4191 11.9067C69.4191 10.9298 69.207 10.1953 68.7828 9.70292C68.3586 9.21058 67.7582 8.96519 66.9831 8.96519C66.2517 8.96519 65.6295 9.22934 65.1164 9.75918C64.6033 10.289 64.2867 11.0049 64.1697 11.9067H69.4191Z"
        fill={fill}
      />
      <path
        d="M82.2774 18.057C81.7612 18.3758 81.2434 18.6071 80.7272 18.7509C80.2094 18.8947 79.6449 18.9666 79.032 18.9666C77.4662 18.9666 76.1906 18.454 75.2049 17.4287C74.2193 16.4034 73.7265 15.0076 73.7265 13.2383C73.7265 11.4691 74.2505 9.92329 75.2985 8.77607C76.3465 7.62885 77.7282 7.05524 79.4406 7.05524C79.9568 7.05524 80.4574 7.11619 80.9409 7.2381C81.4243 7.36002 81.9171 7.5507 82.4193 7.81015L81.8329 8.90892C81.4274 8.69479 81.0297 8.5385 80.643 8.44315C80.2547 8.34781 79.832 8.29936 79.3751 8.29936C78.076 8.29936 77.053 8.74793 76.3075 9.64195C75.5621 10.5375 75.1893 11.6879 75.1893 13.0946C75.1893 14.5012 75.5527 15.6797 76.281 16.4971C77.0077 17.3146 77.9591 17.7241 79.1334 17.7241C79.5544 17.7241 79.9708 17.6662 80.3857 17.5521C80.7989 17.438 81.2122 17.2505 81.6255 16.991L82.2789 18.057H82.2774Z"
        fill={fill}
      />
      <path
        d="M89.4434 18.9666C87.7747 18.9666 86.4491 18.429 85.4666 17.3521C84.4841 16.2752 83.9929 14.8638 83.9929 13.118C83.9929 11.2237 84.5044 9.74197 85.5274 8.6729C86.5505 7.60383 87.9182 7.0646 89.6305 7.05835C91.2852 7.05835 92.5843 7.58507 93.5293 8.64007C94.4744 9.69508 94.947 11.1502 94.947 13.0086C94.947 14.8669 94.4526 16.2408 93.4623 17.3317C92.4735 18.4243 91.1324 18.9697 89.4418 18.9697L89.4434 18.9666ZM89.5214 17.735C90.7908 17.735 91.7702 17.3224 92.4564 16.4971C93.1426 15.6719 93.4872 14.5153 93.4872 13.0289C93.4872 11.5425 93.1441 10.3328 92.4564 9.51534C91.7702 8.69791 90.7986 8.28841 89.5432 8.28841C88.2878 8.28841 87.3022 8.71353 86.563 9.56535C85.8253 10.4172 85.4557 11.5925 85.4557 13.0945C85.4557 14.5075 85.8206 15.6344 86.552 16.4752C87.2834 17.3161 88.2722 17.735 89.5198 17.735H89.5214Z"
        fill={fill}
      />
      <path
        d="M106.866 18.7009H105.449V12.1521C105.449 10.8642 105.216 9.89828 104.752 9.25434C104.287 8.61039 103.588 8.28842 102.659 8.28842C101.662 8.28842 100.839 8.66041 100.189 9.40438C99.5382 10.1484 99.2138 11.0783 99.2138 12.1958V18.7009H97.7962V7.33345H99.2138V9.30904H99.2575C99.6785 8.55413 100.206 7.98989 100.842 7.61634C101.477 7.24279 102.216 7.05524 103.056 7.05524C104.29 7.05524 105.234 7.46005 105.887 8.27123C106.54 9.08241 106.868 10.2453 106.868 11.7629V18.7009H106.866Z"
        fill={fill}
      />
      <path
        d="M118.85 7.33345L114.341 18.7009H112.978L108.636 7.33345H110.208L113.332 15.9798C113.427 16.2392 113.505 16.4846 113.565 16.7175C113.624 16.9504 113.672 17.1677 113.708 17.3661H113.752C113.795 17.1067 113.845 16.866 113.901 16.6441C113.958 16.4221 114.018 16.2142 114.084 16.022L117.34 7.33032H118.847L118.85 7.33345Z"
        fill={fill}
      />
      <path
        d="M125.496 18.9666C123.828 18.9666 122.502 18.429 121.52 17.3521C120.537 16.2752 120.046 14.8638 120.046 13.118C120.046 11.2237 120.557 9.74197 121.58 8.6729C122.604 7.60383 123.971 7.0646 125.684 7.05835C127.338 7.05835 128.637 7.58507 129.582 8.64007C130.527 9.69508 131 11.1502 131 13.0086C131 14.8669 130.506 16.2408 129.515 17.3317C128.527 18.4243 127.185 18.9697 125.495 18.9697L125.496 18.9666ZM125.573 17.735C126.842 17.735 127.822 17.3224 128.508 16.4971C129.194 15.6719 129.539 14.5153 129.539 13.0289C129.539 11.5425 129.196 10.3328 128.508 9.51534C127.822 8.69791 126.85 8.28841 125.595 8.28841C124.339 8.28841 123.354 8.71353 122.614 9.56535C121.877 10.4172 121.507 11.5925 121.507 13.0945C121.507 14.5075 121.872 15.6344 122.604 16.4752C123.335 17.3161 124.324 17.735 125.571 17.735H125.573Z"
        fill={fill}
      />
    </svg>
  );
};
