import { Badge } from 'flowbite-react'
import {
  FiCheckCircle,
  FiClock,
  FiExternalLink,
  FiStar,
  FiUser,
  FiWatch,
} from 'react-icons/fi'

import badgeTheme from '@/lib/themes/badge'
import {
  formatDuration,
  formatToMYT,
  getSecondsBetween,
} from '@/lib/utils/date'

interface FollowUpDetailsProps {
  followUp: FollowUp
}

export default function FollowUpDetails({ followUp }: FollowUpDetailsProps) {
  const responseSeconds = getSecondsBetween(
    followUp.eligible_at,
    followUp.followed_up_at,
  )
  const responseTime = formatDuration(
    responseSeconds ?? followUp.response_seconds,
  )

  const cardClassName =
    'flex min-w-0 flex-col rounded-xl border border-green-100 bg-white/80 p-4 shadow-sm shadow-green-100/40'
  const labelClassName =
    'flex items-center gap-2 text-xs font-semibold uppercase tracking-wide text-green-700'
  const valueClassName = 'mt-2 break-words text-sm font-semibold text-slate-900'

  return (
    <section className="mt-8 max-w-6xl">
      <div className="relative overflow-hidden rounded-2xl border border-green-200 bg-gradient-to-br from-green-50 via-white to-emerald-50 p-5 shadow-sm sm:p-6">
        <div
          className="absolute -right-10 -top-10 h-36 w-36 rounded-full bg-green-100/70"
          aria-hidden="true"
        />

        <div className="relative">
          <div className="flex flex-wrap items-center gap-3">
            <div className="flex h-11 w-11 items-center justify-center rounded-full bg-green-600 text-white shadow-md shadow-green-200">
              <FiCheckCircle className="h-5 w-5" />
            </div>

            <div>
              <h3 className="text-xl font-semibold text-green-950">
                Follow Up Details
              </h3>
              <p className="text-sm font-medium text-green-700">
                Follow-up has been successfully recorded.
              </p>
            </div>

            <Badge color="success" className="sm:ml-auto">
              Follow-Up Completed
            </Badge>
          </div>

          <div className="mt-6 grid grid-cols-1 gap-3 sm:grid-cols-2 sm:gap-4 lg:grid-cols-3">
            <div className={cardClassName}>
              <div className={labelClassName}>
                <FiClock className="h-4 w-4 shrink-0" />
                Follow Up Time
              </div>
              <p className={valueClassName}>
                {formatToMYT(
                  followUp.followed_up_at,
                  "MMMM dd, yyyy 'at' HH:mm",
                )}
              </p>
            </div>

            <div className={cardClassName}>
              <div className={labelClassName}>
                <FiExternalLink className="h-4 w-4 shrink-0" />
                Follow Up Link
              </div>
              <a
                href={followUp.evidence_url}
                target="_blank"
                rel="noreferrer noopener"
                className="mt-2 block break-all text-sm font-semibold text-blue-600 hover:underline"
              >
                {followUp.evidence_url}
              </a>
            </div>

            <div className={`${cardClassName} sm:col-span-2 lg:col-span-1`}>
              <div className={labelClassName}>
                <FiUser className="h-4 w-4 shrink-0" />
                Follow Up By
              </div>
              <p className={valueClassName}>
                {followUp.officer?.name ?? 'N/A'}
              </p>
            </div>

            <div className="col-span-1 grid grid-cols-1 gap-3 sm:col-span-2 sm:grid-cols-2 sm:gap-4 lg:col-span-3">
              <div className={cardClassName}>
                <div className={labelClassName}>
                  <FiWatch className="h-4 w-4 shrink-0" />
                  Response Time
                </div>
                <p className="mt-2 font-mono text-sm font-semibold tabular-nums text-slate-900">
                  {responseTime}
                </p>
              </div>

              <div className={cardClassName}>
                <div className={labelClassName}>
                  <FiStar className="h-4 w-4 shrink-0" />
                  Rating
                </div>
                <div className="mt-2 flex min-h-6 flex-wrap items-center">
                  {followUp.rating ? (
                    <Badge
                      theme={badgeTheme.badge}
                      color="none"
                      className="max-w-full break-words"
                      style={{
                        backgroundColor: followUp.rating.background_color,
                        color: followUp.rating.text_color,
                      }}
                    >
                      {followUp.rating.label}
                    </Badge>
                  ) : (
                    <span className="text-sm font-semibold text-slate-400">
                      -
                    </span>
                  )}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  )
}
