import { Select } from '@pikku/mantine/core';
import type { DB } from '@heygermany/sdk';
import { ApplicantStatuses } from '@heygermany/sdk';
import { useI18n } from '@/context/i18n-provider';

interface ApplicantStatusSelectProps {
  value: DB.ApplicantStatus | null;
  onChange: (value: DB.ApplicantStatus | null) => void;
  isValid: boolean;
}

export const ApplicantStatusSelect: React.FC<ApplicantStatusSelectProps> = ({
  value,
  onChange,
  isValid
}) => {
  const t = useI18n();
  const allStatuses = Object.values(ApplicantStatuses);
  const preferredOrder: DB.ApplicantStatus[] = [
    ApplicantStatuses.INITIAL,
    ApplicantStatuses.PENDING,
    ApplicantStatuses.INVALID,
    ApplicantStatuses.COMPLETE,
  ];

  const orderedStatuses = [
    ...preferredOrder.filter((status) => allStatuses.includes(status)),
    ...allStatuses.filter((status) => !preferredOrder.includes(status)),
  ];

  return (
    <Select
      placeholder={t('backoffice.candidate.fields.selectstatus')}
      value={value}
      onChange={(value) => onChange(value as DB.ApplicantStatus | null)}
      data={orderedStatuses.map((status) => ({
        value: status,
        label: t(`enums.applicantstatus.${status.toLowerCase()}` as any),
        disabled: status === ApplicantStatuses.COMPLETE && !isValid,
      }))}
    />
  );
};
