import { db } from "@/lib/db";
import { requireAuth } from "@/lib/session";
import ShopClient from "./ShopClient";

export default async function ShopPage({
  searchParams,
}: {
  searchParams: { status?: string; refId?: string };
}) {
  const session = await requireAuth();
  const userId = (session.user as any).id;

  const [packages, team] = await Promise.all([
    db.package.findMany({ where: { isActive: true }, orderBy: { price: "asc" } }),
    db.team.findUnique({ where: { userId }, select: { budget: true, name: true } }),
  ]);

  return (
    <div className="max-w-4xl mx-auto py-10 px-6">
      <h1 className="text-3xl font-bold mb-2">فروشگاه</h1>
      <p className="text-gray-500 mb-8">بودجه تیمت رو افزایش بده و بازیکنان بهتری بگیر</p>

      {searchParams.status === "success" && (
        <div className="bg-green-50 border border-green-200 rounded-2xl p-5 mb-6 flex items-center gap-3">
          <span className="text-3xl">🎉</span>
          <div>
            <div className="font-bold text-green-700">پرداخت موفق</div>
            <div className="text-sm text-green-600">کد پیگیری: {searchParams.refId}</div>
          </div>
        </div>
      )}

      {(searchParams.status === "failed" || searchParams.status === "cancelled") && (
        <div className="bg-red-50 border border-red-200 rounded-2xl p-5 mb-6">
          <div className="font-bold text-red-600">پرداخت ناموفق بود</div>
          <div className="text-sm text-red-500">لطفاً دوباره امتحان کنید</div>
        </div>
      )}

      {team && (
        <div className="bg-green-800 text-white rounded-2xl p-5 mb-8 flex justify-between items-center">
          <div>
            <div className="text-sm text-green-300">بودجه فعلی تیم</div>
            <div className="text-3xl font-bold">{team.budget.toFixed(1)}M</div>
          </div>
          <div className="text-4xl">💰</div>
        </div>
      )}

      <ShopClient packages={packages} />
    </div>
  );
}
