import type { Column } from 'element-plus';

import type { VbenFormSchema } from '#/adapter/form';

import { $t } from '@vben/locales';

type TagType = 'danger' | 'info' | 'primary' | 'success' | 'warning';

export function getStatusOptions() {
  return [
    { type: 'danger' as TagType, label: $t('loginLog.statusFailed'), value: 0 },
    {
      type: 'success' as TagType,
      label: $t('loginLog.statusSuccess'),
      value: 1,
    },
  ];
}

export function getFailureReasonOptions() {
  return [
    { label: $t('loginLog.failureReasonUnknown'), value: 0 },
    { label: $t('loginLog.failureReasonUserNotExist'), value: 1 },
    { label: $t('loginLog.failureReasonPasswordError'), value: 2 },
    { label: $t('loginLog.failureReasonUserDisabled'), value: 3 },
    { label: $t('loginLog.failureReasonUserLocked'), value: 4 },
    { label: $t('loginLog.failureReasonUserInactive'), value: 5 },
    { label: $t('loginLog.failureReasonAccountAbnormal'), value: 6 },
    { label: $t('loginLog.failureReasonOther'), value: 7 },
  ];
}

export function getDeviceTypeOptions() {
  return [
    { label: $t('loginLog.deviceTypeDesktop'), value: 'desktop' },
    { label: $t('loginLog.deviceTypeMobile'), value: 'mobile' },
    { label: $t('loginLog.deviceTypeTablet'), value: 'tablet' },
    { label: $t('loginLog.deviceTypeOther'), value: 'other' },
  ];
}

export function getLoginTypeOptions() {
  return [
    {
      label: $t('loginLog.passwordLogin'),
      type: 'info' as TagType,
      value: 'password',
    },
    {
      label: $t('loginLog.codeLogin'),
      type: 'info' as TagType,
      value: 'code',
    },
    {
      label: $t('loginLog.qrcodeLogin'),
      type: 'info' as TagType,
      value: 'qrcode',
    },
    { label: 'Gitee', value: 'gitee', type: 'success' as TagType },
    { label: 'GitHub', value: 'github', type: 'success' as TagType },
    { label: 'QQ', value: 'qq', type: 'success' as TagType },
    { label: 'Google', value: 'google', type: 'success' as TagType },
    {
      label: $t('loginLog.wechatLogin'),
      type: 'success' as TagType,
      value: 'wechat',
    },
    {
      label: $t('loginLog.microsoftLogin'),
      type: 'success' as TagType,
      value: 'microsoft',
    },
    {
      label: $t('loginLog.dingtalkLogin'),
      type: 'success' as TagType,
      value: 'dingtalk',
    },
    {
      label: $t('loginLog.feishuLogin'),
      type: 'success' as TagType,
      value: 'feishu',
    },
  ];
}

export function getTagType(
  value: any,
  options: Array<{ type?: TagType; value: any }>,
): TagType {
  const option = options.find((item) => item.value === value);
  return option?.type || 'info';
}

export function getTagLabel(
  value: any,
  options: Array<{ label: string; value: any }>,
): string {
  const option = options.find((item) => item.value === value);
  return option?.label || String(value ?? '-');
}

export function useSearchFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'username',
      label: $t('loginLog.username'),
      componentProps: {
        placeholder: $t('loginLog.searchPlaceholder', [
          $t('loginLog.username'),
        ]),
      },
    },
    {
      component: 'Select',
      fieldName: 'status',
      label: $t('loginLog.status'),
      componentProps: {
        placeholder: $t('loginLog.selectStatus'),
        options: getStatusOptions(),
        clearable: true,
      },
    },
    {
      component: 'Select',
      fieldName: 'login_type',
      label: $t('loginLog.loginType'),
      componentProps: {
        placeholder: $t('loginLog.selectLoginType'),
        options: getLoginTypeOptions(),
        clearable: true,
      },
    },
  ];
}

export function useZqTableColumns(): Column[] {
  return [
    {
      key: 'username',
      dataKey: 'username',
      title: $t('loginLog.username'),
      width: 120,
    },
    {
      key: 'status',
      title: $t('loginLog.status'),
      width: 100,
      align: 'center' as const,
      slots: { default: 'cell-status' },
    },
    {
      key: 'login_type',
      title: $t('loginLog.loginType'),
      width: 120,
      align: 'center' as const,
      slots: { default: 'cell-login_type' },
    },
    {
      key: 'login_ip',
      dataKey: 'login_ip',
      title: $t('loginLog.loginIp'),
      width: 140,
    },
    {
      key: 'ip_location',
      dataKey: 'ip_location',
      title: $t('loginLog.ipLocation'),
      width: 150,
    },
    {
      key: 'failure_reason',
      title: $t('loginLog.failureReason'),
      width: 120,
      align: 'center' as const,
      slots: { default: 'cell-failure_reason' },
    },
    {
      key: 'failure_message',
      dataKey: 'failure_message',
      title: $t('loginLog.failureMessage'),
      width: 180,
      showOverflowTooltip: true,
    },
    {
      key: 'browser_type',
      dataKey: 'browser_type',
      title: $t('loginLog.browserType'),
      width: 120,
    },
    {
      key: 'os_type',
      dataKey: 'os_type',
      title: $t('loginLog.osType'),
      width: 120,
    },
    {
      key: 'device_type',
      title: $t('loginLog.deviceType'),
      width: 100,
      align: 'center' as const,
      slots: { default: 'cell-device_type' },
    },
    {
      key: 'duration',
      dataKey: 'duration',
      title: $t('loginLog.durationSeconds'),
      width: 120,
    },
    {
      key: 'remark',
      dataKey: 'remark',
      title: $t('loginLog.remark'),
      width: 150,
      showOverflowTooltip: true,
    },
    {
      key: 'sys_create_datetime',
      dataKey: 'sys_create_datetime',
      title: $t('loginLog.loginTime'),
      width: 180,
      sortable: true,
    },
    {
      key: 'actions',
      title: $t('loginLog.operation'),
      width: 150,
      fixed: true,
      align: 'center' as const,
      slots: { default: 'cell-actions' },
    },
  ];
}
