/*
 * @Author: ZhaoYing 
 * @Date: 2026-02-03 16:42:17 
 * @Last Modified by:   ZhaoYing 
 * @Last Modified time: 2026-02-03 16:42:17 
 */
/**
 * Member Modal
 * Modal for inviting new members or editing existing member roles
 * Generates invitation links for new members
 */

import { forwardRef, useImperativeHandle, useState } from 'react';
import { Form, Input, Select, Modal, App } from 'antd';
import type { SelectProps } from 'antd';
import { useTranslation } from 'react-i18next';
import copy from 'copy-to-clipboard'

import type { MemberModalData, Member, MemberModalRef } from '../types'
import RbModal from '@/components/RbModal'
import { inviteMember, updateMember } from '@/api/member'

const FormItem = Form.Item;
const { Option } = Select;
type LabelRender = SelectProps['labelRender'];

/**
 * Component props
 */
interface MemberModalProps {
  /** Callback to refresh member list */
  refreshTable: () => void;
}

/**
 * Modal for member invitation and editing
 */
const MemberModal = forwardRef<MemberModalRef, MemberModalProps>(({
  refreshTable
}, ref) => {
  const { t } = useTranslation();
  const { message } = App.useApp()
  const initialForm = {
    // role: 'member',
  }
  const [visible, setVisible] = useState(false);
  const [editingUser, setEditingUser] = useState<Member | null>(null);
  const [form] = Form.useForm<MemberModalData>();
  const [loading, setLoading] = useState(false)
  const [modal, contextHolder] = Modal.useModal();

  const roleOptions = [
    'member',
    'manager'
  ]
  const values: MemberModalData = Form.useWatch([], form);

  /** Close modal and reset form */
  const handleClose = () => {
    setVisible(false);
    setEditingUser(null);
    form.resetFields();
    setLoading(false)
  };

  /** Open modal with optional member data for editing */
  const handleOpen = (member?: Member | null) => {
    if (member) {
      setEditingUser(member);
      // Set form values
      form.setFieldsValue({
        email: member.account,
        role: member.role
      });
    } else {
      form.resetFields();
    }
    setVisible(true);
  };
  /** Save member (invite or update) */
  const handleSave = () => {
    form
      .validateFields()
      .then(() => {
        setLoading(true)
        const response = editingUser?.id 
          ? updateMember({
            role: values.role,
            id: editingUser?.id
          }) : inviteMember(values)
          
          response.then((res) => {
            setLoading(false)
            refreshTable()
            if (editingUser?.id) {
              refreshTable()
              handleClose()
            } else {
              const inviteLink = `${window.location.origin}/#/invite-register/${(res as { invite_token: string }).invite_token}`
              modal.confirm({
                title: t('member.inviteLinkTip'),
                content: <a href={inviteLink} target="_blank" rel="noopener noreferrer">{inviteLink}</a>,
                okText: t('common.copy'),
                cancelText: t('common.cancel'),
                okType: 'danger',
                onOk: () => {
                  copy(inviteLink)
                  handleClose()
                  message.success(t('common.copySuccess'))
                }
              })
            }
          })
          .catch(() => {
            setLoading(false)
          });
      })
      .catch((err) => {
        console.log('err', err)
      });
  }

  /** Expose methods to parent component */
  useImperativeHandle(ref, () => ({
    handleOpen,
    handleClose
  }));
  /** Custom label renderer for role select */
  const labelRender: LabelRender = (props) => {
    const { label, value } = props;

    if (label) {
      return t(`member.${value}`);
    }
    return <span>No option match</span>;
  };

  return (
    <RbModal
      title={t(editingUser ? 'member.editMember' : 'member.createMember')}
      open={visible}
      onCancel={handleClose}
      okText={editingUser ? t('common.save') : t('member.sendInvitation')}
      onOk={handleSave}
      confirmLoading={loading}
    >
      <Form
        form={form}
        initialValues={initialForm}
        layout="vertical"
      >
        <FormItem
          name="email"
          label={t('member.email')}
          rules={[
            { required: true, message: t('common.pleaseEnter') },
            { type: 'email' },
            { max: 100 },
          ]}
        >
          <Input placeholder={t('common.enterPlaceholder', { title: t('member.email') })} disabled={!!editingUser} />
        </FormItem>
        
        <FormItem
          name="role"
          label={t('member.inviteToMember')}
          rules={[{ required: true, message: t('common.select') }]}
        >
          <Select 
            placeholder={t('common.select')}
            labelRender={labelRender}
          >
            {roleOptions.map(key => (
              <Option key={key} value={key}>
                {t(`member.${key}`)}
                <div className="rb:text-[#5B6167] rb:text-[12px]">{t(`member.${key}Desc`)} </div>
              </Option>
            ))}
          </Select>
        </FormItem>
      </Form>
      {contextHolder}
    </RbModal>
  );
});

export default MemberModal;