import {
  Button,
  Checkbox,
  Modal,
  ModalBody,
  ModalContent,
  ModalFooter,
  ModalHeader,
} from '@/components/emcn'

interface RemoveMemberDialogProps {
  open: boolean
  memberName: string
  shouldReduceSeats: boolean
  isSelfRemoval?: boolean
  error?: Error | null
  onOpenChange: (open: boolean) => void
  onShouldReduceSeatsChange: (shouldReduce: boolean) => void
  onConfirmRemove: (shouldReduceSeats: boolean) => Promise<void>
  onCancel: () => void
}

export function RemoveMemberDialog({
  open,
  memberName,
  shouldReduceSeats,
  error,
  onOpenChange,
  onShouldReduceSeatsChange,
  onConfirmRemove,
  onCancel,
  isSelfRemoval = false,
}: RemoveMemberDialogProps) {
  return (
    <Modal open={open} onOpenChange={onOpenChange}>
      <ModalContent size='sm'>
        <ModalHeader>{isSelfRemoval ? 'Leave Organization' : 'Remove Team Member'}</ModalHeader>
        <ModalBody>
          <p className='text-[var(--text-secondary)]'>
            {isSelfRemoval ? (
              'Are you sure you want to leave this organization? You will lose access to all team resources.'
            ) : (
              <>
                Are you sure you want to remove{' '}
                <span className='font-medium text-[var(--text-primary)]'>{memberName}</span> from
                the team?
              </>
            )}{' '}
            <span className='text-[var(--text-error)]'>This action cannot be undone.</span>
          </p>

          {!isSelfRemoval && (
            <div className='mt-4'>
              <div className='flex items-center gap-2'>
                <Checkbox
                  id='reduce-seats'
                  checked={shouldReduceSeats}
                  onCheckedChange={(checked) => onShouldReduceSeatsChange(checked === true)}
                />
                <label htmlFor='reduce-seats' className='text-[var(--text-primary)] text-small'>
                  Also reduce seat count in my subscription
                </label>
              </div>
              <p className='mt-1 text-[var(--text-muted)] text-small'>
                If selected, your team seat count will be reduced by 1, lowering your monthly
                billing.
              </p>
            </div>
          )}

          {error && (
            <div className='mt-2'>
              <p className='text-[var(--text-error)] text-small leading-tight'>
                {error instanceof Error && error.message ? error.message : String(error)}
              </p>
            </div>
          )}
        </ModalBody>
        <ModalFooter>
          <Button variant='default' onClick={onCancel}>
            Cancel
          </Button>
          <Button variant='destructive' onClick={() => onConfirmRemove(shouldReduceSeats)}>
            {isSelfRemoval ? 'Leave Organization' : 'Remove'}
          </Button>
        </ModalFooter>
      </ModalContent>
    </Modal>
  )
}
