import { FormValidationError } from '../i18n/form-validation';
import { decimalRateToPercent, percentToDecimalRate } from '../utils/rate-percent';

export interface PromotableUserOption {
  id: string;
  username: string;
  status: string;
  parentId: string | null;
  parentUsername: string | null;
  phone: string | null;
  email: string | null;
}

export interface AgentCreateForm {
  userId: string;
  creditLimit: number;
  cashbackRate: number;
  maxSingleDeposit: number;
  maxDailyDeposit: number;
  phone: string;
  email: string;
}

export interface AgentEditForm {
  id: string;
  username: string;
  status: string;
  phone: string;
  email: string;
  cashbackRate: number;
  maxSingleDeposit: number;
  maxDailyDeposit: number;
  managedPassword: string | null;
  newPassword: string;
  loginFailCount: number;
  lastLoginAt: string | null;
  creditLimit: string;
  usedCredit: string;
  availableCredit: string;
  directPlayerCount: number;
  childAgentCount: number;
  level: number;
  parentUsername: string | null;
  createdAt: string;
}

export interface AgentRow {
  userId: string;
  username: string;
  userStatus: string;
  level: number;
  parentAgentId?: string | null;
  parentUsername?: string | null;
  parentChain?: string[];
  parentChainLabel?: string | null;
  status: string;
  creditLimit: string;
  usedCredit: string;
  availableCredit: string;
  directPlayerCount: number;
  childAgentCount: number;
  cashbackRate: string;
  maxSingleDeposit: string | null;
  maxDailyDeposit: string | null;
  phone: string | null;
  email: string | null;
  locale: string;
  inviteCode?: string | null;
  createdAt: string;
}

export interface AgentDetail extends AgentRow {
  parentAgentId: string | null;
  parentUsername: string | null;
  directPlayerLiability: string;
  childAgentExposure: string;
  managedPassword: string | null;
  loginFailCount: number;
  lastLoginAt: string | null;
  updatedAt: string;
  recentCreditTransactions: {
    id: string;
    transactionType: string;
    amount: string;
    creditBefore: string;
    creditAfter: string;
    remark: string | null;
    createdAt: string;
  }[];
}

export function emptyAgentCreateForm(): AgentCreateForm {
  return {
    userId: '',
    creditLimit: 50000,
    cashbackRate: 0,
    maxSingleDeposit: 0,
    maxDailyDeposit: 0,
    phone: '',
    email: '',
  };
}

export function emptyAgentEditForm(): AgentEditForm {
  return {
    id: '',
    username: '',
    status: 'ACTIVE',
    phone: '',
    email: '',
    cashbackRate: 0,
    maxSingleDeposit: 0,
    maxDailyDeposit: 0,
    managedPassword: null,
    newPassword: '',
    loginFailCount: 0,
    lastLoginAt: null,
    creditLimit: '0',
    usedCredit: '0',
    availableCredit: '0',
    directPlayerCount: 0,
    childAgentCount: 0,
    level: 1,
    parentUsername: null,
    createdAt: '',
  };
}

export function editFormFromAgentDetail(d: AgentDetail): AgentEditForm {
  return {
    id: d.userId,
    username: d.username,
    status: d.status,
    phone: d.phone ?? '',
    email: d.email ?? '',
    cashbackRate: decimalRateToPercent(d.cashbackRate),
    maxSingleDeposit: d.maxSingleDeposit ? Number(d.maxSingleDeposit) : 0,
    maxDailyDeposit: d.maxDailyDeposit ? Number(d.maxDailyDeposit) : 0,
    managedPassword: d.managedPassword ?? null,
    newPassword: '',
    loginFailCount: d.loginFailCount ?? 0,
    lastLoginAt: d.lastLoginAt ?? null,
    creditLimit: d.creditLimit,
    usedCredit: d.usedCredit,
    availableCredit: d.availableCredit,
    directPlayerCount: d.directPlayerCount,
    childAgentCount: d.childAgentCount ?? 0,
    level: d.level,
    parentUsername: d.parentUsername ?? null,
    createdAt: d.createdAt,
  };
}

export function applyPromotableUserToForm(
  form: AgentCreateForm,
  user: PromotableUserOption,
): void {
  form.userId = user.id;
  form.phone = user.phone ?? '';
  form.email = user.email ?? '';
}

export function buildCreateAgentPayload(form: AgentCreateForm) {
  if (!form.userId) throw new FormValidationError('err.user_required');
  if (form.creditLimit < 0) throw new FormValidationError('err.credit_negative');
  return {
    userId: form.userId,
    creditLimit: form.creditLimit,
    cashbackRate: percentToDecimalRate(form.cashbackRate),
    maxSingleDeposit: form.maxSingleDeposit > 0 ? form.maxSingleDeposit : undefined,
    maxDailyDeposit: form.maxDailyDeposit > 0 ? form.maxDailyDeposit : undefined,
    phone: form.phone.trim() || undefined,
    email: form.email.trim() || undefined,
  };
}
