
// Campaign Table Component

const { useState, useMemo } = React;

function CampaignTable({ campaigns, onTriggerCall }) {
  const [sortBy, setSortBy] = useState('created');
  const [sortOrder, setSortOrder] = useState('desc');

  const sortedCampaigns = useMemo(() => {
    const sorted = [...campaigns].sort((a, b) => {
      let aVal, bVal;
      
      switch(sortBy) {
        case 'name':
          aVal = a.name.toLowerCase();
          bVal = b.name.toLowerCase();
          break;
        case 'totalLeads':
          aVal = a.totalLeads;
          bVal = b.totalLeads;
          break;
        case 'created':
        default:
          aVal = new Date(a.created);
          bVal = new Date(b.created);
      }
      
      if (sortOrder === 'asc') {
        return aVal > bVal ? 1 : -1;
      } else {
        return aVal < bVal ? 1 : -1;
      }
    });
    
    return sorted;
  }, [campaigns, sortBy, sortOrder]);

  const handleSort = (column) => {
    if (sortBy === column) {
      setSortOrder(sortOrder === 'asc' ? 'desc' : 'asc');
    } else {
      setSortBy(column);
      setSortOrder('desc');
    }
  };

  const SortIcon = ({ column }) => {
    if (sortBy !== column) return null;
    return (
      <svg width="12" height="12" viewBox="0 0 12 12" fill="none" style={{ marginLeft: '4px' }}>
        {sortOrder === 'asc' ? (
          <path d="M6 3L9 7H3L6 3Z" fill="#4CAF50"/>
        ) : (
          <path d="M6 9L3 5H9L6 9Z" fill="#4CAF50"/>
        )}
      </svg>
    );
  };

  const TableHeader = ({ label, column, width }) => (
    <th
      onClick={() => column && handleSort(column)}
      style={{
        padding: '14px 16px',
        textAlign: 'left',
        fontSize: '13px',
        fontWeight: 600,
        color: '#5B6B7A',
        borderBottom: '1px solid #ECECEA',
        cursor: column ? 'pointer' : 'default',
        userSelect: 'none',
        width: width || 'auto'
      }}
    >
      <div style={{ display: 'flex', alignItems: 'center' }}>
        {label}
        {column && <SortIcon column={column} />}
      </div>
    </th>
  );

  return (
    <div style={{ overflowX: 'auto' }}>
      <table style={{
        width: '100%',
        borderCollapse: 'collapse',
        background: '#fff'
      }}>
        <thead>
          <tr>
            <TableHeader label="Campaign name" column="name" width="250px" />
            <TableHeader label="AI Agent" width="200px" />
            <TableHeader label="Total leads" column="totalLeads" />
            <TableHeader label="Connected leads" />
            <TableHeader label="Total calls" />
            <TableHeader label="Connected calls" />
            <TableHeader label="Created" column="created" />
            <TableHeader label="Status" />
            <TableHeader label="Actions" width="100px" />
          </tr>
        </thead>
        <tbody>
          {sortedCampaigns.map((campaign) => (
            <tr key={campaign.id} style={{
              borderBottom: '1px solid #F5F5F5',
              transition: 'background 150ms ease'
            }}
            onMouseEnter={(e) => e.currentTarget.style.background = '#FAFAFA'}
            onMouseLeave={(e) => e.currentTarget.style.background = '#fff'}
            >
              <td style={{ padding: '12px', fontSize: '13px', fontWeight: 500, color: '#1F2433', verticalAlign: 'middle' }}>
                {campaign.name}
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <a
                  href={campaign.aiAgentLink}
                  target="_blank"
                  rel="noopener noreferrer"
                  style={{
                    fontSize: '13px',
                    color: '#4CAF50',
                    textDecoration: 'none',
                    display: 'inline-flex',
                    alignItems: 'center',
                    gap: '4px'
                  }}
                >
                  {campaign.aiAgent}
                  <svg width="11" height="11" viewBox="0 0 12 12" fill="none">
                    <path d="M9 6.5V9.5C9 9.77614 8.77614 10 8.5 10H2.5C2.22386 10 2 9.77614 2 9.5V3.5C2 3.22386 2.22386 3 2.5 3H5.5M7 2H10M10 2V5M10 2L5.5 6.5" stroke="currentColor" strokeWidth="1.2" strokeLinecap="round" strokeLinejoin="round"/>
                  </svg>
                </a>
              </td>
              <td style={{ padding: '12px', fontSize: '13px', color: '#1F2433', verticalAlign: 'middle' }}>
                {campaign.totalLeads}
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <span style={{ fontSize: '13px', color: '#1F2433' }}>{campaign.connectedLeads}</span>
                <PercentageBadge percentage={campaign.connectedLeadsPercentage} />
              </td>
              <td style={{ padding: '12px', fontSize: '13px', color: '#1F2433', verticalAlign: 'middle' }}>
                {campaign.totalCalls}
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <span style={{ fontSize: '13px', color: '#1F2433' }}>{campaign.connectedCalls}</span>
                <PercentageBadge percentage={campaign.connectedCallsPercentage} />
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: '6px' }}>
                  <Avatar letter={campaign.createdBy} />
                  <span style={{ fontSize: '12px', color: '#5B6B7A' }}>{campaign.created}</span>
                </div>
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <StatusBadge status={campaign.status} type={campaign.statusType} />
              </td>
              <td style={{ padding: '12px', verticalAlign: 'middle' }}>
                <div style={{ display: 'flex', gap: '8px', alignItems: 'center' }}>
                  <Button
                    variant="primary"
                    size="small"
                    onClick={() => onTriggerCall(campaign)}
                    icon={
                      <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
                        <path d="M8.75 2.33333C9.20166 2.43958 9.62468 2.64917 9.98264 2.94726C10.3406 3.24535 10.6252 3.62498 10.8146 4.05556M8.75 1.16667C9.71462 1.29495 10.6233 1.69944 11.3628 2.32994C12.1024 2.96044 12.6433 3.78951 12.9227 4.71667M11.2727 10.0333V11.4333C11.2733 11.5746 11.2447 11.7146 11.1888 11.8447C11.1328 11.9748 11.0507 12.0922 10.9477 12.1896C10.8447 12.287 10.7227 12.3625 10.5893 12.4113C10.456 12.4601 10.3141 12.481 10.1726 12.4725C8.70869 12.3169 7.29816 11.8409 6.04086 11.0792C4.87564 10.3853 3.87196 9.45754 3.09682 8.35222C2.32999 7.15167 1.8466 5.80556 1.68682 4.40556C1.67822 4.26672 1.69902 4.12734 1.74782 3.99622C1.79662 3.8651 1.87235 3.74501 1.96989 3.64331C2.06743 3.54161 2.18456 3.46041 2.31397 3.40515C2.44338 3.34989 2.58227 3.32176 2.72273 3.32222H4.15C4.40295 3.31995 4.64873 3.40999 4.84258 3.57496C5.03643 3.73992 5.16692 3.96906 5.20909 4.22056C5.28779 4.72233 5.42755 5.21363 5.62545 5.68556C5.69708 5.86703 5.71497 6.0651 5.67714 6.25649C5.63931 6.44788 5.54735 6.62428 5.41364 6.76444L4.78364 7.38444C5.48628 8.58607 6.47927 9.52148 7.65909 10.1853L8.28909 9.57C8.43238 9.43962 8.61205 9.35071 8.80703 9.31455C9.002 9.27839 9.20327 9.29651 9.38773 9.36667C9.87387 9.55806 10.3812 9.69279 10.8991 9.76889C11.1572 9.81058 11.3924 9.93905 11.5611 10.1317C11.7297 10.3244 11.8211 10.5698 11.8182 10.8222" stroke="white" strokeWidth="1.3" strokeLinecap="round" strokeLinejoin="round"/>
                      </svg>
                    }
                  >
                    Trigger
                  </Button>
                  <IconButton
                    onClick={() => alert('Edit campaign: ' + campaign.name)}
                    icon={
                      <svg width="16" height="16" viewBox="0 0 18 18" fill="none">
                        <path d="M8.25 3H3C2.60218 3 2.22064 3.15804 1.93934 3.43934C1.65804 3.72064 1.5 4.10218 1.5 4.5V15C1.5 15.3978 1.65804 15.7794 1.93934 16.0607C2.22064 16.342 2.60218 16.5 3 16.5H13.5C13.8978 16.5 14.2794 16.342 14.5607 16.0607C14.842 15.7794 15 15.3978 15 15V9.75M13.875 1.875C14.1734 1.57663 14.5784 1.40918 15 1.40918C15.4216 1.40918 15.8266 1.57663 16.125 1.875C16.4234 2.17337 16.5908 2.57835 16.5908 3C16.5908 3.42165 16.4234 3.82663 16.125 4.125L9 11.25L6 12L6.75 9L13.875 1.875Z" stroke="#5B6B7A" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
                      </svg>
                    }
                    tooltip="Edit"
                  />
                  <IconButton
                    onClick={() => {
                      if (confirm('Delete campaign "' + campaign.name + '"?')) {
                        alert('Delete functionality would be implemented here');
                      }
                    }}
                    icon={
                      <svg width="16" height="16" viewBox="0 0 18 18" fill="none">
                        <path d="M2.25 4.5H3.75H15.75M6 4.5V3C6 2.60218 6.15804 2.22064 6.43934 1.93934C6.72064 1.65804 7.10218 1.5 7.5 1.5H10.5C10.8978 1.5 11.2794 1.65804 11.5607 1.93934C11.842 2.22064 12 2.60218 12 3V4.5M14.25 4.5V15C14.25 15.3978 14.092 15.7794 13.8107 16.0607C13.5294 16.342 13.1478 16.5 12.75 16.5H5.25C4.85218 16.5 4.47064 16.342 4.18934 16.0607C3.90804 15.7794 3.75 15.3978 3.75 15V4.5H14.25Z" stroke="#DC2626" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
                      </svg>
                    }
                    tooltip="Delete"
                  />
                </div>
              </td>
            </tr>
          ))}
        </tbody>
      </table>
    </div>
  );
}
