import analysisAgentAvatar from './avatar-assets/analysis-agent.png';
import alertAgentAvatar from './avatar-assets/alert-agent.png';
import executionAgentAvatar from './avatar-assets/execution-agent.png';
import outboundAgentAvatar from './avatar-assets/outbound-agent.png';
import plannerAgentAvatar from './avatar-assets/planner-agent.png';

export const AGENT_FAMILY_SIZE_DIMENSIONS = {
  xxs: 16,
  mini: 20,
  xs: 24,
  s: 32,
  default: 40,
  m: 48,
};

export const AGENT_FAMILY_AVATARS = [
  {
    id: 'planner',
    name: '规划Agent',
    src: plannerAgentAvatar,
    alt: '规划Agent头像',
    background: 'var(--semi-color-data-0, #24cda5)',
    paddingTop: 1,
    marginLeft: -5,
    imageWidth: 48,
    imageHeight: 48,
  },
  {
    id: 'analysis',
    name: '研判Agent',
    src: analysisAgentAvatar,
    alt: '研判Agent头像',
    background: 'var(--semi-color-data-8, #b399fe)',
    paddingTop: 2,
    marginLeft: -2,
    imageWidth: 47,
    imageHeight: 47,
  },
  {
    id: 'execution',
    name: '执行Agent',
    src: executionAgentAvatar,
    alt: '执行Agent头像',
    background: '#3f81eb',
    paddingTop: 2,
    marginLeft: -6,
    imageWidth: 49,
    imageHeight: 49,
  },
  {
    id: 'outbound',
    name: '外呼Agent',
    src: outboundAgentAvatar,
    alt: '外呼Agent头像',
    background: '#a1e066',
    paddingTop: 5,
    marginLeft: -6,
    imageWidth: 50,
    imageHeight: 50,
  },
  {
    id: 'alert',
    name: '异常提醒Agent',
    src: alertAgentAvatar,
    alt: '异常提醒Agent头像',
    background: 'var(--semi-color-data-6, #f2496f)',
    paddingTop: 3,
    marginLeft: -7,
    imageWidth: 50,
    imageHeight: 50,
  },
];

const BASE_AGENT_FAMILY_SIZE = 36;

export function getAgentFamilyAvatar(agentFamilyAvatar) {
  return AGENT_FAMILY_AVATARS.find((avatar) => avatar.id === agentFamilyAvatar) || AGENT_FAMILY_AVATARS[0];
}

function resolveContainerSize(size) {
  if (typeof size === 'number' && Number.isFinite(size) && size > 0) return size;
  if (typeof size === 'string' && Object.prototype.hasOwnProperty.call(AGENT_FAMILY_SIZE_DIMENSIONS, size)) {
    return AGENT_FAMILY_SIZE_DIMENSIONS[size];
  }
  return BASE_AGENT_FAMILY_SIZE;
}

export default function AgentFamilyAvatar({
  avatar = 'planner',
  size = 'default',
  className = '',
  style = null,
}) {
  const resolvedAvatar = typeof avatar === 'string' ? getAgentFamilyAvatar(avatar) : avatar;
  const containerSize = resolveContainerSize(size);
  const scale = containerSize / BASE_AGENT_FAMILY_SIZE;

  return (
    <div
      className={['tfds-agent-family-avatar flex items-start overflow-hidden rounded-full', className].filter(Boolean).join(' ')}
      style={{
        width: containerSize,
        height: containerSize,
        background: resolvedAvatar.background,
        paddingTop: resolvedAvatar.paddingTop * scale,
        ...style,
      }}
      data-tfds-component="AgentFamilyAvatar"
    >
      <img
        src={resolvedAvatar.src}
        alt={resolvedAvatar.alt}
        className="block max-w-none shrink-0"
        style={{
          marginLeft: resolvedAvatar.marginLeft * scale,
          width: resolvedAvatar.imageWidth * scale,
          height: resolvedAvatar.imageHeight * scale,
        }}
        draggable="false"
      />
    </div>
  );
}
