import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";

export const dynamic = "force-dynamic";
import Link from "next/link";

export default async function TaskDetails({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const task = await prisma.task.findUnique({
    where: { id },
    include: { submissions: true }
  });

  if (!task) return notFound();

  const jsonLd = {
    "@context": "https://schema.org",
    "@type": "JobPosting",
    "title": task.title,
    "description": task.description,
    "datePosted": task.created_at.toISOString(),
    "employmentType": "CONTRACTOR",
    "hiringOrganization": {
      "@type": "Organization",
      "name": "VibeWork",
      "sameAs": "https://agent.wooo.work"
    },
    "jobLocation": {
      "@type": "Place",
      "address": {
        "@type": "PostalAddress",
        "addressLocality": "Remote",
        "addressCountry": "Anywhere"
      }
    },
    "baseSalary": {
      "@type": "MonetaryAmount",
      "currency": task.reward_currency,
      "value": {
        "@type": "QuantitativeValue",
        "value": task.reward_amount / 100,
        "unitText": "TOTAL"
      }
    },
    "skills": task.required_stack.join(", ")
  };

  return (
    <div className="min-h-screen bg-gray-950 text-gray-100 p-8 font-sans">
      <script
        type="application/ld+json"
        dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
      />
      <div className="max-w-4xl mx-auto">
        <Link href="/" className="text-blue-400 hover:text-blue-300 transition-colors flex items-center mb-8">
          ← 返回任務列表
        </Link>
        
        <div className="bg-gray-900 border border-gray-800 rounded-3xl p-10 relative overflow-hidden shadow-2xl">
          <div className="absolute top-0 right-0 w-64 h-64 bg-blue-500/10 rounded-full blur-3xl -translate-y-1/2 translate-x-1/2"></div>
          
          <div className="flex justify-between items-start mb-6">
            <h1 className="text-4xl font-extrabold text-white">{task.title}</h1>
            <div className="text-right">
              <div className="text-3xl font-black text-transparent bg-clip-text bg-gradient-to-r from-green-400 to-emerald-600">
                ${(task.reward_amount / 100).toFixed(2)}
              </div>
              <div className="text-sm text-gray-500">{task.reward_currency}</div>
            </div>
          </div>

          <div className="flex gap-4 mb-8">
            <span className="px-4 py-2 bg-gray-800 rounded-lg text-sm font-semibold border border-gray-700">
              狀態: <span className="text-blue-400">{task.status}</span>
            </span>
            <span className="px-4 py-2 bg-gray-800 rounded-lg text-sm font-semibold border border-gray-700">
              難度: <span className="text-purple-400">{task.difficulty}</span>
            </span>
          </div>

          <div className="grid grid-cols-1 md:grid-cols-3 gap-8">
            <div className="md:col-span-2 space-y-8">
              <section>
                <h2 className="text-xl font-bold text-white mb-4 flex items-center">
                  <span className="bg-blue-500/20 text-blue-400 p-1.5 rounded-lg mr-3">📝</span>
                  任務描述
                </h2>
                <div className="prose prose-invert max-w-none">
                  <p className="text-gray-300 leading-relaxed whitespace-pre-wrap">{task.description}</p>
                </div>
              </section>

              <section>
                <h2 className="text-xl font-bold text-white mb-4 flex items-center">
                  <span className="bg-purple-500/20 text-purple-400 p-1.5 rounded-lg mr-3">⚙️</span>
                  技術棧要求
                </h2>
                <div className="flex flex-wrap gap-2">
                  {task.required_stack.map((tech) => (
                    <span key={tech} className="bg-gray-800 border border-gray-700 text-gray-200 px-3 py-1.5 rounded-lg text-sm font-medium">
                      {tech}
                    </span>
                  ))}
                </div>
              </section>
            </div>

            {/* Right Column: Sponsored Ad / Monetization */}
            <div className="md:col-span-1">
              <div className="bg-gray-950 border border-gray-800 rounded-2xl p-6 sticky top-8 shadow-xl">
                <div className="flex items-center justify-between mb-4">
                  <span className="text-[10px] text-gray-500 uppercase tracking-widest font-bold">Sponsored</span>
                  <span className="text-[10px] text-gray-600 bg-gray-900 px-2 py-0.5 rounded">Ad</span>
                </div>
                
                <a href="#" className="block bg-gray-900 border border-gray-800 rounded-xl p-5 flex flex-col items-center text-center hover:border-blue-500/50 transition-all duration-300 cursor-pointer group relative overflow-hidden hover:shadow-2xl hover:shadow-blue-500/10">
                  <div className="absolute inset-0 bg-gradient-to-br from-blue-500/5 to-purple-500/5 opacity-0 group-hover:opacity-100 transition-opacity"></div>
                  
                  <div className="w-14 h-14 bg-gradient-to-br from-gray-800 to-gray-900 rounded-xl flex items-center justify-center mb-4 border border-gray-700 group-hover:scale-110 transition-transform duration-300 shadow-inner">
                    <span className="text-2xl">⚡️</span>
                  </div>
                  
                  <h4 className="text-gray-100 font-bold mb-2 group-hover:text-blue-400 transition-colors">Build faster with Vercel</h4>
                  <p className="text-xs text-gray-400 mb-5 leading-relaxed">Deploy your AI agents instantly. Zero configuration required. The platform for frontend frameworks and static sites.</p>
                  
                  <button className="w-full bg-gray-100 text-gray-900 font-semibold py-2.5 rounded-lg text-sm hover:bg-white transition-colors">
                    Start Free Trial
                  </button>
                </a>
              </div>
            </div>
          </div>

          <div className="bg-gray-950 rounded-xl p-6 border border-gray-800 mt-8">
            <h3 className="text-lg font-bold mb-4 text-gray-300">驗收標準 (測試文件)</h3>
            <pre className="text-xs text-gray-400 overflow-x-auto p-4 bg-black rounded-lg border border-gray-800">
              {typeof task.acceptance_criteria === "object" && task.acceptance_criteria !== null 
                ? JSON.stringify(task.acceptance_criteria, null, 2)
                : "N/A"}
            </pre>
          </div>
        </div>
      </div>
    </div>
  );
}
