import type { TerminalLine } from '$lib/components/tui/types';
import { sortedCards } from '$lib/config';
import { projectNavLinks } from './projectNav';
import { tagColor } from './palette';

const totalHackathons = sortedCards.length;
const totalAwards = sortedCards.filter(c => c.awards && c.awards.length > 0).length;
const featuredCount = sortedCards.filter(c => c.featured).length;

const hackathonSections: TerminalLine[] = sortedCards.flatMap(card => {
    const hasImage = !!card.image;
    const hasAwards = !!card.awards?.length;
    const icon = hasAwards ? 'mdi:trophy' : 'mdi:rocket-launch';

    const infoChildren: TerminalLine[] = [
        {
            type: 'output' as const,
            content: `(&icon, ${icon}) (&bg-green,black) HACKATHON (&)${card.featured ? ' (&bg-orange,black) FEATURED (&)' : ''}`
        },
        ...(card.hackathonName
            ? [{ type: 'info' as const, content: `(&primary)${card.hackathonName}(&)${card.year ? ` (&muted)(${card.year})(&)` : ''}` }]
            : []),
        ...(card.university
            ? [{ type: 'output' as const, content: `(&muted)${card.university}${card.location ? `, ${card.location}` : ''}(&)` }]
            : []),
        { type: 'output' as const, content: `(&text)${card.description}(&)` },
        ...(card.awards?.length
            ? card.awards.map(award => ({ type: 'success' as const, content: `(&success)🏆 ${award.place} — ${award.track}(&)` }))
            : []),
        ...(card.tags?.length
            ? [{ type: 'output' as const, content: card.tags.map((tag, i) => `(&bg-${tagColor(i)},black) ${tag} (&)`).join(' ') }]
            : []),
        ...(card.liveWarning ? [{ type: 'warning' as const, content: `(&warning)Demo may be unavailable(&)` }] : []),
        {
            type: 'group' as const,
            content: '',
            groupDirection: 'row' as const,
            groupGap: '0.75rem',
            groupExpand: true,
            children: [
                ...(card.link
                    ? [{ type: 'button' as const, content: 'View Demo', href: card.link, icon: 'mdi:open-in-new', style: 'primary' as const, flex: true }]
                    : []),
                ...(card.repo
                    ? [{ type: 'button' as const, content: 'View Code', href: card.repo, icon: 'mdi:github', style: 'accent' as const, flex: true }]
                    : []),
                ...(card.devpost
                    ? [{ type: 'button' as const, content: 'View on Devpost', href: card.devpost, icon: 'mdi:rocket-launch', style: 'secondary' as const, flex: true }]
                    : [])
            ]
        }
    ];

    return [
        { type: 'divider' as const, content: card.title.toUpperCase() },
        ...(hasImage
            ? [{ type: 'image' as const, content: '', image: card.image as string, imageAlt: card.title, imageWidth: 350, inline: true }]
            : []),
        { type: 'group' as const, content: '', inline: hasImage, groupDirection: 'column' as const, groupAlign: 'stretch' as const, children: infoChildren },
        { type: 'blank' as const, content: '' }
    ];
});

export const lines: TerminalLine[] = [
    { type: 'command' as const, content: 'ls ~/projects/hackathons' },
    { type: 'blank' as const, content: '', height: '1rem' },
    projectNavLinks,
    { type: 'blank' as const, content: '', height: '1rem' },
    { type: 'header' as const, content: 'Hackathon Journey' },
    { type: 'output' as const, content: `(&muted)Total:(&) (&primary)${totalHackathons}(&) (&muted)| Awards:(&) (&yellow)${totalAwards}(&) (&muted)| Featured:(&) (&accent)${featuredCount}(&)` },
    { type: 'blank' as const, content: '' },
    ...hackathonSections
];
