export function isValidGstin(gstin: string): boolean {
  if (!gstin || typeof gstin !== 'string') return false;
  const clean = gstin.trim().toUpperCase();
  if (clean.length !== 15) return false;
  // Indian GSTIN standard format: 2 digit state code + 10 char PAN + 1 entity num + 'Z' + 1 checksum char
  const gstinRegex = /^[0-9]{2}[A-Z]{5}[0-9]{4}[A-Z]{1}[1-9A-Z]{1}Z[0-9A-Z]{1}$/;
  return gstinRegex.test(clean);
}

export function deriveStateCodeFromGstin(gstin: string): string | null {
  if (!gstin || typeof gstin !== 'string') return null;
  const clean = gstin.trim().toUpperCase();
  if (clean.length >= 2 && /^[0-9]{2}/.test(clean)) {
    return clean.substring(0, 2);
  }
  return null;
}
