import { auth } from "@/lib/auth"
import { db } from "@/lib/db"
import { redirect } from "next/navigation"
import Link from "next/link"
import { DashboardNav } from "@/components/ui/DashboardNav"
import { PhotoCarousel } from "@/components/ui/PhotoCarousel"

export default async function StudentDashboardPage() {
  const session = await auth()

  if (!session?.user) {
    redirect("/login")
  }

  // Admins belong to /admin, not here
  if (session.user.role === "ADMIN") {
    redirect("/admin")
  }


  // Fetch global settings to check if contributions are frozen
  const settings = await db.appSettings.findUnique({ where: { id: "GLOBAL_SETTINGS" } })
  const isFrozen = settings?.freezeDate != null && settings.freezeDate <= new Date()

  // Fetch student's entries
  const entries = await db.entry.findMany({
    where: {
      userId: session.user.id
    },
    include: {
      photos: true
    },
    orderBy: {
      createdAt: 'desc'
    }
  })

  return (
    <main className="container" style={{ padding: "3rem 1rem", minHeight: "100vh" }}>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: "3rem", flexWrap: "wrap", gap: "1rem" }}>
        <div>
          <h1 style={{ fontSize: "2rem", marginBottom: "0.5rem" }}>Salut, {session.user.name}</h1>
          <p style={{ color: "var(--text-muted)" }}>Iată intrările tale Ro-VIBE</p>
        </div>

        <div style={{ display: "flex", gap: "0.75rem", alignItems: "center", flexWrap: "wrap" }}>
          <Link
            href="/"
            title="Pagina Principală"
            style={{
              padding: "0.65rem 0.85rem",
              borderRadius: "var(--radius-md)",
              background: "var(--surface-color)",
              border: "1px solid var(--border-color)",
              color: "var(--text-main)",
              display: "flex",
              alignItems: "center",
              justifyContent: "center",
            }}
          >
            <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
              <path d="m3 9 9-7 9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" />
              <polyline points="9 22 9 12 15 12 15 22" />
            </svg>
          </Link>



          {!isFrozen && (
            <Link
              href="/upload"
              style={{
                backgroundColor: "var(--primary)",
                color: "white",
                padding: "0.65rem 1.1rem",
                borderRadius: "var(--radius-md)",
                fontWeight: 600,
                display: "flex",
                alignItems: "center",
                gap: "0.5rem",
                fontSize: "0.9rem"
              }}
            >
              <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14" /><path d="M12 5v14" /></svg>
              Adaugă Intrare
            </Link>
          )}
          <DashboardNav userName={session.user.name} userEmail={session.user.email} />
        </div>

      </div>

      {isFrozen ? (
        <div style={{ marginBottom: "2rem", padding: "1rem 1.5rem", background: "rgba(239, 68, 68, 0.1)", border: "1px solid rgba(239, 68, 68, 0.3)", borderLeft: "4px solid var(--danger)", borderRadius: "var(--radius-md)" }}>
          <p style={{ fontSize: "1.05rem", fontWeight: 600, color: "var(--danger)", display: "flex", alignItems: "center", gap: "0.5rem", margin: 0 }}>
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M19 11H5a2 2 0 0 0-2 2v5a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-5a2 2 0 0 0-2-2z"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg>
            Perioada de concurs s-a încheiat. Nu se mai pot adăuga sau edita contribuții.
          </p>
        </div>
      ) : (
        <div style={{ marginBottom: "2rem", padding: "1rem 1.5rem", background: "var(--surface-color)", border: "1px solid var(--border-color)", borderLeft: "4px solid var(--primary)", borderRadius: "var(--radius-md)" }}>
          <p style={{ fontSize: "1.05rem", fontWeight: 600, color: "var(--text-main)", display: "flex", alignItems: "center", gap: "0.5rem", margin: 0 }}>
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
            Competiția este deschisă. Poți adăuga contibuții noi sau poți edita intrările existente.
          </p>
        </div>
      )}

      {entries.length === 0 ? (
        <div style={{ textAlign: "center", padding: "4rem 2rem", background: "var(--surface-color)", borderRadius: "var(--radius-lg)", border: "1px dashed var(--border-color)" }}>
          <div style={{ background: "var(--primary-light)", width: "64px", height: "64px", borderRadius: "50%", display: "flex", alignItems: "center", justifyContent: "center", margin: "0 auto 1.5rem", color: "var(--primary)" }}>
            <svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="10" /><path d="M8 12h8" /><path d="M12 8v8" /></svg>
          </div>
          <h3 style={{ fontSize: "1.25rem", marginBottom: "0.5rem" }}>Nu ai nicio intrare încă</h3>
          <p style={{ color: "var(--text-muted)", marginBottom: "2rem", maxWidth: "400px", margin: "0 auto 2rem" }}>
            {isFrozen 
              ? "Competiția s-a încheiat. Nu mai poți adăuga contribuții noi."
              : "Aplicația Ro-VIBE așteaptă contribuția ta. Adaugă o nouă locație folosind formularul digital."}
          </p>
          {!isFrozen && (
            <Link
              href="/upload"
              style={{
                backgroundColor: "var(--primary)",
                color: "white",
                padding: "0.75rem 1.5rem",
                borderRadius: "var(--radius-md)",
                fontWeight: 500,
                display: "inline-block"
              }}
            >
              Începe documentarea
            </Link>
          )}
        </div>
      ) : (
        <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fill, minmax(300px, 1fr))", gap: "1.5rem" }}>
          {entries.map((entry: any) => (
            <div key={entry.id} style={{ background: "var(--surface-color)", borderRadius: "var(--radius-lg)", overflow: "hidden", border: "1px solid var(--border-color)", boxShadow: "var(--shadow-sm)" }}>
              <PhotoCarousel
                photos={entry.photos}
                altText={entry.title}
                height="200px"
                borderRadius="0"
              />

              <div style={{ padding: "1.5rem" }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start", marginBottom: "0.5rem" }}>
                  <h3 style={{ fontSize: "1.1rem", fontWeight: 600, display: "-webkit-box", WebkitLineClamp: 1, WebkitBoxOrient: "vertical", overflow: "hidden" }}>
                    {entry.title}
                  </h3>
                  <span style={{
                    fontSize: "0.75rem",
                    padding: "0.25rem 0.5rem",
                    borderRadius: "999px",
                    background: entry.status === "APPROVED" ? "rgba(16, 185, 129, 0.1)" :
                      entry.status === "REJECTED" ? "rgba(239, 68, 68, 0.1)" : "rgba(245, 158, 11, 0.1)",
                    color: entry.status === "APPROVED" ? "var(--success)" :
                      entry.status === "REJECTED" ? "var(--danger)" : "var(--warning)",
                    fontWeight: 600
                  }}>
                    {entry.status}
                  </span>
                </div>

                <p style={{ color: "var(--text-muted)", fontSize: "0.9rem", marginBottom: "1rem", display: "flex", alignItems: "center", gap: "0.25rem" }}>
                  <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z" /><circle cx="12" cy="10" r="3" /></svg>
                  {entry.city}, {entry.county}
                </p>

                {!isFrozen && (
                  <div style={{ display: "flex", gap: "0.5rem", marginTop: "1rem" }}>
                    <Link
                      href={`/entry/${entry.id}/edit`}
                      style={{ flex: 1, textAlign: "center", padding: "0.5rem", background: "var(--surface-hover)", color: "var(--text-main)", borderRadius: "var(--radius-md)", fontSize: "0.9rem", fontWeight: 500, border: "1px solid var(--border-color)" }}
                    >
                      Editează
                    </Link>
                  </div>
                )}
              </div>
            </div>
          ))}
        </div>
      )}
    </main>
  )
}
