import { Verified } from 'lucide-react'
import { type ReactNode, type HTMLAttributes } from 'react'

import { cn } from '@/lib/utils'

// Status configuration interface
interface StatusConfig {
  icon? : ReactNode
  label: string
  className: string
}

// Status configurations for different entity types
const statusConfig: Record<string, Record<string, StatusConfig>> = {
  member: {
    verified: {
      icon : <Verified size={16}/>,
      label: 'Terverifikasi',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    unverified: {
      label: 'Belum Terverifikasi',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
  },
  savings: {
    active: {
      label: 'Aktif',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    inactive: {
      label: 'Tidak Aktif',
      className: 'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-200',
    },
    closed: {
      label: 'Tutup',
      className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
    },
    frozen: {
      label: 'Dibekukan',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
  },
  financing: {
    proposed: {
      label: 'Diajukan',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
    approved: {
      label: 'Disetujui',
      className: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
    },
    disbursed: {
      label: 'Dicairkan',
      className: 'bg-indigo-100 text-indigo-800 dark:bg-indigo-900 dark:text-indigo-200',
    },
    active: {
      label: 'Aktif',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    paid_off: {
      label: 'Lunas',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    rejected: {
      label: 'Ditolak',
      className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
    },
    written_off: {
      label: 'Dihapus Buku',
      className: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
    },
  },
  collectibility: {
    1: {
      label: 'Lancar',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    2: {
      label: 'Dalam Perhatian',
      className: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
    },
    3: {
      label: 'Kurang Lancar',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
    4: {
      label: 'Diragukan',
      className: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
    },
    5: {
      label: 'Macet',
      className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
    },
  },
  installment: {
    pending: {
      label: 'Belum Bayar',
      className: 'bg-gray-100 text-gray-800 dark:bg-gray-800 dark:text-gray-200',
    },
    partial: {
      label: 'Sebagian',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
    'partial-overdue': {
      label: 'Sebagian Telat',
      className: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
    },
    paid: {
      label: 'Lunas',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    skipped: {
      label: 'Dilewati',
      className: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200',
    },
    overdue: {
      label: 'Telat',
      className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
    },
  },
  journal: {
    draft: {
      label: 'Draft',
      className: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
    },
    posted: {
      label: 'Dikonfirmasi',
      className: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    },
    void: {
      label: 'Dibatalkan',
      className: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
    },
  },
}

export type StatusType = keyof typeof statusConfig
export type MemberStatus = 'verified' | 'unverified'
export type SavingsStatus = 'active' | 'inactive' | 'closed' | 'frozen'
export type FinancingStatus = 'proposed' | 'approved' | 'disbursed' | 'active' | 'paid_off' | 'rejected' | 'written_off'
export type CollectibilityScore = 1 | 2 | 3 | 4 | 5
export type InstallmentStatus = 'pending' | 'partial' | 'partial-overdue' | 'paid' | 'skipped' | 'overdue'

export interface StatusBadgeProps extends HTMLAttributes<HTMLDivElement> {
  type: StatusType
  status: string
  customLabel?: string
}

export function StatusBadge({
  type,
  status,
  customLabel,
  className,
  ...props
}: StatusBadgeProps) {
  const config = statusConfig[type]?.[status]

  if (!config) {
    return null
  }

  return (
    <div
      className={cn(
        'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-semibold transition-colors gap-1 ',
        config.className,
        className
      )}
      {...props}
    >
      { config.icon} {customLabel ||config.label}
    </div>
  )
}
