import { cn } from '@/lib/utils';
import { Badge } from '@/components/ui/badge';

export interface CultivationInfo {
    id: number;
    name: string;
    tier: 'none' | 'low' | 'mid' | 'high' | 'master' | 'celestial';
}

export const cultivations: Record<number, CultivationInfo> = {
    0: { id: 0, name: 'No Cultivation', tier: 'none' },
    1: { id: 1, name: 'Spiritual Adept', tier: 'low' },
    2: { id: 2, name: 'Aware of Principle', tier: 'low' },
    3: { id: 3, name: 'Aware of Harmony', tier: 'low' },
    4: { id: 4, name: 'Aware of Discord', tier: 'low' },
    5: { id: 5, name: 'Aware of Coalescence', tier: 'mid' },
    6: { id: 6, name: 'Transcendent', tier: 'mid' },
    7: { id: 7, name: 'Enlightened One', tier: 'mid' },
    8: { id: 8, name: 'Aware of Vacuity', tier: 'high' },
    20: { id: 20, name: 'Aware of the Myriad', tier: 'master' },
    21: { id: 21, name: 'Master of Harmony', tier: 'master' },
    22: { id: 22, name: 'Celestial Sage', tier: 'celestial' },
    30: { id: 30, name: 'Aware of the Void', tier: 'master' },
    31: { id: 31, name: 'Master of Discord', tier: 'master' },
    32: { id: 32, name: 'Celestial Demon', tier: 'celestial' },
};

export function getCultivationInfo(level2: number): CultivationInfo {
    // level2 format: high byte = path (1=Sage, 0=Demon path), low byte = cultivation level
    const cultivationLevel = level2 & 0xFF; // Extract low byte (cultivation level)
    return cultivations[cultivationLevel] ?? { id: cultivationLevel, name: `Unknown (${cultivationLevel})`, tier: 'none' };
}

export function getCultivationName(level2: number): string {
    return getCultivationInfo(level2).name;
}

const tierVariants: Record<CultivationInfo['tier'], string> = {
    none: 'bg-muted text-muted-foreground',
    low: 'bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200',
    mid: 'bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200',
    high: 'bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200',
    master: 'bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200',
    celestial: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200 border-yellow-400',
};

interface CultivationBadgeProps {
    level2: number;
    showId?: boolean;
    className?: string;
}

export function CultivationBadge({ level2, showId = false, className }: CultivationBadgeProps) {
    const info = getCultivationInfo(level2);

    return (
        <Badge 
            variant="outline" 
            className={cn(tierVariants[info.tier], className)}
        >
            {info.name}
            {showId && <span className="ml-1 opacity-60">({level2})</span>}
        </Badge>
    );
}

export default CultivationBadge;
