'use client';

/** HUB-715: a team inbox's automatic assignment of new conversations. */
import type { InboxRouting, RoutedChannel } from '@/features/inbox/types';

const CHANNELS: [RoutedChannel, string][] = [
  ['email', 'Email'],
  ['chat', 'Live chat'],
  ['form', 'Website forms'],
];

const choice = { display: 'flex', gap: 6, alignItems: 'center' } as const;

export const routingOf = (saved: InboxRouting | null | undefined): InboxRouting =>
  saved ?? { enabled: false, channels: CHANNELS.map(([id]) => id), teamId: null };

export const routingProblem = (routing: InboxRouting): string | null =>
  routing.enabled && !routing.channels.length
    ? 'Choose which new conversations to assign in turn'
    : null;

export function RoutingFields({
  value,
  teams,
  onChange,
}: {
  value: InboxRouting;
  /** Teams with a member of the inbox, as last saved. */
  teams: { id: string; name: string }[];
  onChange: (next: InboxRouting) => void;
}) {
  return (
    <fieldset style={{ border: 0, padding: 0, margin: '8px 0 0' }}>
      <label style={choice}>
        <input
          type="checkbox"
          checked={value.enabled}
          onChange={(e) => onChange({ ...value, enabled: e.target.checked })}
        />
        Assign new conversations to members in turn
      </label>
      {value.enabled ? (
        <div style={{ margin: '6px 0 0 22px' }}>
          <div
            role="group"
            aria-label="Assign in turn"
            style={{ display: 'flex', flexWrap: 'wrap', gap: '6px 16px' }}
          >
            {CHANNELS.map(([id, label]) => (
              <label key={id} style={choice}>
                <input
                  type="checkbox"
                  checked={value.channels.includes(id)}
                  onChange={(e) =>
                    onChange({
                      ...value,
                      channels: CHANNELS.map(([c]) => c).filter((c) =>
                        c === id ? e.target.checked : value.channels.includes(c),
                      ),
                    })
                  }
                />
                {label}
              </label>
            ))}
          </div>
          <label style={{ ...choice, marginTop: 8 }}>
            Share between
            <select
              className="input"
              style={{ width: 'auto', minWidth: 220 }}
              value={value.teamId ?? ''}
              onChange={(e) => onChange({ ...value, teamId: e.target.value || null })}
            >
              <option value="">All members of this inbox</option>
              {teams.map((team) => (
                <option key={team.id} value={team.id}>
                  Team: {team.name}
                </option>
              ))}
              {value.teamId && !teams.some((team) => team.id === value.teamId) ? (
                <option value={value.teamId}>A team with nobody in this inbox</option>
              ) : null}
            </select>
          </label>
          <p className="crm-text-muted crm-fs-8 crm-mt-2">
            {value.teamId
              ? 'Only the team’s members who are in this inbox and set to available take a turn. While none is, the conversation waits in the team’s queue and is given out soon after one is available.'
              : 'Only members who are set to available take a turn. If nobody is, the conversation stays unassigned.'}{' '}
            Read-only members are never assigned.
          </p>
          {teams.length ? null : (
            <p className="crm-text-muted crm-fs-8">
              To assign to a team, add one of its members to this inbox and save.
            </p>
          )}
        </div>
      ) : null}
    </fieldset>
  );
}
