import CryptoJS from 'crypto-js'

const SIGNATURE_SECRET = import.meta.env.VITE_SIGNATURE_SECRET || 'NovalonManageSystemSecretKey2026'

export interface SignatureHeaders {
  'X-Signature': string
  'X-Timestamp': string
  'X-Nonce': string
}

export function generateSignature(
  method: string,
  path: string,
  query: string = '',
  body: string = '',
  timestamp: number,
  nonce: string
): string {
  const stringToSign = buildStringToSign(method, path, query, body, timestamp, nonce)
  
  const signature = CryptoJS.HmacSHA256(stringToSign, SIGNATURE_SECRET)
  const signatureBase64 = CryptoJS.enc.Base64.stringify(signature)
  
  return signatureBase64
}

export function generateSignatureHeaders(
  method: string,
  url: string,
  body?: any
): SignatureHeaders {
  const timestamp = Date.now()
  const nonce = generateNonce()
  
  const { path, query } = parseUrl(url)
  
  const signature = generateSignature(
    method.toUpperCase(),
    path,
    query || '',
    '',
    timestamp,
    nonce
  )
  
  return {
    'X-Signature': signature,
    'X-Timestamp': timestamp.toString(),
    'X-Nonce': nonce
  }
}

function buildStringToSign(
  method: string,
  path: string,
  query: string,
  body: string,
  timestamp: number,
  nonce: string
): string {
  return [
    method,
    path,
    query || '',
    body || '',
    timestamp.toString(),
    nonce
  ].join('\n')
}

function generateNonce(): string {
  const timestamp = Date.now().toString(36)
  const randomPart = Math.random().toString(36).substring(2, 15)
  return `${timestamp}-${randomPart}`
}

function parseUrl(url: string): { path: string; query: string } {
  if (url.startsWith('http://') || url.startsWith('https://')) {
    const urlObj = new URL(url)
    return {
      path: urlObj.pathname,
      query: urlObj.search.substring(1)
    }
  }
  
  const queryIndex = url.indexOf('?')
  if (queryIndex === -1) {
    return { path: url, query: '' }
  }
  
  return {
    path: url.substring(0, queryIndex),
    query: url.substring(queryIndex + 1)
  }
}
