import Link from "next/link"
import { getTranslations } from "next-intl/server"
import { ArrowLeft, CalendarDays } from "lucide-react"

import { getAuthContext } from "@/shared/lib/auth-guard"
import { Permissions } from "@/shared/types/permissions"
import { Button } from "@/shared/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/shared/components/ui/card"
import { getLeaveRequests } from "@/modules/leave-requests/data-access"
import { LeaveReviewList } from "@/modules/leave-requests/components/leave-review-list"

export const dynamic = "force-dynamic"

/**
 * 教师请假审批页面。
 *
 * L-5 功能：
 * - 列表展示教师所辖班级的所有请假申请（按 dataScope=class_taught 过滤）
 * - 待审批项可点击「审批」按钮打开对话框进行批准/拒绝
 */
export default async function TeacherLeavePage() {
  const t = await getTranslations("leave")
  const ctx = await getAuthContext()

  // 校验 LEAVE_REQUEST_REVIEW 权限（教师/管理员才有）
  if (!ctx.permissions.includes(Permissions.LEAVE_REQUEST_REVIEW)) {
    return (
      <div className="p-6 md:p-8 space-y-6">
        <div className="space-y-1">
          <h1 className="text-2xl font-bold tracking-tight">{t("title.teacher")}</h1>
          <p className="text-sm text-muted-foreground">{t("description.teacher")}</p>
        </div>
        <Card>
          <CardContent className="py-10 text-center text-sm text-muted-foreground">
            {t("empty.teacherDesc")}
          </CardContent>
        </Card>
      </div>
    )
  }

  const result = await getLeaveRequests({
    scope: ctx.dataScope,
    currentUserId: ctx.userId,
    page: 1,
    pageSize: 50,
  })

  return (
    <div className="p-6 md:p-8 space-y-6">
      <div className="space-y-1">
        <h1 className="text-2xl font-bold tracking-tight">{t("title.teacher")}</h1>
        <p className="text-sm text-muted-foreground">{t("description.teacher")}</p>
      </div>

      <Button asChild variant="ghost" size="sm" className="gap-2 -ml-2">
        <Link href="/teacher/dashboard">
          <ArrowLeft className="h-4 w-4" />
          {t("backToDashboard")}
        </Link>
      </Button>

      <Card>
        <CardHeader>
          <CardTitle className="flex items-center gap-2 text-base">
            <CalendarDays className="h-4 w-4 text-muted-foreground" aria-hidden />
            {t("onlineLeave")}
          </CardTitle>
        </CardHeader>
        <CardContent>
          <LeaveReviewList
            items={result.items}
            emptyTitle={t("empty.teacherTitle")}
            emptyDescription={t("empty.teacherDesc")}
          />
        </CardContent>
      </Card>
    </div>
  )
}
