"use client";

import type { Guide } from "@/lib/types";
import Image from "next/image";
import { forwardRef } from "react";
import { shouldBypassImageOptimization } from "@/lib/api";

interface IDCardTemplateProps {
  guide: Guide;
  validUntil: string;
  cardNumber: string;
}

export const IDCardTemplate = forwardRef<HTMLDivElement, IDCardTemplateProps>(
  function IDCardTemplate({ guide, validUntil, cardNumber }, ref) {
    return (
      <div
        ref={ref}
        id="id-card-print"
        className="w-[400px] bg-white rounded-xl overflow-hidden shadow-2xl"
        style={{ fontFamily: "Arial, sans-serif" }}
      >
        {/* Front of Card */}
        <div className="relative">
          {/* Header */}
          <div className="bg-gradient-to-r from-[#0e7490] to-[#059669] p-4 text-white">
            <div className="flex items-center justify-between">
              <div className="flex items-center gap-3">
                <div className="w-12 h-12 bg-white rounded-full flex items-center justify-center">
                  <span className="text-[#0e7490] font-bold text-xs text-center leading-tight">
                    UTTARA
                    <br />
                    KHAND
                  </span>
                </div>
                <div>
                  <h1 className="text-sm font-bold">UTTARAKHAND TOURISM</h1>
                  <p className="text-xs opacity-90">DEVELOPMENT BOARD</p>
                </div>
              </div>
              <div className="text-right">
                <p className="text-xs font-semibold">OFFICIAL</p>
                <p className="text-xs">TOURIST GUIDE</p>
              </div>
            </div>
          </div>

          {/* Body */}
          <div className="p-4">
            <div className="flex gap-4">
              {/* Photo */}
              <div className="flex-shrink-0">
                <div className="w-28 h-36 rounded-lg overflow-hidden border-2 border-[#0e7490] shadow-md">
                  <Image
                    src={guide.photo}
                    alt={guide.name}
                    width={112}
                    height={144}
                    unoptimized={shouldBypassImageOptimization(guide.photo)}
                    className="w-full h-full object-cover"
                  />
                </div>
              </div>

              {/* Details */}
              <div className="flex-1 space-y-2">
                <div>
                  <p className="text-xs text-gray-500 uppercase tracking-wide">
                    Name
                  </p>
                  <p className="font-bold text-gray-900 text-sm">
                    {guide.name}
                  </p>
                </div>
                <div>
                  <p className="text-xs text-gray-500 uppercase tracking-wide">
                    Card Number
                  </p>
                  <p className="font-mono font-bold text-[#0e7490] text-sm">
                    {cardNumber}
                  </p>
                </div>
                <div className="grid grid-cols-2 gap-2">
                  <div>
                    <p className="text-xs text-gray-500 uppercase tracking-wide">
                      Experience
                    </p>
                    <p className="font-medium text-gray-900 text-xs">
                      {guide.experience} years
                    </p>
                  </div>
                  <div>
                    <p className="text-xs text-gray-500 uppercase tracking-wide">
                      Phone
                    </p>
                    <p className="font-medium text-gray-900 text-xs">
                      {guide.phone}
                    </p>
                  </div>
                </div>
              </div>
            </div>

            {/* Expertise */}
            <div className="mt-3">
              <p className="text-xs text-gray-500 uppercase tracking-wide mb-1">
                Expertise
              </p>
              <div className="flex flex-wrap gap-1">
                {guide.expertise.slice(0, 4).map((exp, index) => (
                  <span
                    key={`${exp}-${index}`}
                    className="px-2 py-0.5 bg-[#0e7490]/10 text-[#0e7490] rounded text-xs font-medium"
                  >
                    {exp}
                  </span>
                ))}
              </div>
            </div>

            {/* Languages */}
            <div className="mt-2">
              <p className="text-xs text-gray-500 uppercase tracking-wide mb-1">
                Spoken Languages
              </p>
              <p className="text-xs text-gray-700">
                {guide.languages.join(", ")}
              </p>
            </div>

            {/* Districts */}
            <div className="mt-2">
              <p className="text-xs text-gray-500 uppercase tracking-wide mb-1">
                Operating Districts
              </p>
              <p className="text-xs text-gray-700">
                {guide.districts.slice(0, 3).join(", ")}
              </p>
            </div>
          </div>

          {/* Footer */}
          <div className="bg-gray-100 px-4 py-3 border-t">
            <div className="flex justify-between items-center">
              <div>
                <p className="text-xs text-gray-500">Card No.</p>
                <p className="font-mono text-xs font-bold">{cardNumber}</p>
              </div>
              <div className="text-center">
                <p className="text-xs text-gray-500">Status</p>
                <p className="text-xs font-medium text-green-600">VERIFIED</p>
              </div>
              <div className="text-right">
                <p className="text-xs text-gray-500">Valid Until</p>
                <p className="text-xs font-medium">
                  {new Date(validUntil).toLocaleDateString("en-IN")}
                </p>
              </div>
            </div>
          </div>

          {/* Bottom Strip */}
          <div className="h-2 bg-gradient-to-r from-[#0e7490] via-[#059669] to-[#0e7490]" />
        </div>
      </div>
    );
  },
);
