---
import Base from './Base.astro'

const years = await Astro.glob('../pages/archivio/*/index.md')
const currentYear = years
    .map(module => module.file.split('/').at(-2))
    .toSorted()
    .at(-1)
const {
    file,
    frontmatter: { title, tutors: tutorEntries },
} = Astro.props
const selectedCourseLabel = file.split('/').at(-1)

const allCourses = await Astro.glob(`../pages/archivio/*/*`)
const courses = allCourses.filter(module => module.file.includes(file.split('/').at(-2)))

function upperFirst(string) {
    return string.charAt(0).toUpperCase() + string.slice(1)
}
---

<Base title={title} courses={courses} selectedCourseLabel={selectedCourseLabel}>
    <h1>{title}</h1>
    {
        tutorEntries && (
            <div class="tutor-entries">
                <h2>{tutorEntries.length === 1 ? 'Il' : 'I'} Tutor</h2>
                {tutorEntries.map(({ name, image, contacts }) => (
                    <div class="tutor">
                        <h3 class="name">{name ?? '???'}</h3>
                        <img class="image" src={image ? image : '/tutors/placeholder-person.svg'} alt={name} />
                        {contacts && (
                            <ul class="contacts">
                                {contacts.map(({ type, value }) => (
                                    <li>
                                        <strong>{upperFirst(type)}</strong>:{' '}
                                        {type === 'email' ? (
                                            <a href={`mailto:${value}`}>{value}</a>
                                        ) : type === 'telegram' ? (
                                            <a href={`https://t.me/${value}`}>{value}</a>
                                        ) : type === 'website' ? (
                                            <a href={value}>{value}</a>
                                        ) : (
                                            value
                                        )}
                                    </li>
                                ))}
                            </ul>
                        )}
                    </div>
                ))}
            </div>
        )
    }
    <slot />
</Base>
