import Link from 'next/link';

import { Author, Tag } from '@/lib/types/taxonomy';
import { toMilitaryDTG } from '@/lib/utils/date';

import styles from './Meta.module.css';

interface MetaProps {
  author: Author | null;
  tags: Tag[];
  updateDate?: string;
  publicationDate: string;
}

export default function Meta({
  author,
  tags,
  updateDate,
  publicationDate,
}: MetaProps) {
  return (
    <div className={styles.meta}>
      {author && (
        <div className={styles.section}>
          <span className={styles.label}>Author</span>
          <span className={styles.author}>
            <Link href={author.slug}>{author.name}</Link>
          </span>
        </div>
      )}
      {tags && tags.length > 0 && (
        <div className={styles.section}>
          <span className={styles.label}>Tags</span>
          <span className={styles.author}>
            <ul className={styles.taglist}>
              {tags.map((tag) => (
                <li key={tag.slug} className={styles.tag}>
                  <Link className={styles.link} href={`/tags/${tag.slug}`}>
                    {tag.name}
                  </Link>
                </li>
              ))}
            </ul>
          </span>
        </div>
      )}
      {publicationDate && (
        <div className={styles.metasection}>
          <span className={styles.metalabel}>Last Update</span>
          <time className={styles.updatedate}>
            ⟫
            {updateDate
              ? toMilitaryDTG(updateDate)
              : toMilitaryDTG(publicationDate)}
          </time>
        </div>
      )}
    </div>
  );
}
