import React from "react";
import GridShape from "../../components/common/GridShape";
import { Link } from "react-router-dom";
import ThemeTogglerTwo from "../../components/common/ThemeTogglerTwo";

interface PlanPreview {
  name?: string;
  price?: number | string;
  billing_cycle?: string;
  included_credits?: number;
  max_sites?: number;
  max_users?: number;
  features?: string[];
}

export default function AuthLayout({
  children,
  plan,
}: {
  children: React.ReactNode;
  plan?: PlanPreview | null;
}) {
  return (
    <div className="relative p-6 bg-white z-1 dark:bg-gray-900 sm:p-0">
      <div className="relative flex flex-col justify-center w-full h-screen lg:flex-row dark:bg-gray-900 sm:p-0">
        {children}
        <div className="items-center hidden w-full h-full lg:w-1/2 bg-brand-950 dark:bg-white/5 lg:grid">
          <div className="relative flex items-center justify-center z-1">
            {/* <!-- ===== Common Grid Shape Start ===== --> */}
            <GridShape />
            <div className="flex flex-col items-center max-w-sm w-full gap-6 px-4">
              <div className="flex flex-col items-center">
                <Link to="/" className="block mb-4">
                  <img
                    width={231}
                    height={48}
                    src="/images/logo/auth-logo.svg"
                    alt="Logo"
                  />
                </Link>
                <p className="text-center text-gray-400 dark:text-white/60">
                  Free and Open-Source Tailwind CSS Admin Dashboard Template
                </p>
              </div>
              {plan && (
                <div className="w-full rounded-2xl border border-white/10 bg-white/5 backdrop-blur-sm p-5 text-white shadow-lg">
                  <div className="flex items-center justify-between mb-2">
                    <div className="text-lg font-semibold">{plan.name}</div>
                    {plan.price !== undefined && (
                      <div className="text-sm font-medium">
                        ${Number(plan.price).toFixed(2)}
                        {plan.billing_cycle ? `/ ${plan.billing_cycle}` : ""}
                      </div>
                    )}
                  </div>
                  <div className="text-xs text-white/80 space-y-1">
                    {plan.included_credits !== undefined && (
                      <div>Credits: {plan.included_credits}</div>
                    )}
                    {(plan.max_sites !== undefined || plan.max_users !== undefined) && (
                      <div>
                        {plan.max_sites !== undefined ? `Sites: ${plan.max_sites}` : ""}
                        {plan.max_sites !== undefined && plan.max_users !== undefined ? " • " : ""}
                        {plan.max_users !== undefined ? `Users: ${plan.max_users}` : ""}
                      </div>
                    )}
                  </div>
                  {Array.isArray(plan.features) && plan.features.length > 0 && (
                    <ul className="mt-3 space-y-1 text-xs text-white/85 list-disc list-inside">
                      {plan.features.slice(0, 7).map((f) => (
                        <li key={f}>{f}</li>
                      ))}
                    </ul>
                  )}
                </div>
              )}
            </div>
          </div>
        </div>
        <div className="fixed z-50 hidden bottom-6 right-6 sm:block">
          <ThemeTogglerTwo />
        </div>
      </div>
    </div>
  );
}
