import styles from "./Event.module.css";
import Thumbnail from "@/components/Thumbnail/Thumbnail";
import Button from "@/components/Button/Button";
import Link from "next/link";
import Image from "next/image";
import React from "react";
import formatDate from "@/utils/formatDate";
import { getCachedMedia, getCachedUser } from "@/services/wp";

type EventProps = {
  title: {
    rendered: string;
  };
  excerpt: {
    rendered: string;
  };
  featured_media: number;
  acf: {
    eventbrite_link: string;
    event_date: string;
    event_location: string;
    event_host: number;
    event_time: string;
  };
};

const Event = async (props: EventProps) => {
  const media: { source_url: string; alt_text: string } = await getCachedMedia(props.featured_media);
  const user = await getCachedUser(props.acf.event_host);
  const userImage = await getCachedMedia(user.acf.profile_image);

  const formattedDate = formatDate(props.acf.event_date);

  return (
    <div className={styles.event}>
      <div className={styles.event__img}>
        <Thumbnail
          width={475}
          height={475}
          imageUrl={media.source_url}
          alt={media.alt_text}
        />
      </div>
      <div className={styles.event__info}>
        <div className={styles.event__location}>
          <svg
            className={"block mr-[0.5rem]"}
            xmlns="http://www.w3.org/2000/svg"
            height="24"
            viewBox="0 -960 960 960"
            width="24"
          >
            <path d="M480-480q33 0 56.5-23.5T560-560q0-33-23.5-56.5T480-640q-33 0-56.5 23.5T400-560q0 33 23.5 56.5T480-480Zm0 294q122-112 181-203.5T720-552q0-109-69.5-178.5T480-800q-101 0-170.5 69.5T240-552q0 71 59 162.5T480-186Zm0 106Q319-217 239.5-334.5T160-552q0-150 96.5-239T480-880q127 0 223.5 89T800-552q0 100-79.5 217.5T480-80Zm0-480Z" />
          </svg>
          {props.acf.event_location}
        </div>
        <div className={styles.event__date}>
          <svg
            className={"block mr-[0.5rem]"}
            xmlns="http://www.w3.org/2000/svg"
            height="24"
            viewBox="0 -960 960 960"
            width="24"
          >
            <path d="M580-240q-42 0-71-29t-29-71q0-42 29-71t71-29q42 0 71 29t29 71q0 42-29 71t-71 29ZM200-80q-33 0-56.5-23.5T120-160v-560q0-33 23.5-56.5T200-800h40v-80h80v80h320v-80h80v80h40q33 0 56.5 23.5T840-720v560q0 33-23.5 56.5T760-80H200Zm0-80h560v-400H200v400Zm0-480h560v-80H200v80Zm0 0v-80 80Z" />
          </svg>
          {formattedDate}&nbsp;/&nbsp;{props.acf.event_time}
        </div>
        <h3 className="h1">{props.title.rendered}</h3>
        <p
          className={styles.event__summary}
          dangerouslySetInnerHTML={{ __html: props.excerpt.rendered || "" }}
        />
        <div className={styles.event__host}>
          <div className={"h3 flex justify-start items-center"}>
            <div className="flex items-center hidden md:block">
              <Image
                className={"rounded-xl block"}
                src={userImage.source_url}
                alt={user.name}
                width={72}
                height={72}
              />
            </div>
            <span className={"block md:ml-xs text-sm lg:text-2xl"}>
              {user.name}
            </span>
          </div>
          <Link href={props.acf.eventbrite_link} target={"_blank"}>
            <Button label={"Book now"} />
          </Link>
        </div>
      </div>
    </div>
  );
};

export default Event;
