"use client";

import { useEffect, useState } from "react";
import { Container } from "@/components/common/Container";
import { Card, CardContent } from "@/components/common/Card";
import { cn } from "@/lib/utils";

function Bone({ className }: { className?: string }) {
  return (
    <div className={cn("animate-pulse rounded-xl bg-slate-200 dark:bg-slate-700/50", className)} />
  );
}

const SWAP_INTERVAL_MS = 2200;

// Real page renders one of two very different layouts once status is known
// (upload form for unverified, centered status card for pending/verified).
// The skeleton can't know which ahead of time, so it cross-fades between
// both shapes instead of guessing wrong and causing a layout jump on swap.
function FormShape() {
  return (
    <>
      <Bone className="h-[72px] rounded-2xl" />

      <Card>
        <CardContent>
          <div className="flex flex-col gap-1">
            <Bone className="h-5 w-40" />
            <Bone className="mt-0.5 h-3.5 w-60" />
          </div>

          <div className="mt-6 space-y-6">
            <div className="flex flex-col gap-1.5">
              <Bone className="h-4 w-28" />
              <Bone className="h-[42px] rounded-xl" />
            </div>

            <div className="grid gap-5 sm:grid-cols-2">
              <div className="flex flex-col gap-1.5">
                <Bone className="h-4 w-24" />
                <Bone className="h-40 rounded-2xl" />
              </div>
              <div className="flex flex-col gap-1.5">
                <Bone className="h-4 w-24" />
                <Bone className="h-40 rounded-2xl" />
              </div>
            </div>

            <div className="flex items-center gap-4 border-t border-slate-100 pt-5 dark:border-slate-800">
              <Bone className="h-10 w-32 rounded-xl" />
            </div>
          </div>
        </CardContent>
      </Card>
    </>
  );
}

function StatusShape() {
  return (
    <Card>
      <CardContent className="flex flex-col items-center gap-5 py-14 text-center">
        <Bone className="h-16 w-16 rounded-full" />
        <div className="flex flex-col items-center gap-2">
          <Bone className="h-6 w-48" />
          <Bone className="h-4 w-64" />
        </div>
        <Bone className="h-10 w-40 rounded-xl" />
      </CardContent>
    </Card>
  );
}

export function AgentKycSkeleton() {
  const [showForm, setShowForm] = useState(true);
  const [visible, setVisible] = useState(true);

  useEffect(() => {
    let swapTimeout: ReturnType<typeof setTimeout>;
    const interval = setInterval(() => {
      setVisible(false);
      swapTimeout = setTimeout(() => {
        setShowForm((v) => !v);
        setVisible(true);
      }, 200);
    }, SWAP_INTERVAL_MS);
    return () => {
      clearInterval(interval);
      clearTimeout(swapTimeout);
    };
  }, []);

  return (
    <Container className="max-w-full space-y-6 pb-10">
      <div className="flex items-center gap-4">
        <Bone className="icon-box-md rounded-2xl" />
        <div>
          <Bone className="h-6 w-40" />
          <Bone className="mt-1 h-4 w-56" />
        </div>
      </div>

      <div className={cn("space-y-6 transition-opacity duration-200", visible ? "opacity-100" : "opacity-0")}>
        {showForm ? <FormShape /> : <StatusShape />}
      </div>
    </Container>
  );
}
